The sockets can not send data

advertisements

This question already has an answer here:

  • TypeError: 'str' does not support the buffer interface 7 answers

Code

host = "127.0.0.1"
port=4446
from socket import *
s = socket(AF_INET, SOCK_STREAM)
s.bind((host,port))

s.listen(1)

print("Listening for connections...")

q,addr = s.accept()

data = input("Type something in")
q.send(data)
s.close

Error

TypeError:'str' does not support the buffer interface

So I know that there are hundreds of questions on here about this error but I still cant think up a solution, can one of you guys help me out? :(


In Python 3, strings are Unicode values, but sockets can only take encoded bytes.

Encode your data first:

q.send(data.encode('utf8'))

I picked UTF-8 here as the codec to use, but you need to consciously pick a encoding suitable to your specific application.