4

I'm using the websockets module, but it doesn't support connecting though my corporate proxy for a client connection:

>>> import asyncio, websockets
>>> async def connect(uri):
...     async with websockets.connect(uri) as websocket:
...         pass
...
>>> asyncio.get_event_loop().run_until_complete(connect('ws://myhost.com/path/'))
    ....
ConnectionRefusedError: [Errno 10061] Connect call failed ('myhost.com', 80)

However if use curl with my http_proxy env var set, it works:

$ curl -i -N -H "Connection: Upgrade" -H "Upgrade: websocket" -H "Sec-WebSocket-Version: 13" -H "Sec-WebSocket-Key: MTIzNDEyMzQxMjM0MTIzNA==" http://myhost.com/path/
HTTP/1.1 101 Switching Protocols
Server: nginx/1.13.6
Date: Fri, 10 Nov 2017 14:51:00 GMT
Upgrade: websocket
Sec-WebSocket-Accept: s+CT5bkW5F3N2/5JUXrCPtLHn68=
Connection: Upgrade

What are my best options? Some other module for making client-websocket connects?

Jonas Byström
  • 25,316
  • 23
  • 100
  • 147
  • 1
    `websockets` don't support client connection through proxy. https://websockets.readthedocs.io/en/stable/limitations.html So use https://pypi.python.org/pypi/websocket-client – Ahsan Jul 25 '19 at 11:13
  • @Ahsan: You're a couple of years late, see my answer to my own question below. – Jonas Byström Jul 30 '19 at 10:06
  • 3
    Yeah, I saw your answer but didn't find anywhere why you left `websockets` and moved to `websocket-client`. Actually I was in situation to choose one between these two. So proxy is limitation for `websockets` – Ahsan Jul 30 '19 at 13:26

1 Answers1

5

Use pip install websocket-client. Then place your http*_proxy variables in os.environ as you normally do. Code like so:

ws = websocket.create_connection('ws://example.com/path')
ws.send(out_data)
in_data = ws.recv()

If you don't know exactly what protocols your proxy uses, set up both http*_proxy variables like this example:

$ export http_proxy=http://jonasb:password@proxyserver:8000
$ export https_proxy=https://jonasb:password@proxyserver:8000

(assuming your username is jonasb, and port is 8000).

Jonas Byström
  • 25,316
  • 23
  • 100
  • 147
  • Bystrom, Do i need to use `https_proxy` or `http?_proxy`? I'm having same proxy for http, https and ftp. Can you please help? – shaik moeed Feb 07 '19 at 10:08
  • I'm getting this error : `websocket._exceptions.WebSocketProxyException: failed CONNECT via proxy status: 503`. Can you help with this? Do i need to ask in new question? – shaik moeed Feb 08 '19 at 05:33
  • [This](https://superuser.com/questions/124897/503-service-unavailable-what-really-it-means) or [this](https://askubuntu.com/questions/26528/error-503-service-unavailable-when-using-apt-get-update-behind-proxy) perhaps? I remember getting 503 a long time ago, but unfortunately cannot recall the cause. Check your hostname and port. Drop username+password. Ask your admin. Poke around. – Jonas Byström Feb 09 '19 at 11:29
  • Thanks for your response. If possible to look at my question at which i explained in detail. This is the [link](https://stackoverflow.com/questions/54586749/websocket-exceptions-websocketproxyexception-failed-connect-via-proxy-status) – shaik moeed Feb 09 '19 at 11:48
  • @JonasByström what about socks/tor based proxies ? – user2284570 May 22 '22 at 10:11