Mierdin Mierdin - 1 year ago 233
Python Question

TypeError: Connect() takes exactly one argument

I'm creating an extremely simple python script designed to open a TCP socket and send some JSON data. (I'm new to python)

I'm recieving an error below when I run the script:

Traceback (most recent call last):
File "JSONTest.py", line 17, in <module>
s.connect('', 6634)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
TypeError: connect() takes exactly one argument (2 given)

My script is below:

import socket
import json
import time

data = "{\"method\": \"echo\",\"id\": \"echo\",\"params\": []}"

#Create socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#Establish TCP session via IP address and port specified
s.connect('', 6634)

#Send JSON to socket

#Wait for response and print to console
result = json.loads(s.recv(1024))
#print str(result)


I've checked the documentation on the socket library and connect() lists only a single argument, namely the destination IP address desired. So, my question is, how can I do the above, where I'm also specifying my TCP port, if the connect() method won't allow me to input it there?

I'm also open to input on better ways to accomplish this.

Answer Source

Host and port must be a tuple.

s.connect(('', 6634))

See the second part of the example in the Python Socket documentation here.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download