[Twisted-Python] Interactive Twisted client (newbie question?)

Jon Blower jdb at mail.nerc-essc.ac.uk
Mon Jan 3 18:00:01 EST 2005

Hi all,

I'm new to Twisted, and I'm trying to create an implementation of a client
program that communicates over a network with a server using a protocol called
Styx.  (In case you're interested, Styx is a protocol for distributed systems,
as used by the Inferno operating system:
http://www.vitanuova.com/inferno/papers/styx.html.)  In short, Styx is a pretty
simple call-response type of protocol, in which the client sends a T-message
and waits for the corresponding R-message from the server.  There is a
one-to-one correspondence between T- and R-messages.

I understand that Twisted is an event-driven framework and I have written a
basic client program.  My initial attempt has involved creating a StyxProtocol
class that inherits from protocol.Protocol, in which I override key methods
such as dataReceived() and connectionMade().  Having created this protocol and
told it how to respond to messages that arrive, I created a simple Factory
class, then set the whole thing running with reactor.run().  It works fine, but
it's not quite in the form I'd like.  In my current attempt, what I've done is
essentially to create a load of callbacks and "press GO".  Once I've set the
program running I can't interact with it until one of the methods calls

What I'd like to be able to do is illustrated by the following pseudo-Python. 
This could be a script or could be typed into an interactive Python shell:

conn = StyxConnection("myserver.remotehost.com", 6666)
conn.connect() # Starts the reactor going and performs handshakes
# The connection is now open and I can interact with it
myRemoteFile = conn.openFile("/path/to/myfile") # Gets handle to file on remote
server, by exchange of a few messages
data = myRemoteFile.read() # Reads data from the remote file, again by exchange
of some messages
conn.close() # Closes the connection to the remote server and stops the reactor

How can I do this with the Twisted framework?  As far as I can tell,
reactor.run() will not return until something calls reactor.stop().  I could
achieve what I want using threads (I think), but I'd like to avoid this if

I'm sure I'm not the first to ask this sort of question, but I've looked through
the docs and example code and haven't been able to track down an answer.  I'd
be very grateful for any pointers that anyone could offer.


Dr Jon Blower              Tel: +44 118 378 5213 (direct line)
Technical Director         Tel: +44 118 378 8741 (ESSC)
Reading e-Science Centre   Fax: +44 118 378 6413
ESSC                       Email: jdb at mail.nerc-essc.ac.uk
University of Reading
3 Earley Gate
Reading RG6 6AL, UK

This message was sent using IMP, the Internet Messaging Program.

More information about the Twisted-Python mailing list