Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6178237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:25:24+00:00 2026-05-24T00:25:24+00:00

Learn Twisted. I decided to write a server and client that once a second

  • 0

Learn Twisted. I decided to write a server and client that once a second to share data.
Wrote one implementation, but it seems to me that it is not correct.

# -*- coding: utf-8 -*-

from twisted.spread import pb
from twisted.internet import reactor, task
from twisted.cred import credentials
from win32com.server import factory

class login_send:

    def __init__(self):
        self.count=0
        self.timeout = 1.0
        self.factory = pb.PBClientFactory()
        reactor.connectTCP("localhost", 8800, self.factory)

    def testTimeout(self):
        self.count+=1
        print self.count

        def1 = self.factory.login(credentials.UsernamePassword("test1","bb1b"))
        def1.addCallbacks(self.good_connected, self.bad_connected)
        def1.addCallback(self.send_data)
        def1.addErrback(self.disconnect)
        if self.count>10:def1.addBoth(self.disconnect)

    def start(self):
        l = task.LoopingCall(self.testTimeout)
        l.start(self.timeout)
        reactor.run()

    def good_connected(self, perspective):
        print 'good login and password', perspective
        return perspective

    def bad_connected(self, perspective):
        print 'bad login or password', perspective
        return perspective

    def send_data(self, perspective):
        print 'send'
        return perspective.callRemote("foo", self.count)

    def disconnect(self, perspective):
        print 'disconnect'
        reactor.stop()

if __name__ == "__main__":
    st=login_send()
    st.start()

Code: if login and password True -> send self.count, if login or password False -> disconnect, if self.count>10 -> disconnect

The first mistake, in my opinion is that I have to login every time.

def1 = self.factory.login(credentials.UsernamePassword("test1", "bb1b"))

How to make one authorization, and continue to send data every second?

simple test server code:

from zope.interface import implements

from twisted.spread import pb
from twisted.cred import checkers, portal
from twisted.internet import reactor

class MyPerspective(pb.Avatar):
    def __init__(self, name):
        self.name = name
    def perspective_foo(self, arg):
        print "I am", self.name, "perspective_foo(",arg,") called on", self
        return arg

class MyRealm:
    implements(portal.IRealm)
    def requestAvatar(self, avatarId, mind, *interfaces):
        if pb.IPerspective not in interfaces:
            print 'qqqq'
            raise NotImplementedError
        return pb.IPerspective, MyPerspective(avatarId), lambda:None

p = portal.Portal(MyRealm())
c = checkers.InMemoryUsernamePasswordDatabaseDontUse(test1="bbb",
                                                     user2="pass2")
p.registerChecker(c)
reactor.listenTCP(8800, pb.PBServerFactory(p))
reactor.run()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:25:25+00:00Added an answer on May 24, 2026 at 12:25 am

    I believe this should do the trick.

    # Upper case first letter of class name is good policy.
    class Login_send:
    
        def __init__(self):
            # initialize the state variable to False.
            self.connection = False
            self.count=0
            self.timeout = 1.0
            self.factory = pb.PBClientFactory()
            reactor.connectTCP("localhost", 8800, self.factory)
    
        def testTimeout(self):
            self.count+=1
            print self.count
    
            # no connection -- create one.
            if not self.connection:
                self.assign_connection()
    
            # cached connection exists, call send_data manually.
            elif self.count > 10:
                self.disconnect(self.connection)
            else:
                #you probably want to send data only if it it should be valid.
                self.send_data(self.connection)       
    
        def assign_connection(self):
        ''' Creates and stores a Deffered which represents the connection to
            the server. '''
            # cache the connection.
            self.connection = self.factory.login(
                                  credentials.UsernamePassword("test1","bb1b"))
            # add connection callbacks as normal.
            self.connection.addCallbacks(
                                  self.good_connected, self.bad_connected)
            self.connection.addCallback(self.send_data)
            self.connection.addErrback(self.disconnect)
    
        def disconnect(self, perspective):
            # be sure to cleanup after yourself!
            self.connection = False
            print 'disconnect'
            reactor.stop()
    
        # the rest of your class goes here.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking to learn about embedded programming (in C mainly, but I hope to
I'm trying to learn C. As a C# developer, my IDE is Visual Studio.
As I learn more and more about OOP, and start to implement various design
I am trying to learn the keyboard shortcuts in Visual Studio in order to
I'd like to learn how to program in Assembler. I've done a bit of
Whilst starting to learn lisp, I've come across the term tail-recursive . What does
I need to learn ADO.NET to build applications based on MS Office. I have
I'd love to learn Cocoa, it seems like the best systems language for Mac
I just started to learn Ruby and as a .Net developer, I'm wondering if
I am interested to learn: what are the most common technical causes (from the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.