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

  • Home
  • SEARCH
  • 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 954163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:10:07+00:00 2026-05-16T00:10:07+00:00

i’m trying to create a bridge program in twisted.web that receives data from a

  • 0

i’m trying to create a bridge program in twisted.web that receives data from a web server and sends it to another server, thus i’m using 2 getPage applications that i have wrapped in a class for convenience, the class contains all the callbacks and the client “routine”.. 1)auth 2)receive data 3)send data, all this is done in a cyclic way and works perfectly in both clients!!

What i am planning to do now is to integrate the two, this would mean that i would have to make some callbacks outside the classes in order to process them.

client1<—>main<—>client2

How can i do this?

i’m using twisted getPage

i’ll post one of the two classes

class ChatService():
    def __init__(self):
         self.myServID= self.generatemyServID()
         self.myServServer= "http://localhost"
         ## This is where the magic starts
             reactor.callWhenRunning(self.mainmyServ)
             reactor.run()

    def generatemyServID(self):
         a= ""
         for x in range(60):
            c= floor(random() * 61)
            a += "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"[int(c):int(c)+1]
         return a

    def sentMessage(self, data):
        print "Message was sent successfully"

    def sendMessage(self, mess):
        s1= DeferredList([client.getPage(self.myServServer+"/chat/", headers={'Content-Type': 'application/x-www-form-urlencoded'}, method="POST", postdata="action=chat&user=%s&message=%s" % (self.myServID, mess)),])
        s1.addCallback(self.sentMessage)
        s1.addErrback(self.errMessage)

    def recivedMessage(self,data):
        chat= loads(data[0][1])
        if chat['from'] != "JOINED" and chat['from'] != "TYPING" and chat['from'] != "Ben":
            print "%s says: %s" % (chat['from'], decode(chat['chat']))
            self.sendMessage("Hello")
        # Restart Loop
        self.loopChat()

    def errMessage(self,e):
            # print "An error occured receiving/sending the messages\n%s" % e
        print "Still no connectiions, waiting..."
        self.loopChat()

    def loopChat(self):
        s1= DeferredList([client.getPage(self.myServServer+"/chat/", headers={'Content-Type': 'application/x-www-form-urlencoded'}, method="POST", postdata="action=poll&user=%s&message=null" % self.myServID),])
        s1.addCallback(self.recivedMessage)
        s1.addErrback(self.errMessage)   

    def error(self,e):
        print "An error occured\n%s" % e

    def connectedtomyServService(self,data):
        if data[0][0] == False:
            print "Connection to myServ Service was impossible"
            reactor.stop()
            return
        if loads(data[0][1])['action'] == 'join':
            print "Connected to the server and joined chat"
            print "Started chat loop"
            self.loopChat()
        else:
            print "An Error Occured"
            return

    def mainmyServ(self):
            # print "Client ID is: " + self.myServID
        # Joining Chat
        s1= DeferredList([client.getPage(self.myServServer+"/chat/", headers={'Content-Type': 'application/x-www-form-urlencoded'}, method="POST", postdata="action=join&user=%s&message=null" % self.myServID),])
        s1.addCallback(self.connectedtomyServService)
        s1.addErrback(self.error)

How can I make callbacks outside the class?

I hope I managed to express myself =D

Thnaks a lot

  • 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-16T00:10:08+00:00Added an answer on May 16, 2026 at 12:10 am

    How can I make callbacks outside the class?

    This sounds like a very common misunderstanding. As a result of the misunderstanding, the question, as asked, doesn’t make a lot of sense. So let’s forget about the question.

    You already have some code that’s using Deferreds. Let’s start with mainmyServ:

    def mainmyServ(self):
            # print "Client ID is: " + self.myServID
        # Joining Chat
        s1= DeferredList([client.getPage(self.myServServer+"/chat/", headers={'Content-Type': 'application/x-www-form-urlencoded'}, method="POST", postdata="action=join&user=%s&message=null" % self.myServID),])
        s1.addCallback(self.connectedtomyServService)
        s1.addErrback(self.error)
    

    First, you can get rid of the DeferredList. Your list only has one Deferred in it, so the DeferredList isn’t adding any value. You’ll get practically the same behavior like this, and your callbacks can be simplified by removing all of the [0][0] expressions.

    def mainmyServ(self):
            # print "Client ID is: " + self.myServID
        # Joining Chat
        s1= client.getPage(self.myServServer+"/chat/", headers={'Content-Type': 'application/x-www-form-urlencoded'}, method="POST", postdata="action=join&user=%s&message=null" % self.myServID)
        s1.addCallback(self.connectedtomyServService)
        s1.addErrback(self.error)
    

    So this is a perfectly reasonable method which is calling a function that returns a Deferred and then adding a callback and an errback to that Deferred. Say you have another function, perhaps your overall main function:

    def main():
        service = ChatService()
        service.mainmyServ()
    

    What prevents the main function from adding more callbacks to the Deferred in mainmyServ? Only that mainmyServ doesn’t bother to return it. So:

    def mainmyServ(self):
            # print "Client ID is: " + self.myServID
        # Joining Chat
        s1= client.getPage(self.myServServer+"/chat/", headers={'Content-Type': 'application/x-www-form-urlencoded'}, method="POST", postdata="action=join&user=%s&message=null" % self.myServID)
        s1.addCallback(self.connectedtomyServService)
        s1.addErrback(self.error)
        return s1
    
    
    def main():
        service = ChatService()
        d = service.mainmyServ()
        d.addCallback(doSomethingElse)
    

    Nothing special there, it’s just another addCallback. All you were missing was a reference to the Deferred.

    You can set up your loop now, by having doSomethingElse call another method on ChatService. If that other method returns a Deferred, then doSomethingElse can add a callback to it that calls mainmyServ again. And so on. There’s your loop, controlled “outside” of the class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.