TypeError - python 中的客户端错误 [英] TypeError - Client error in python

查看:28
本文介绍了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 中的客户端错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆