Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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('10.12.0.30', 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:

#Imports
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('10.12.0.30', 6634)

#Send JSON to socket
s.send(json.dumps(data))

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

#Exit
s.close()

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.

like image 945
Mierdin Avatar asked Oct 02 '13 17:10

Mierdin


People also ask

How many arguments does on_connect() take?

Learn more TypeError: on_connect() takes exactly 3 arguments (4 given) Ask Question Asked4 years, 1 month ago Active2 years, 1 month ago Viewed6k times

Why does Python take 1 positional argument but 2 were given?

The Python "TypeError: takes 1 positional argument but 2 were given" occurs for multiple reasons: Forgetting to specify the self argument in a class method. Forgetting to specify a second argument in a function's definition. Passing two arguments to a function that only takes one. Overriding a built-in function by mistake.

Why does list append take exactly one argument in Python?

The Python "TypeError: list.append () takes exactly one argument (2 given)" occurs when we pass multiple arguments to the list.append method. To solve the error, either pass a list containing the arguments to the append method, or use the extend () method. Here is an example of how the error occurs.

Why does Python pass implicit self arguments when calling a method?

Even though these functions are not originally defined as part of your class, when you call them as if they are methods of your class instance Python will pass the implicit selfargument just as if they have been part of your class all along. To fix, just add the selfargument.


2 Answers

Host and port must be a tuple.

s.connect(('10.12.0.30', 6634))

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

like image 189
RyPeck Avatar answered Sep 21 '22 00:09

RyPeck


You must call connect with one argument: a tuple with the host and the port:

s.connect(('10.12.0.30',6677))
like image 28
Nacib Neme Avatar answered Sep 21 '22 00:09

Nacib Neme