'Socket Programming - Python

I'm referring : https://bedfordsarah.wordpress.com/2013/10/29/python-socket-programming-project-1-http-web-server/. However, I'm getting error : a Byte like object is required not string Also, when I'm trying to encode the string into bytes, it is only rendering the closing tags.

Please help me with the same.

Thank you in advance.



Solution 1:[1]

You can use encode() and decode() function to encode and decode strings as shown below to convert between string to bytes and vice versa.

s = 'abcd'
e = s.encode()
d = e.decode()
print(s)
print(e)
print(d)

Output...

abcd
b'abcd'
abcd

Use the above functions to decode the data recevied from the socket using recv() function and encode() the data that needs to be sent with the send() function.

data_from_socket = your_socket.recv().decode()
data_to_socket   = data_from_socket.ecnode()
your_socket.send(data_to_socket)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1