Hi!
I need a fast protocol to use between a client and a server, both
sides written i Python.
What the protocol has to accomplish is extremely simple; the client
sends a number of lines (actually a RDF) and the server accepts or
rejects the packet. That's all !
Now, presently I'm using Twisted and XMLRPC ( why so is a long
history which I will not go into here) and that is a bit to slow for
my needs. On my present setup it takes close to 200 ms to perform one
transfer. But since that includes setting up and tearing down the
connection I thought I'd try doing something simpler so I wrote a
server and a client using socket.
The client sends a number of lines (each ending with \n) and ends one
set of lines with a empty line.
When the client sends a line with only a "." it means "I'm done close
the connection".
Letting the client open the connection and sending a number of sets I
was surprised to find that the performance was equal to what Twisted/
XMLRPC did. Around 200 ms per set, not significantly less.
So what can be done to speed up things or is Python incapable of
supporting fast networking (just a teaser).
-- Roland
#!/usr/bin/env python
from SocketServer import ThreadingMixIn, TCPServer, StreamRequestHandler
class UdsRequestHandler( StreamRequestHandler ):
def handle(self):
rdf = []
while 1:
tri = self.rfile.readline().strip()
if tri == ".":
if rdf:
print rdf
self.wfile.write("ACCEPT")
break
if tri:
rdf.append(tri)
else:
print rdf
self.wfile.write("ACCEPT")
rdf = []
class UdsServer(ThreadingMixIn, TCPServer):
allow_reuse_address = 1
if __name__ == "__main__":
serveraddr = ('', 8765)
srv = UdsServer(serveraddr, UdsRequestHandler)
srv.serve_forever()
#!/usr/bin/env python
import socket
# Ending \n vital !!
RDF = """<uds:person> <uds:src> "primula" .
<uds:person> <uds:eid> "4182" .
<uds:person> <uds:oid> "NIN:19720412-7802" .
<uds:person> <uds:add> _:a .
_:a <uds:guise> "anst1" .
_:a <udsattr:umuEduPersonEmploymentExtent> "100" .
_:a <udsattr:norEduOrgUnitUniqueNumber> "7528" .
_:a <udsattr:eduPersonAffiliation> "staff" .
"""
class UdsClient:
def __init__(self, ip, port):
self.ip = ip
self.port = port
self.s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
try:
self.s.connect((self.ip, self.port))
except:
print "Connection error"
return None
def send( self, rdf ):
self.s.send( rdf )
self.s.send( "\n" )
def close( self ):
self.s.send( ".\n" )
def read( self ):
r = ""
try:
r = self.s.recv(256)
except:
print "No response"
return r
if __name__ == "__main__":
uc = UdsClient("localhost", 8765)
n = 10
while n:
uc.send( RDF )
print "Response: %s" % uc.read()
n -= 1
uc.close()
print "DONE"
--
http://mail.python.org/mailman/listinfo/python-list