[Python-3000] python3000 udp problem
Nick Coghlan
ncoghlan at gmail.com
Wed Jun 11 11:16:07 CEST 2008
Long Ge wrote:
> import socket
>
> if __name__ == '__main__':
> print("main")
> udp = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
> udp.sendto("1",0,('127.0.0.1 <http://127.0.0.1>',3722))
> udp.close()
>
>
> *output:*
> main
> Traceback (most recent call last):
> File "C:\Python30\Projects\udp.py", line 6, in <module>
> udp.sendto("1",0,('127.0.0.1 <http://127.0.0.1>',3722))
> TypeError: sendto() argument 1 must be bytes or read-only buffer, not str
>
> what is the problem about argument 1?
Python 3.0 is much stricter than 2.x about the distinction between
character based-data (i.e. Unicode strings) and 8-bit byte sequences
(bytes and bytearray).
As Benjamin pointed out, socket.sendto expects a bytestream rather than
a character string, so you need to either use bytes directly (such as
b"1") or encode the character string to a byte sequence (such as
"1".encode("utf-8")).
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://www.boredomandlaziness.org
More information about the Python-3000
mailing list