Note that there are some explanatory texts on larger screens.

plurals
  1. POtwisted web getPage, 2 clients in 2 classes, manage events between the two
    text
    copied!<p>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!!</p> <p>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.</p> <p>client1&lt;--->main&lt;--->client2</p> <p>How can i do this?</p> <p>i'm using twisted getPage</p> <p>i'll post one of the two classes</p> <pre><code>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&amp;user=%s&amp;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&amp;user=%s&amp;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&amp;user=%s&amp;message=null" % self.myServID),]) s1.addCallback(self.connectedtomyServService) s1.addErrback(self.error) </code></pre> <p>How can I make callbacks outside the class?</p> <p>I hope I managed to express myself =D</p> <p>Thnaks a lot</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload