1

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? :(

jww
  • 97,681
  • 90
  • 411
  • 885

2 Answers2

1

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
0

Strings are Unicode objects in python 3. You need to encode it to a byte string before sending.

Data.encode("ASCII")
Keith
  • 42,110
  • 11
  • 57
  • 76