【问题标题】:Websocket handshake status 200 exceptionWebsocket握手状态200异常
【发布时间】:2017-08-23 13:16:49
【问题描述】:

这是我的客户:

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)

这是我的服务器:

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()

当我运行客户端时,客户端抛出以下错误

websocket._exceptions.WebSocketBadStatusException: Handshake status 200

在服务器上时:

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 "-" "-"

我不明白问题出在哪里。我的服务器来自这个例子https://github.com/crossbario/autobahn-python/tree/master/examples/twisted/websocket/echo_tls。如果我使用该示例中所示的客户端,它就可以正常连接。我无法使用简单的 websocket 库进行连接。

websocket客户端的create_connection抛出异常

【问题讨论】:

    标签: python ssl websocket autobahn


    【解决方案1】:

    而不是

    ws = create_connection(
        "wss://127.0.0.1:8080",
        sslopt={"cert_reqs": ssl.CERT_NONE, 
                "check_hostname": False, 
                "ssl_version": ssl.PROTOCOL_TLSv1
        }
    )
    

    使用

    ws = create_connection(
        "ws://127.0.0.1:8080",
        sslopt={"cert_reqs": ssl.CERT_NONE, 
                "check_hostname": False, 
                "ssl_version": ssl.PROTOCOL_TLSv1}
    )
    

    应该是 'ws' 而不是 'wss'。

    您可以使用websocket-server,如果您需要更多帮助,请告诉我

    【讨论】:

    • 这会抛出websocket._exceptions.WebSocketConnectionClosedException: Connection is already closed.
    • 你运行的是什么服务器。重启你的服务器
    猜你喜欢
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 2016-09-20
    • 1970-01-01
    相关资源
    最近更新 更多