Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to properly trigger a python twisted transport?
    text
    copied!<p>I have been asked to write a class that connects to a server, asynchronously sends the server various commands, and then provides the returned data to the client. I've been asked to do this in Python, which is a new language to me. I started digging around and found the Twisted framework which offers some very nice abstractions (Protocol, ProtocolFactory, Reactor) that do a lot of the things that I would have to do if I would roll my own socket-based app. It seems like the right choice given the problem that I have to solve. </p> <p>I've looked through numerous examples on the web (mostly <a href="http://krondo.com/?page_id=1327" rel="nofollow" title="krondo">Krondo</a>), but I still haven't seen a good example of creating a client that will send multiple commands across the wire and I maintain the connection I create. The server (of which I have no control over), in this case, doesn't disconnect after it sends the response. So, what's the proper way to design the client so that I can tickle the server in various ways? </p> <p>Right now I do this: </p> <pre><code>class TestProtocol(Protocol) def connectionMade(self): self.transport.write(self.factory.message) class TestProtocolFactory(Factory): message = '' def setMessage(self, msg): self.message = msg def main(): f = TestProtocolFactory() f.setMessage("my message") reactor.connectTCP(...) reactor.run() </code></pre> <p>What I really want to do is call <code>self.transport.write(...)</code> via the reactor (really, call TestProtocolFactory::setMessage() on-demand from another thread of execution), not just when the connection is made. </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