Simpler non-threaded ws_echo.py
This commit is contained in:
parent
5505589604
commit
033df68db1
93
ws_echo.py
93
ws_echo.py
|
@ -1,8 +1,6 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# File: asynchat-example-1.py
|
|
||||||
|
|
||||||
import asyncore, asynchat
|
import sys, os, socket
|
||||||
import sys, os, socket, string
|
|
||||||
|
|
||||||
server_handshake = """HTTP/1.1 101 Web Socket Protocol Handshake\r
|
server_handshake = """HTTP/1.1 101 Web Socket Protocol Handshake\r
|
||||||
Upgrade: WebSocket\r
|
Upgrade: WebSocket\r
|
||||||
|
@ -13,66 +11,45 @@ WebSocket-Protocol: sample\r
|
||||||
\r
|
\r
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class WSChannel(asynchat.async_chat):
|
def start_server(port):
|
||||||
|
tick = 0
|
||||||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||||
|
sock.bind(('', port))
|
||||||
|
sock.listen(100)
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
print 'listening on port %s' % port
|
||||||
|
csock, address = sock.accept()
|
||||||
|
tick+=1
|
||||||
|
print 'Got connection'
|
||||||
|
handshake(csock, tick)
|
||||||
|
print 'handshaken'
|
||||||
|
while True:
|
||||||
|
interact(csock, tick)
|
||||||
|
tick+=1
|
||||||
|
except Exception, e:
|
||||||
|
print "Ignoring exception:", e
|
||||||
|
|
||||||
def __init__(self, server, sock, addr):
|
def handshake(client, tick):
|
||||||
print ">> WSChannel.__init__"
|
handshake = client.recv(255)
|
||||||
asynchat.async_chat.__init__(self, sock)
|
req_lines = handshake.split("\r\n")
|
||||||
self.set_terminator("\r\n\r\n")
|
_, path, _ = req_lines[0].split(" ")
|
||||||
self.handshake = None
|
_, origin = req_lines[4].split(" ")
|
||||||
self.data = ""
|
_, host = req_lines[3].split(" ")
|
||||||
self.shutdown = 0
|
print "*** got handshake:\n%s" % handshake
|
||||||
|
print "*** origin: %s, location: ws://%s%s" % (origin, host, path)
|
||||||
|
client.send(server_handshake % (origin, host, path))
|
||||||
|
|
||||||
def collect_incoming_data(self, data):
|
def interact(client, tick):
|
||||||
#print ">> WSChannel.collect_incoming_data"
|
data = client.recv(255)
|
||||||
self.data = self.data + data
|
print 'got:%s' %(data)
|
||||||
|
client.send("\x00 server response %d \xff" % (tick))
|
||||||
def found_terminator(self):
|
|
||||||
#print ">> WSChannel.found_terminator"
|
|
||||||
if not self.handshake:
|
|
||||||
# got the client handshake lines
|
|
||||||
self.handshake = self.data
|
|
||||||
req_lines = self.handshake.split("\r\n")
|
|
||||||
_, path, _ = req_lines[0].split(" ")
|
|
||||||
_, origin = req_lines[4].split(" ")
|
|
||||||
_, host = req_lines[3].split(" ")
|
|
||||||
print "*** got handshake:\n%s" % self.handshake
|
|
||||||
print "*** origin: %s, location: ws://%s%s" % (origin, host, path)
|
|
||||||
self.push(server_handshake % (origin, host, path))
|
|
||||||
# self.push("HTTP/1.1 101 Web Socket Protocol Handshake\r\n")
|
|
||||||
# self.push("Upgrade: WebSocket\r\n")
|
|
||||||
# self.push("Connection: Upgrade\r\n")
|
|
||||||
# self.push("WebSocket-Origin: %s\r\n" % origin)
|
|
||||||
# self.push("WebSocket-Location: ws://%s%s\r\n" % (host, path))
|
|
||||||
# self.push("WebSocket-Protocol: sample\r\n")
|
|
||||||
# self.push("\r\n")
|
|
||||||
self.set_terminator("\xff") # look for frame terminators
|
|
||||||
else:
|
|
||||||
# return payload.
|
|
||||||
print "received: %s" % self.data
|
|
||||||
self.push("\x00 client sent: %s \xff" % self.data)
|
|
||||||
|
|
||||||
self.data = ""
|
|
||||||
|
|
||||||
class WSServer(asyncore.dispatcher):
|
|
||||||
|
|
||||||
def __init__(self, port):
|
|
||||||
asyncore.dispatcher.__init__(self)
|
|
||||||
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
self.bind(("", port))
|
|
||||||
self.listen(5)
|
|
||||||
print "<< WSServer.__init__"
|
|
||||||
|
|
||||||
def handle_accept(self):
|
|
||||||
print ">> WSServer.handle_accept"
|
|
||||||
conn, addr = self.accept()
|
|
||||||
WSChannel(self, conn, addr)
|
|
||||||
|
|
||||||
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]
|
||||||
sys.exit(2)
|
sys.exit(2)
|
||||||
PORT = int(sys.argv[1])
|
PORT = int(sys.argv[1])
|
||||||
s = WSServer(PORT)
|
start_server(PORT)
|
||||||
print "serving Web Socket at port", PORT, "..."
|
|
||||||
asyncore.loop()
|
|
||||||
|
|
Loading…
Reference in New Issue