sockets - 'str' does not support the buffer interface - Python 3 -
this question has answer here:
import socket servername = "hostname" serverport = 12000 clientsocket = socket.socket(socket.af_inet, socket.sock_dgram) message = input('input lowercase sentence:') clientsocket.sendto(message, (servername, serverport)) modifiedmessage, serveraddress = clientsocket.recvfrom(2048) print (modifiedmessage) clientsocket.close()
gives error
traceback (most recent call last): file "c:/python34/server1.py", line 11, in <module> clientsocket.sendto(message, (servername, serverport)) typeerror: 'str' not support buffer interface
what do?
i've encode code
import socket servername = "hostname" serverport = 12000 clientsocket = socket.socket(socket.af_inet,socket.sock_dgram) message = input('input lowercase sentence:').encode('ascii') clientsocket.sendto(message,(servername, serverport)) modifiedmessage, serveraddress = clientsocket.recvfrom(2048) print (modifiedmessage.decode('ascii')) clientsocket.close()
but still error
traceback (most recent call last): file "j:\sistem jaringan\task i\client.py", line 11, in <module> clientsocket.sendto(message,(servername, serverport)) socket.gaierror: [errno 11004] getaddrinfo failed
how can fix it?
sockets sent bytes other end of connection. encode strings bytes before trying send. in sendto method, change message
message.encode()
. add argument encoding='xyz'
if not want default utf-8 encoding. modifiedmessage
receive bytes, , may want decode string.
Comments
Post a Comment