Python 认为我传递的参数比我实际传递的参数多?

发布于 11-28 18:56 字数 577 浏览 5 评论 0原文

尝试在 Python 中设置一些基本的套接字代码(好吧,Jython,但我认为这与这里无关)。

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

它告诉我:

 s.connect("localhost", 2057)
  文件“”,第 1 行,连接中
类型错误:connect() 恰好需要 2 个参数(给定 3 个参数)

我感觉有一些非常简单的东西正在盯着我的脸,但我不知道我做错了什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦里人2024-12-05 18:56:27

您必须将元组传递给 connect() 方法。

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

第一个(隐式)参数预期是 self,第二个是元组。

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.

拥抱我好吗2024-12-05 18:56:27

您正在传递三个参数! s 作为隐式第一个参数传递,您指定的其他两个参数是第二个和第三个参数。

现在,它感到不安的原因是因为 socket.connect() 只接受一个参数(当然,如果算上隐式实例参数,则为两个参数): 查看文档

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.

花辞树2024-12-05 18:56:27
s.connect(("localhost", 2057))

您隐式传递的第三个(或第一个)参数是 self (s)。

套接字采用由(HOST, PORT)组成的元组

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

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

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

如若梦似彩虹2024-12-05 18:56:27

套接字connect函数用于将套接字连接到远程地址。对于 IP 套接字,地址是一对(主机、端口),

因此您应该使用:

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

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) )
半衾梦2024-12-05 18:56:27

使用:

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

use:

s.connect(("localhost", 2057))
温馨耳语2024-12-05 18:56:27

socket.connect 只接受 1 个参数,即地址,如果自计数则接受 2 个参数。
地址的格式在第四段中说明, http://docs.python.org/library /socket.html

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文