简体   繁体   中英

Python websocket-client WebSocketApp proxy not connecting?

I'm trying to use proxies with a python WebSocketApp included in the websocket module. However, whenever I use this code,

ws=websocket.WebSocketApp('ws://echo.websocket.org:443')
ws.run_forever(http_proxy_host='my proxy here',http_proxy_port=80, # 80 is the proxy port
    on_close=on_close) 

I get this in the console, and then the socket closes

Connecting proxy...
--- request header ---
CONNECT echo.websocket.org:443 HTTP/1.0




-----------------------
--- response header ---
## SOCKET CLOSED ##

Help please?

If you want a secure connection use a scheme of wss instead of ws in the URI. The default port is 443, so specifying the port is optional. Try this:

websocket.enableTrace(True)
ws=websocket.WebSocketApp('wss://echo.websocket.org')    # N.B. use secure socket
ws.run_forever(http_proxy_host='my proxy here', http_proxy_port=80, on_close=on_close)

The reason that it was failing is that the remote server is closing the connection because you were connecting on a secure port on the server, but not using a secure connection on the client.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM