gpt4 book ai didi

python-3.x - 通过python3中的套接字发送.mp4文件

转载 作者:行者123 更新时间:2023-12-03 11:52:51 25 4
gpt4 key购买 nike

我正在尝试制作两个小程序;一个是服务器,它将从客户端接收mp4文件。客户端只是一个小程序,它发送位于其文件夹中的.mp4文件。
我能够完全发送mp4文件,并且创建了相同大小的文件,但是由于某些原因mp4损坏或出现其他问题,因此无法在QuickTime播放器或VLC中播放mp4文件。
我不明白这一点,因为我正在复制所有字节,然后全部以小数据包形式发送。我真的很感谢您的帮助或提示。
服务器代码:

#!/usr/bin/python3

from socket import socket, gethostname

s = socket()
host = gethostname()
port = 3399
s.bind((host, port))
s.listen(5)
n = 0

while True:
print("Listening for connections...")
connection, addr = s.accept()

try:
print("Starting to read bytes..")
buffer = connection.recv(1024)

with open('video_'+str(n), "wb") as video:
n += 1
i = 0
while buffer:
buffer = connection.recv(1024)
video.write(buffer)
print("buffer {0}".format(i))
i += 1

print("Done reading bytes..")
connection.close()

except KeyboardInterrupt:
if connection:
connection.close()
break

s.close()
客户代码:
#!/usr/bin/python3

from socket import socket, gethostname, SHUT_WR

s = socket()
host = gethostname()
port = 3399

s.connect((host, port))

print("Sending video..")

with open("test.mp4", "rb") as video:
buffer = video.read()
print(buffer)
s.sendall(buffer)

print("Done sending..")
s.close()

最佳答案

修复服务器代码中的错误:

#!/usr/bin/python3

from socket import socket, gethostname

s = socket()
host = gethostname()
port = 3399
s.bind((host, port))
s.listen(5)
n = 0

while True:
print("Listening for connections...")
connection, addr = s.accept()

try:
print("Starting to read bytes..")
buffer = connection.recv(1024)

with open('video_'+str(n)+'.mp4', "wb") as video:
n += 1
i = 0
while buffer:
video.write(buffer)
print("buffer {0}".format(i))
i += 1
buffer = connection.recv(1024)

print("Done reading bytes..")
connection.close()

except KeyboardInterrupt:
if connection:
connection.close()
break

s.close()

在这里修复:
with open('video_'+str(n)+'.mp4', "wb") as video:

和这里:
while buffer:                
video.write(buffer)
print("buffer {0}".format(i))
i += 1
buffer = connection.recv(1024)

关于python-3.x - 通过python3中的套接字发送.mp4文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54388823/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com