2

I need to connect from python client to a tornado server using an url like ws://localhost:8006/user?id=666.

I have tried something like this:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect('ws://localhost:8006/user?id=666')
print s.recv(1024)
s.close

...and got the following error:

TypeError: getsockaddrarg: AF_INET address must be tuple, not str

Thanks

Anto
  • 5,900
  • 7
  • 37
  • 60

2 Answers2

1

You don't connect to a remote socket with a namespace and parameters. You connect with a host and port. Period. Python's socket module is a thin wrapper around native sockets. It doesn't know anything about protocols like http, or ws.

You can connect to the host and port that your web socket uses. Then you can send any parameters you like...but it's not going to understand them, unless you use the websocket protocol.

Your best bet is to use a websocket client that someone else has written (see some answers here)

If you want to write it yourself, here is a minimal example.

Community
  • 1
  • 1
Gerrat
  • 25,859
  • 8
  • 64
  • 94
0

You must convert string to tuple. Pratic example:

x = "(1,2,3)"
t = tuple(int(v) for v in re.findall("[0-9]+", x))
Black_Ram
  • 313
  • 4
  • 9
  • 17