[Python] Websocket secured server in Python using SimpleWebSocketServer module

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to implement a websocket secure server in Python (based on this module)
    In the link, it says that the module supports TLS and SSL connections, but doesn't explain how.

    The code I currently have goes like that:

    from SimpleWebSocketServer import SimpleWebSocketServer, WebSocket
    import ssl

    class SimpleResponder(WebSocket):

    def handleMessage(self):
    self.sendMessage(self.data)

    def handleConnected(self):
    print self.address, 'connected'

    def handleClose(self):
    print self.address, 'closed'

    server = SimpleWebSocketServer('', 8000, SimpleResponder)
    try:
    server.serveforever()
    except KeyboardInterrupt:
    pass
    print "Server ended"


    I tried to implement SSL wrapping in the same way I would do with a HTTP server (like that):

    server.socket = ssl.wrap_socket (server.socket, certfile='path/to/localhost.pem', server_side=True)


    But seems like it doesn't work (I get the next error):

    AttributeError: 'SimpleWebSocketServer' object has no attribute 'socket'


    So clearly the SimpleWebSocketServer class isn't structured like the SimpleHTTPServer class.

    So how can I implement a secure websocket server?

    Continue reading...

Compartilhe esta Página