d0608a63b6
websocket.py: * With echo.py, which doesn't need any server configuration, we can just switch over our application class to inherit from WebSocketRequestHandler instead of WebSocketServer. Also, need to use the new method name new_websocket_client. * With load.py, since we have the "delay" configuration, we need both a server class and a request handler. Note that for both tests, I've removed the raising of self.EClose(closed). This is incorrect. First of all, it's described as "An exception before the WebSocket connection was established", so not suitable for our case. Second, it will cause send_close to be called twice. Finally, self.EClose is now in the WebSocketServer class, and not a member of the request handler.
75 lines
2.3 KiB
Python
Executable File
75 lines
2.3 KiB
Python
Executable File
#!/usr/bin/env python
|
|
|
|
'''
|
|
A WebSocket server that echos back whatever it receives from the client.
|
|
Copyright 2010 Joel Martin
|
|
Licensed under LGPL version 3 (see docs/LICENSE.LGPL-3)
|
|
|
|
You can make a cert/key with openssl using:
|
|
openssl req -new -x509 -days 365 -nodes -out self.pem -keyout self.pem
|
|
as taken from http://docs.python.org/dev/library/ssl.html#certificates
|
|
'''
|
|
|
|
import os, sys, select, optparse
|
|
sys.path.insert(0,os.path.dirname(__file__) + "/../websockify")
|
|
from websocket import WebSocketServer, WebSocketRequestHandler
|
|
|
|
class WebSocketEcho(WebSocketRequestHandler):
|
|
"""
|
|
WebSockets server that echos back whatever is received from the
|
|
client. """
|
|
buffer_size = 8096
|
|
|
|
def new_websocket_client(self):
|
|
"""
|
|
Echo back whatever is received.
|
|
"""
|
|
|
|
cqueue = []
|
|
c_pend = 0
|
|
cpartial = ""
|
|
rlist = [self.request]
|
|
|
|
while True:
|
|
wlist = []
|
|
|
|
if cqueue or c_pend: wlist.append(self.request)
|
|
ins, outs, excepts = select.select(rlist, wlist, [], 1)
|
|
if excepts: raise Exception("Socket exception")
|
|
|
|
if self.request in outs:
|
|
# Send queued target data to the client
|
|
c_pend = self.send_frames(cqueue)
|
|
cqueue = []
|
|
|
|
if self.request in ins:
|
|
# Receive client data, decode it, and send it back
|
|
frames, closed = self.recv_frames()
|
|
cqueue.extend(frames)
|
|
|
|
if closed:
|
|
self.send_close()
|
|
|
|
if __name__ == '__main__':
|
|
parser = optparse.OptionParser(usage="%prog [options] listen_port")
|
|
parser.add_option("--verbose", "-v", action="store_true",
|
|
help="verbose messages and per frame traffic")
|
|
parser.add_option("--cert", default="self.pem",
|
|
help="SSL certificate file")
|
|
parser.add_option("--key", default=None,
|
|
help="SSL key file (if separate from cert)")
|
|
parser.add_option("--ssl-only", action="store_true",
|
|
help="disallow non-encrypted connections")
|
|
(opts, args) = parser.parse_args()
|
|
|
|
try:
|
|
if len(args) != 1: raise
|
|
opts.listen_port = int(args[0])
|
|
except:
|
|
parser.error("Invalid arguments")
|
|
|
|
opts.web = "."
|
|
server = WebSocketServer(WebSocketEcho, **opts.__dict__)
|
|
server.start_server()
|
|
|