Mohammad Taghi Ghandili Mohammad Taghi Ghandili - 1 year ago 104
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:

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

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 Source

python 3 send takes a bytes object.

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download