This is my client:
from websocket import create_connection
ws = create_connection("wss://127.0.0.1:8080",
sslopt={"cert_reqs": ssl.CERT_NONE, "check_hostname": False, "ssl_version": ssl.PROTOCOL_TLSv1})
data = json.dumps({"api_command":"sensor_data","session_id":session_id})
ws.send(data)
And this is my server:
if __name__ == '__main__':
txaio.start_logging(level='debug')
# SSL server context: load server key and certificate
# We use this for both WS and Web!
contextFactory = ssl.DefaultOpenSSLContextFactory('keys/server.key',
'keys/server.crt')
factory = WebSocketServerFactory(u"wss://127.0.0.1:9000")
# by default, allowedOrigins is "*" and will work fine out of the
# box, but we can do better and be more-explicit about what we
# allow. We are serving the Web content on 8080, but our WebSocket
# listener is on 9000 so the Origin sent by the browser will be
# from port 8080...
factory.setProtocolOptions(
allowedOrigins=[
"https://127.0.0.1:8080",
"https://localhost:8080",
]
)
factory.protocol = MyServerProtocol
listenWS(factory, contextFactory)
webdir = File(".")
webdir.contentTypes['.crt'] = 'application/x-x509-ca-cert'
web = Site(webdir)
reactor.listenSSL(8080, web, contextFactory)
#reactor.listenTCP(8080, web)
reactor.run()
When I run the client, the following error is thrown in the client
websocket._exceptions.WebSocketBadStatusException: Handshake status 200
While on the server:
2017-08-23T16:08:58+0300 WebSocketServerFactory (TLS) starting on 9000
2017-08-23T16:08:58+0300 Starting factory <autobahn.twisted.websocket.WebSocketServerFactory object at 0x04AADAF0>
2017-08-23T16:08:58+0300 Site (TLS) starting on 8080
2017-08-23T16:08:58+0300 Starting factory <twisted.web.server.Site object at 0x04ACD770>
2017-08-23T16:09:32+0300 "127.0.0.1" - - [23/Aug/2017:13:09:32 +0000] "GET / HTTP/1.1" 200 2042 "-" "-"
I don't understand what the problem is. My server is from this example https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/echo_tls. If I use a client like the one shown in that example, it connects just fine. I can't connect using the simple websocket library though.
The exception is thrown at create_connection
of the websocket client
instead of
ws = create_connection(
"wss://127.0.0.1:8080",
sslopt={"cert_reqs": ssl.CERT_NONE,
"check_hostname": False,
"ssl_version": ssl.PROTOCOL_TLSv1
}
)
use
ws = create_connection(
"ws://127.0.0.1:8080",
sslopt={"cert_reqs": ssl.CERT_NONE,
"check_hostname": False,
"ssl_version": ssl.PROTOCOL_TLSv1}
)
It should be 'ws' not 'wss'.
You can use websocket-server, if you need more help let me know