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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With