[Tutor] UDP client

Alan Gauld alan.gauld at yahoo.co.uk
Sun Feb 26 03:42:15 EST 2017


On 26/02/17 06:44, Phil wrote:

> s.connect((host, 1210))
> data = "GET_LIST"

This is a string, you need to use bytes.

data = bytes("GET_LIST",'utf8')

> s.sendall(data)
> #s.sendto(data, (host, 1210))
> s.shutdown(1)

> Traceback (most recent call last):
>    File "/home/phil/Python/predict_client1.py", line 12, in <module>
>      s.sendall(data)
> TypeError: a bytes-like object is required, not 'str'

Python 3 uses unicode strings by default.
Many low level functions need a byte array instead.
Usually you can just wrap your original string in
a bytes call with utf8. If you are using more complex
strings you may need to start using explicit encode/decode.
But in this case it shouldn't be needed.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list