Python sendto () does not work on 3.1 (works since 2.6)

For some reason, the following seems to work fine on my ubuntu machine running python 2.6 and returns an error on my Windows XP stack with python 3.1 running

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)) 

The following is the error python 3.1 creates:

 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 read the documentation for python 3.1, and sendto () only requires two parameters. Any ideas as to what could be causing this?

+4
source share
2 answers

In Python 3, the string (first) argument must be a byte type or buffer, not str. You will receive this error message if you specify the optional flags option. Change the data to:

q ata = b'UDP Test Data'

You might want to post a bug report in the python.org error tracking log. [EDIT: already filed as Dove noted]

...

 >>> 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 
+6
source

A related issue in the Python bugtracker: http://bugs.python.org/issue5421

+4
source

All Articles