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?
在 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]
...