Mohammad Taghi Ghandili Mohammad Taghi Ghandili - 1 month ago 6
Python Question

sending a message by socket in python

I'm working on Python Socking programming and i have a problem with send() function.
Im following this:
https://pythontips.com/2013/08/06/python-socket-network-programming

# send a thank you message to the client.
c.send('Thank you for connecting')
# Close the connection with the client
c.close()


but i will get this error:

Traceback (most recent call last):
File "*.py", line 27, in
c.send('Thank you for connecting')
TypeError: a bytes-like object is required, not 'str'

Answer

python 3 send takes a bytes object.

https://docs.python.org/3/library/socket.html#socket.socket.send

c.send(b'Thank you for connecting')