Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.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 如何使用套接字将wave文件从客户端发送到服务器_Python_Sockets_Wave - Fatal编程技术网

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

Python 如何使用套接字将wave文件从客户端发送到服务器,python,sockets,wave,Python,Sockets,Wave,因此,我试图用套接字将保存的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 r

因此,我试图用套接字将保存的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:”\u io.BufferedReader“对象没有属性‘encode’”,所以有没有办法解决这个问题?我使用python

因为您使用的是“读取二进制”模式,所以在发送之前不需要对字节进行编码

您应该读取文件以获取字节,而不是BufferedReader

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

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

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