10

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.

Cam Jackson
  • 11,860
  • 8
  • 45
  • 78
  • 1
    Erm, too many answers to comment on, so this is to everyone who answered: Ah, I thought it might have something to do with the implicit self argument. However, with it telling me it wanted 2 args, I figured it meant host and port, and that self didn't get counted in that list. All is clear now. Upvotes for everyone yay! – Cam Jackson Aug 11 '11 at 07:39
  • One other thing: what do you think `class Foo():` does, as opposed to `class Foo:` or `class Foo(object):`? – pillmuncher Aug 11 '11 at 10:05
  • Oh, woops. That's not in my real application, I just forgot it here. I'll fix it up now. Why, does it do something different the way I had it in the question initially? – Cam Jackson Aug 11 '11 at 23:34
  • CamJackson makes a good point. This is a rather confusing error message to new programmers. Has it been suggested as a change to the way objects are implemented in Python? Maybe it's too complex to tackle. – Carl F. Aug 12 '11 at 02:02
  • It's not so much the way the objects work, it was the text of the exception that got me. I would have figured it out myself if the error had said `TypeError: connect() takes exactly 2 arguments: (self, address), 3 given. Had I realised self was counting towards the given number (2), then I might have realised that the remaining single argument needed to encapsulate both the host and the port, suggesting a tuple. – Cam Jackson Aug 12 '11 at 02:13
  • @CamJackson: `class Foo():` in Python 2.x is the same as `class Foo:`, the definition of an old style class. In Python 3.x there are no old style classes anymore, so all three versions have the same meaning. What I'm trying to point out is that the empty parentheses serve absolutely no purpose except to allow one to write code like `class Bar(*some_possibly_empty_list_of_classes):`. – pillmuncher Aug 12 '11 at 02:41

6 Answers6

12

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.

dave
  • 2,199
  • 1
  • 16
  • 34
9

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.

cdhowie
  • 158,093
  • 24
  • 286
  • 300
4
s.connect(("localhost", 2057))

The third (or first) argument you are implicitely passing is self (s).

Sockets take a tuple consisting of (HOST, PORT).

Jacob
  • 41,721
  • 6
  • 79
  • 81
4

The socket connect function is used to connect the socket to a remote address. For IP sockets, the address is a pair (host, port)

So you should use:

s.connect( ("localhost", 2057) )
winterTTr
  • 1,739
  • 1
  • 10
  • 30
3

use:

s.connect(("localhost", 2057))
HYRY
  • 94,853
  • 25
  • 187
  • 187
0

The socket.connect only accept 1 argument, that is address, 2 if self counted. And the format of address is stated on the fourth paragraph, http://docs.python.org/library/socket.html

Jason Lee
  • 3,200
  • 1
  • 34
  • 71