Simplify ws_echo.py code.

This commit is contained in:
Joel Martin 2010-03-31 18:20:48 -05:00
parent 033df68db1
commit feb4e5f801

View File

@ -26,7 +26,9 @@ def start_server(port):
handshake(csock, tick) handshake(csock, tick)
print 'handshaken' print 'handshaken'
while True: while True:
interact(csock, tick) data = csock.recv(255)
print 'got:%s' %(data)
csock.send("\x00 server response %d \xff" % (tick))
tick+=1 tick+=1
except Exception, e: except Exception, e:
print "Ignoring exception:", e print "Ignoring exception:", e
@ -37,15 +39,10 @@ def handshake(client, tick):
_, path, _ = req_lines[0].split(" ") _, path, _ = req_lines[0].split(" ")
_, origin = req_lines[4].split(" ") _, origin = req_lines[4].split(" ")
_, host = req_lines[3].split(" ") _, host = req_lines[3].split(" ")
print "*** got handshake:\n%s" % handshake #print "*** got handshake:\n%s" % handshake
print "*** origin: %s, location: ws://%s%s" % (origin, host, path) print "*** client origin: %s, location: ws://%s%s" % (origin, host, path)
client.send(server_handshake % (origin, host, path)) client.send(server_handshake % (origin, host, path))
def interact(client, tick):
data = client.recv(255)
print 'got:%s' %(data)
client.send("\x00 server response %d \xff" % (tick))
if __name__ == '__main__': if __name__ == '__main__':
if len(sys.argv) < 2: if len(sys.argv) < 2:
print "Usage: %s <port>" % sys.argv[0] print "Usage: %s <port>" % sys.argv[0]