在 python3 中通过套接字发送 .mp4 文件
Sending a .mp4 file over sockets in python3
我正在尝试制作两个小程序;一个是服务器,它将从客户端接收 mp4 文件。客户端只是一个小程序,它发送位于其文件夹中的 .mp4 文件。
我能够完全发送 mp4 文件并创建一个相同大小的文件,但由于某种原因 mp4 损坏或其他问题导致我无法在 QuickTime 播放器中播放 mp4 文件或VLC.
我不明白这一点,因为我正在复制所有字节并以小数据包发送。我真的很感激一些帮助或提示。
服务器代码:
#!/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)
我正在尝试制作两个小程序;一个是服务器,它将从客户端接收 mp4 文件。客户端只是一个小程序,它发送位于其文件夹中的 .mp4 文件。
我能够完全发送 mp4 文件并创建一个相同大小的文件,但由于某种原因 mp4 损坏或其他问题导致我无法在 QuickTime 播放器中播放 mp4 文件或VLC.
我不明白这一点,因为我正在复制所有字节并以小数据包发送。我真的很感激一些帮助或提示。
服务器代码:
#!/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)