Python sendto() 不适用于 3.1(适用于 2.6)

发布于 2024-08-02 10:16:59 字数 586 浏览 4 评论 0原文

由于某种原因,以下内容似乎在我运行 python 2.6 的 ubuntu 机器上完美运行,并在运行 python 3.1 的 windows xp 机器上返回错误

from socket import socket, AF_INET, SOCK_DGRAM
data = 'UDP Test Data'
port = 12345
hostname = '192.168.0.1'
udp = socket(AF_INET,SOCK_DGRAM)
udp.sendto(data, (hostname, port))

下面是 python 3.1 抛出的错误:

Traceback (most recent call last):
  File "sendto.py", line 6, in <module>
    udp.sendto(data, (hostname, port))
TypeError: sendto() takes exactly 3 arguments (2 given)

我已查阅了 python 3.1 和 sendto 的文档()只需要两个参数。关于可能导致此问题的任何想法?

For some reason, the following seems to work perfectly on my ubuntu machine running python 2.6 and returns an error on my windows xp box running python 3.1

from socket import socket, AF_INET, SOCK_DGRAM
data = 'UDP Test Data'
port = 12345
hostname = '192.168.0.1'
udp = socket(AF_INET,SOCK_DGRAM)
udp.sendto(data, (hostname, port))

Below is the error that the python 3.1 throws:

Traceback (most recent call last):
  File "sendto.py", line 6, in <module>
    udp.sendto(data, (hostname, port))
TypeError: sendto() takes exactly 3 arguments (2 given)

I have consulted the documentation for python 3.1 and the sendto() only requires two parameters. Any ideas as to what may be causing this?

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

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

发布评论

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

评论(2

牵你的手,一向走下去 2024-08-09 10:16:59

在 Python 3 中,字符串(第一个)参数必须是 bytes 或 buffer 类型,而不是 str。如果您提供可选的 flags 参数,您将收到该错误消息。将数据更改为:

data = b'UDP Test Data'

您可能希望在 python.org 错误跟踪器上提交有关该问题的错误报告。 [编辑:已按 Dav 的说明提交]

...

>>> data = 'UDP Test Data'
>>> udp.sendto(data, (hostname, port))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sendto() takes exactly 3 arguments (2 given)
>>> udp.sendto(data, 0, (hostname, port))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sendto() argument 1 must be bytes or buffer, not str
>>> data = b'UDP Test Data'
>>> udp.sendto(data, 0, (hostname, port))
13
>>> udp.sendto(data, (hostname, port))
13

In Python 3, the string (first) argument must be of type bytes or buffer, not str. You'll get that error message if you supply the optional flags parameter. Change data to:

data = b'UDP Test Data'

You might want to file a bug report about that at the python.org bug tracker. [EDIT: already filed as noted by Dav]

...

>>> data = 'UDP Test Data'
>>> udp.sendto(data, (hostname, port))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sendto() takes exactly 3 arguments (2 given)
>>> udp.sendto(data, 0, (hostname, port))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sendto() argument 1 must be bytes or buffer, not str
>>> data = b'UDP Test Data'
>>> udp.sendto(data, 0, (hostname, port))
13
>>> udp.sendto(data, (hostname, port))
13
故事还在继续 2024-08-09 10:16:59

Python bugtracker 上的相关问题:
http://bugs.python.org/issue5421

Related issue on the Python bugtracker:
http://bugs.python.org/issue5421

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