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

TypeError: can't concat int to bytes #574

Closed
jdavid opened this issue May 9, 2024 · 3 comments
Closed

TypeError: can't concat int to bytes #574

jdavid opened this issue May 9, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@jdavid
Copy link

jdavid commented May 9, 2024

I got this error:

  File "/usr/local/lib/python3.9/dist-packages/websockify/websocket.py", line 560, in close
    self.shutdown(socket.SHUT_RDWR, code, reason)
  File "/usr/local/lib/python3.9/dist-packages/websockify/websocket.py", line 547, in shutdown
    self._sendmsg(0x8, msg)
  File "/usr/local/lib/python3.9/dist-packages/websockify/websocket.py", line 734, in _sendmsg
    mask += random.randrange(256)
TypeError: can't concat int to bytes

https://github.com/novnc/websockify/blob/master/websockify/websocket.py#L732-L734

            mask = b''
            for i in range(4):
                mask += random.randrange(256)

Here it tries to concatenate an integer to a byte string, what raises a type error.

@CendioOssman
Copy link
Member

Thanks for your report. I'm afraid the client code is rarely used, so it is likely to have more bugs.

Does this fix work:

diff --git a/websockify/websocket.py b/websockify/websocket.py
index dae93b4..af87d3e 100644
--- a/websockify/websocket.py
+++ b/websockify/websocket.py
@@ -731,7 +731,7 @@ class WebSocket(object):
         if self.client:
             mask = b''
             for i in range(4):
-                mask += random.randrange(256)
+                mask += random.randrange(256).to_bytes()
             frame = self._encode_hybi(opcode, msg, mask)
         else:
             frame = self._encode_hybi(opcode, msg)

@CendioOssman CendioOssman added the bug Something isn't working label May 16, 2024
@jdavid
Copy link
Author

jdavid commented May 19, 2024

It should do, but we've moved to websocket-client

@CendioOssman
Copy link
Member

Okay. I've pushed the above fix as a2362da. Hopefully it is sufficient to fix the client side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants