I'm currently using socketserver to build a simple XMLSocket (an XML
based protocol used for communication between flash and the outside
world) server. I've got flash establishing a connection, sending a
request and my python server responding. However at this point
socketserver terminates the connection. Which is bad, since i need a
persistent connection so i can push data from the server to the client
without the overhead of polling.
I need to create TCP connection that persists until I explicitly tell
it to terminate, or the connection is terminated on the other end. A
quick look through the socketserver code makes this seem impossible
since it appears to call close after the handle() method.
Is there an even lower level library to be using, or is there some
option I haven't found yet in socketserver.
Cheers
If you don't want the connection to close, then don't let the request
complete. SocketServer implements logic for single request/response
per connection. You can change this by making your requests take a
really long time (until you're done with the connection) or you can
override the behavior which closes the connection after a response.
Or you could use the socket module, on which the SocketServer module is
based. Or you could use Twisted, another higher-level package built on
the socket module (mostly). Actually, I recommend Twisted, since it will
mostly isolate you from boring low-level details and let you implement
whatever high-level behavior you're after (I know that a bunch of people
have used it to communicate with Flash, for example).
Jean-Paul
Cheers mate I had a look into twisted but was put off by the FAQ
stating 1.0+ modules may or may not be stable, and only the 'core' is.
I don't wanna be messing around with a potentially buggy server, so im
gonna roll my own using the sockets module.
Thanks for your help !
Cheers mate I had a look into twisted but was put off by the FAQ
stating 1.0+ modules may or may not be stable, and only the 'core' is.
I don't wanna be messing around with a potentially buggy server, so im
gonna roll my own using the sockets module.
Hi,
Jean-Paul has something like this in mind (I think):
class Foobar(BaseRequestHandler):
def handle(self):
self.data = None
while self.data != 'QUIT':
self.data = self.request.recv(1024).strip().upper()
if self.data == '':
sleep(1)
else:
self.request.send(self.data)
This will keep a persistent connection, only closing on 'quit' being
received. I'm sure it's not the best way, but it certainly works.
Cheers,
-Blake
For what it's worth, that FAQ entry was grossly out of date. I just
deleted it and replaced it with an entry which says the exact opposite.
Jean-Paul