[Twisted-Python] Simple bidirectional Socket Client
Hi there, i'm new to twisted, but i want to use it in a project for dealing with the socket connection to a server for receiving and sending data. I already did some projects in Delphi and there you have an build-in eventdriven socket class. Python itself lacks at this feature (afaik) and so i stumpled upon twisted. Since its only a small part of my task, im looking for an easy solution but after consulting google for two days and testing around im still failing at get it working. What i got so far: class ControllerProtocol(Protocol): def connectionMade(self): # send Greeting self.factory.isConnected = True self.transport.write("hello, world!") def dataReceived(self, data): "Parse received data and send corresponding Message." self.transport.write(self.factory.reactOnReceive(data)) def connectionLost(self, reason): #try to reconnect self.factory.isConnected = False print "connection lost" def sendMessage(self, data): print 'Send:', data self.transport.write(data) class Communicator(ClientFactory): protocol = ControllerProtocol def __init__(self, Host, Port): if isinstance(Host, str) and isinstance(Port, int) : self.myHost = Host self.myPort = Port self.isConnected = False print 'Going To connect' self.connectToMother() def connectToMotherShip(self): reactor.connectTCP(self.myHost, self.myPort, self) reactor.run() def buildProtocol(self, addr): p = ClientFactory.buildProtocol(self, addr) self.connectedProtocol = p return p def sendMessage(self, Message): self.connectedProtocol.SendMessage(Message) def reactOnReceive(self, String): #here i parse the received string for different requests after converting it to json and update values of the operator pass class Operator: def __init__(self): self.myCommunicator = Communicator("192.168.1.12", 815) do other stuff ... I want an Operator Object that builds the Communicator. The communicator updates values of the Operator asynchronously if it receives messages. Each 10 secs the Operator will initiate a Communicator.sendMessage to send a status information to the server. Two questions: 1) Is it possible to solve my task like this with twisted and if so, how do I get my Communicator to not block my Operator? 2) If twisted is not the easiest way for this approach, do i have alternatives? I want at least something event driven. I also read about the asyncore packages, but people often recommend twisted over asyncore. I simply want to send and receive message with a "threaded" communicator class, while doing other stuff in my Operator. Thanks a lot for your help, Toni
On Fri, Jun 14, 2013 at 02:11:20PM +0200, Raketenschnitzel wrote: ...
class Communicator(ClientFactory): protocol = ControllerProtocol
def __init__(self, Host, Port): if isinstance(Host, str) and isinstance(Port, int) : self.myHost = Host self.myPort = Port self.isConnected = False print 'Going To connect' self.connectToMother()
def connectToMotherShip(self): reactor.connectTCP(self.myHost, self.myPort, self) reactor.run()
...
class Operator: def __init__(self): self.myCommunicator = Communicator("192.168.1.12", 815) do other stuff ...
I want an Operator Object that builds the Communicator. The communicator updates values of the Operator asynchronously if it receives messages. Each 10 secs the Operator will initiate a Communicator.sendMessage to send a status information to the server.
Two questions: 1) Is it possible to solve my task like this with twisted and if so, how do I get my Communicator to not block my Operator?
The operator is blocked because reactor.run() is a loop, do not call it inside the factory constructor. class Communicator(ClientFactory): isConnected = False def __init__(self): pass class Operator: def __init__(self): # do other stuff but do not use self.myCommunicator yet factory = Communicator() operator = Operator() operator.myCommunicator = factory reactor.connectTCP(host, port, factory) reactor.run()
2) If twisted is not the easiest way for this approach, do i have alternatives? I want at least something event driven. I also read about the asyncore packages, but people often recommend twisted over asyncore.
I simply want to send and receive message with a "threaded" communicator class, while doing other stuff in my Operator.
The great advantage of twisted is that you do not need to use threads to communicate to the external world, use it only if you have to run code that could "block" the main loop. But in my modest opinion, twisted is the easiest way to do network programming. Ciao m.
participants (2)
-
Marco Giusti
-
Raketenschnitzel