Commit dbe4eecf authored by Taddeüs Kroes's avatar Taddeüs Kroes

Improved Server contructor arguments

parent c140e422
...@@ -31,8 +31,8 @@ class Server(object): ...@@ -31,8 +31,8 @@ class Server(object):
>>> EchoServer(('', 8000)).run() >>> EchoServer(('', 8000)).run()
""" """
def __init__(self, address, loglevel=logging.INFO, protocols=[], def __init__(self, address, loglevel=logging.INFO, ssl_args=None,
secure=False, max_join_time=2.0, **kwargs): max_join_time=2.0, **kwargs):
""" """
Constructor for a simple web socket server. Constructor for a simple web socket server.
...@@ -45,10 +45,11 @@ class Server(object): ...@@ -45,10 +45,11 @@ class Server(object):
`protocols` and `extensions` are passed directly to the websocket `protocols` and `extensions` are passed directly to the websocket
constructor. constructor.
`secure` is a flag indicating whether the server is SSL enabled. In `ssl_args` is a dictionary with arguments for `websocket.enable_ssl`
this case, `keyfile` and `certfile` must be specified. Any additional (and thus to ssl.wrap_socket). If omitted, the server is not
keyword arguments are passed to `websocket.enable_ssl` (and thus to SSL-enabled. If specified, at least the dictionary keys "keyfile" and
`ssl.wrap_socket`). "certfile" must be present because these are required arguments for
`websocket.enable_ssl` for a server socket.
`max_join_time` is the maximum time (in seconds) to wait for client `max_join_time` is the maximum time (in seconds) to wait for client
responses after sending CLOSE frames, it defaults to 2 seconds. responses after sending CLOSE frames, it defaults to 2 seconds.
...@@ -61,11 +62,11 @@ class Server(object): ...@@ -61,11 +62,11 @@ class Server(object):
hostname, port = address hostname, port = address
logging.info('Starting server at %s://%s:%d', scheme, hostname, port) logging.info('Starting server at %s://%s:%d', scheme, hostname, port)
self.sock = websocket(protocols=protocols, extensions=extensions) self.sock = websocket(**kwargs)
self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if secure: if ssl_args:
self.sock.enable_ssl(server_side=True, **kwargs) self.sock.enable_ssl(server_side=True, **ssl_args)
self.sock.bind(address) self.sock.bind(address)
self.sock.listen(5) self.sock.listen(5)
......
...@@ -16,6 +16,6 @@ class EchoServer(Server): ...@@ -16,6 +16,6 @@ class EchoServer(Server):
if __name__ == '__main__': if __name__ == '__main__':
EchoServer(8000, 'localhost', EchoServer(('localhost', 8000),
#secure=True, keyfile='cert.pem', certfile='cert.pem', #ssl_args=dict(keyfile='cert.pem', certfile='cert.pem'),
loglevel=logging.DEBUG).run() loglevel=logging.DEBUG).run()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment