Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python thinks I'm passing more arguments than I am?

Trying to set up some basic socket code in Python (well, Jython, but I don't think that's relevant here).

import socket
class Foo(object):
    def __init__(self):
        #some other init code here

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect("localhost", 2057)
        s.send("Testing 1,2,3...")
        data = s.recv()
        s.close()
        print data

It tells me:

    s.connect("localhost", 2057)
  File "<string>", line 1, in connect
TypeError: connect() takes exactly 2 arguments (3 given)

I get the feeling something really simple is staring me in the face, but I can't tell what I'm doing wrong.

like image 296
Cam Jackson Avatar asked Aug 11 '11 07:08

Cam Jackson


2 Answers

You have to pass a Tuple to connect() method.

s.connect( ('localhost', 2057) )

The first (implicit) argument expected is self, the second is the Tuple.

like image 116
dave Avatar answered Oct 08 '22 04:10

dave


You are passing three arguments! s is being passed as the implicit first argument, and the other two arguments you have specified are the second and third arguments.

Now, the reason that it's upset is because socket.connect() only takes one argument (two, of course, if you count the implicit instance argument): see the docs.

like image 38
cdhowie Avatar answered Oct 08 '22 05:10

cdhowie