TypeError - python 中的客户端错误
问题描述
我在 python 中创建了一个客户端/服务器代码.服务器运行良好并在 8000 端口上进行监听,但是当我通过客户端连接到它然后尝试向服务器发送消息时,出现以下错误:
I created a Client/Server code in python. Server works well and get listen on 8000 port, but when I connect to it via client and then I try to send a message to server I get the following error:
Traceback (most recent call last):
File "C:UsersmiladworkspaceNetworkProgrammingclient.py", line 26, in <module>
if __name__ == "__main__" : main()
File "C:UsersmiladworkspaceNetworkProgrammingclient.py", line 20, in main
TcpSocket.send(sendData)
TypeError: 'str' does not support the buffer interface
我不知道如何解决有关客户端代码的问题.下面我放了客户端代码.我用 Python 语言编写的.
I don't know how can I fix this problem about the client code. In the following I put the client code. I written it with Python language.
#!/usr/bin/python3
import socket
from builtins import input
def main():
serverHostNumber = input("Please enter the ip address of the server:
")
serverPortNumber = input("Please enter the port of the server:
")
# create a socket object
TcpSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# connection to hostname on the port.
TcpSocket.connect((serverHostNumber, int(serverPortNumber)))
while True:
data = TcpSocket.recv(1024)
print("Server : ", data)
sendData = str(input("Client : "))
TcpSocket.send(sendData)
TcpSocket.close()
if __name__ == "__main__" : main()
解决方案
TcpSocket.send(sendData)
看起来像 send
只接受 bytes
实例.试试:
Looks like send
accepts only bytes
instances. Try:
TcpSocket.send(bytes(sendData, "ascii")) #... or whatever encoding is appropriate
相关文章