Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websockets.connect() raises Exception after upgrading to version 10 #1046

Closed
sentrip opened this issue Sep 11, 2021 · 2 comments
Closed

websockets.connect() raises Exception after upgrading to version 10 #1046

sentrip opened this issue Sep 11, 2021 · 2 comments

Comments

@sentrip
Copy link

sentrip commented Sep 11, 2021

The following minimal code sample works with websockets==9.1, and raises an exception upon upgrading to websockets==10.
This is a reduced example of code that is currently running and working with websockets==9.1.

import websockets

async def main():
    await websockets.connect('wss://testnet.bitmex.com/realtime')

if __name__ == '__main__':
    import asyncio
    asyncio.get_event_loop().run_until_complete(main())

I have been able to reproduce this error in a clean virtual environment where the only change is using different versions of the library. I have tested this on Ubuntu 20.04.2 LTS with Python 3.8. The traceback of the error is as follows:

Traceback (most recent call last):
  File "main2.py", line 8, in <module>
    asyncio.get_event_loop().run_until_complete(main())
  File "/usr/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "main2.py", line 4, in main
    await websockets.connect('wss://testnet.bitmex.com/realtime')
  File "/home/jayjay/projects/pyct/venv/lib/python3.8/site-packages/websockets/legacy/client.py", line 649, in __await_impl_timeout__
    return await asyncio.wait_for(self.__await_impl__(), self.open_timeout)
  File "/usr/lib/python3.8/asyncio/tasks.py", line 494, in wait_for
    return fut.result()
  File "/home/jayjay/projects/pyct/venv/lib/python3.8/site-packages/websockets/legacy/client.py", line 660, in __await_impl__
    await protocol.handshake(
  File "/home/jayjay/projects/pyct/venv/lib/python3.8/site-packages/websockets/legacy/client.py", line 331, in handshake
    self.extensions = self.process_extensions(
  File "/home/jayjay/projects/pyct/venv/lib/python3.8/site-packages/websockets/legacy/client.py", line 220, in process_extensions
    raise NegotiationError(
websockets.exceptions.NegotiationError: Unsupported extension: name = permessage-deflate, params = [('client_no_context_takeover', None)]
@aaugustin
Copy link
Member

The server you're connecting to doesn't implement https://www.rfc-editor.org/rfc/rfc7692.html#section-7.1.2.1 correctly.

Specifically, it doesn't comply with:

A server accepts an extension negotiation offer with this parameter
by including the "server_max_window_bits" extension parameter in the
extension negotiation response to send back to the client with the
same or smaller value as the offer.

You can work around this issue by adding compression=None or extensions=[ClientPerMessageDeflateFactory()] to the connect() call.

(I briefly believed this was a bug in websockets, which is why I investigated despite the cryptocurrency policy, but I wasted my time 🤷)

@Avon619
Copy link

Avon619 commented Sep 17, 2021

#!/usr/bin/env python

import asyncio
import websockets

async def hello():
async with websockets.connect("ws://localhost:8765") as websocket:
await websocket.send("Hello world!")
await websocket.recv()

asyncio.run(hello())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants