TypeError - python 中的客户端错误

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 中的客户端错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)