TypeError - Client error in python(TypeError - python 中的客户端错误)
本文介绍了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
这篇关于TypeError - python 中的客户端错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:TypeError - python 中的客户端错误


猜你喜欢
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01