我如何使用套接字将波形文件从客户端发送到服务器

How can i send a wave file from a client to a server using socket

所以我尝试使用套接字将保存的 wave 文件从客户端发送到服务器,但是每次尝试都失败了,我得到的最接近的是:

#Server.py
requests = 0
while True:
    wavfile = open(str(requests)+str(addr)+".wav", "wb")
    while True:
        data = clientsocket.recv(1024)
        if not data:
            break
        requests = requests+1
        wavefile.write(data) 

#Client.py
bytes = open("senddata", "rb")
networkmanager.send(bytes.encode())

此代码的错误是 "AttributeError: '_io.BufferedReader' object has no attribute 'encode'" 那么有什么方法可以解决这个问题吗?我正在使用 python

由于您使用的是 "read-binary" 模式,因此无需在发送前对字节进行编码。

并且您应该读取文件以获取字节而不是 BufferedReader。

bytes = open("senddata", "rb").read()
networkmanager.send(bytes)

您必须使用 read 读取字节,并使用 sendall 将字节发送到服务器:

bytes = open("senddata", "rb")
networkmanager.sendall(bytes.read())