Python sendto() 不适用于 3.1(适用于 2.6)
由于某种原因,以下内容似乎在我运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Python 3 中,字符串(第一个)参数必须是 bytes 或 buffer 类型,而不是 str。如果您提供可选的 flags 参数,您将收到该错误消息。将数据更改为:
d
ata = b'UDP Test Data'
您可能希望在 python.org 错误跟踪器上提交有关该问题的错误报告。 [编辑:已按 Dav 的说明提交]
...
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:
d
ata = 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]
...
Python bugtracker 上的相关问题:
http://bugs.python.org/issue5421
Related issue on the Python bugtracker:
http://bugs.python.org/issue5421