Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 客户端和服务器无法交换数据_Python_Sockets - Fatal编程技术网

Python 客户端和服务器无法交换数据

Python 客户端和服务器无法交换数据,python,sockets,Python,Sockets,我使用的是python 3.3。这是Server.py。一切都很好,服务器和客户端都可以连接 此处的“tcpcli.send(“[%s]%s%”(字节(ctime(),“utf-8”),data))可能有问题。请帮助我解决此问题 from socket import * from time import ctime HOST='' PORT=21567 BUFSIZ=1024 ADDR=(HOST,PORT) tcp=socket(AF_INET,SOCK_STREAM) tcp.bind(

我使用的是python 3.3。这是Server.py。一切都很好,服务器和客户端都可以连接 此处的“tcpcli.send(“[%s]%s%”(字节(ctime(),“utf-8”),data))可能有问题。请帮助我解决此问题

from socket import *
from time import ctime

HOST=''
PORT=21567
BUFSIZ=1024
ADDR=(HOST,PORT)

tcp=socket(AF_INET,SOCK_STREAM)
tcp.bind(ADDR)
tcp.listen(5)

while True:
    print('waiting for connection')
    tcpcli,addr=tcp.accept()
    print('...connected from:',addr)

    while True:
        data=tcpcli.recv(BUFSIZ)
        if not data:
            break
        tcpcli.send('[%s]%s'%(bytes(ctime(),'utf-8'),data))
    tcpcli.close()
tcp.close()                 
这是CLient.py

    from socket import *


HOST='127.0.0.1'
PORT=21567
BUFSIZ=1024
ADDR=(HOST,PORT)

tcpcli=socket(AF_INET,SOCK_STREAM)
tcpcli.connect(ADDR)


while True:
    data=input('>')
    if not data:
        break
    tcpcli.send(data)
    data=tcpcli.recv(BUFSIZ)
    if not data:
        break
    print (data.decode('utf-8'))

tcpcli.close()                 
当我运行这两个程序时,它们工作正常,只是我无法从客户端发送任何数据。 我收到了这个错误消息

tcpcli.send(data)
TypeError: 'str' does not support the buffer interface

您正在使用Python3。这意味着,在使用CLI时,
input()
将返回一个
str
对象(相当于python2
unicode
)。它包含您输入的字符的unicode代码点的内部表示形式。要通过字节流接口(如管道、套接字等)发送数据,必须将其转换为字节对象。这可以通过选择一种编码(如UTF-8)并执行如下操作轻松完成:

data_raw = data.encode("utf-8")
tcpcli.send(data_raw)
您必须以类似的方式调整服务器代码,首先解码从客户端接收的数据,然后在对其执行字符串操作后重新编码:

        data_decoded = data.decode("utf-8")
        reply = '[%s]%s' % (ctime(), data_decoded)
        tcpcli.send(reply.encode("utf-8"))

您正在构建unicode字符串,而不是字节字符串,并且套接字接口不支持unicode字符串。您需要对字符串插值的结果进行编码:

    tcpcli.send(bytes('[%s]%s' % (ctime(),data), 'utf-8'))

@Martin嘿,谢谢…它起作用了我在输出中得到了一个额外的b和时间戳..ny help?tcpcli.send(字节(“[%s]%s%”(ctime(),data),“utf-8”))工作得很好,但我在每个输出中都得到了一个额外的b?ny主意