我正在尝试构建一个通过Python 3.4套接字模块通过无线局域网发送文件的应用程序。它似乎能够在一个工作站内发送文件,但是当尝试在同一网络上的两台计算机之间发送文件时,计算机似乎只接收前几千字节,该数量取决于发送时文件的总大小
例如,发送一个127字节的文件没有问题,但是一个34,846字节的文件,只收到1,431或4,327字节(似乎只接收到这两个字节数,似乎在两者之间随机切换)和一个65,182字节的文件,只收到5,772或4,324字节(相同的情况)。通过查看收到的文件的内容,它似乎是收到的前几个字节。
两个系统都有超过2GB的免费可访问RAM和足够的存储空间。服务器在Windows 8.1,Python 3.4.2上运行,客户端是Ubuntu 14.04 Linux,Python 3.4.0。
我的代码可能是零碎的,而且写得很差,因为我是初学者,没有正式的计算机科学教育或任何值得注意的经验,特别是在网络编程方面。但是,我已经根除了代码,并且没有明确的解决方案来展示我的大脑。
服务器(主机):
import os
import socket
import struct
import time
import hashlib
def md5(fname):
hash_md5 = hashlib.md5()
with open(fname, "rb") as readFile:
for chunk in iter(lambda: readFile.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
try:
s = socket.socket()
host = socket.gethostname()
port = 26
s.bind(("0.0.0.0", port))
filename = input("File to send? ")
fileLength = os.stat(filename).st_size
print("Length:", fileLength, "bytes")
fileLengthBytes = struct.pack(">L", fileLength)
filenameBytes = bytes(filename, "ascii")
filenameLength = struct.pack(">L", len(filenameBytes))
checksum = md5(filename)
checksumBytes = bytes(bytearray.fromhex(checksum))
s.listen(5)
while True:
c, addr = s.accept()
print("Connection from", addr)
c.send(checksumBytes)
time.sleep(0.1)
c.send(filenameLength + fileLengthBytes)
time.sleep(0.1)
with open(filename, "rb") as f:
c.send(filenameBytes + f.read())
c.close()
finally:
try:
c.close()
except NameError:
pass
except Exception as e:
print(type(e), e.args, e)
try:
f.close()
except NameError:
pass
except Exception as e:
print(type(e), e.args, e)
客户(收件人):
import socket
import struct
import hashlib
def md5(fname):
hash_md5 = hashlib.md5()
with open(fname, "rb") as readFile:
for chunk in iter(lambda: readFile.read(4096), b""):
hash_md5.update(chunk)
return hash_md5.hexdigest()
CHECKSUM_LENGTH = 16
try:
s = socket.socket()
host = socket.gethostname()
port = 26
ip = input("Connect to IP: ")
s.connect((ip, port))
initialChecksum = s.recv(CHECKSUM_LENGTH)
received = s.recv(8)
# 1st 4 bytes: filename length
# 2nd 4 bytes: file contents length
filenameLength = struct.unpack(">L", received[0:4])[0]
fileLength = struct.unpack(">L", received[4:])[0]
print("Length:", fileLength)
bytesToReceive = filenameLength + fileLength
receivedBytes = s.recv(bytesToReceive)
filename = str(receivedBytes[0:filenameLength], "ascii")
f = open(filename, "wb")
f.write(receivedBytes[filenameLength:]) # Write file contents
actualChecksum = bytes(bytearray.fromhex(md5(filename)))
if initialChecksum == actualChecksum:
print("Fully received", filename)
else:
print(filename, "not received correctly")
finally:
try:
f.close()
except NameError:
pass
except Exception as e:
print(type(e), e.args, e)
try:
s.close()
except NameError:
pass
except Exception as e:
print(type(e), e.args, e)
我知道我使用的md5
函数似乎不适用于下面的小文件,我认为是4千字节。
问题出在哪里?怎么解决?我错过了一些重要的事情吗?
答案 0 :(得分:0)
您只收到传输的前几个包。这是因为s.recv()
将接收已经到达客户端的字节,但最多只接收作为参数给出的数字。在本地机器上传输速度很快,您将收到更多。
要获得传输的所有部分,您应该收集所有字节,直到达到预期的长度。
一种非常简单的方法是:
buffer = ''
while len(buffer) < bytes_to_receive:
buffer += s.recv(1500)
1500是典型的LAN MTU,并且至少可以获得完整的包。给出的代码只是Python 2解释概念的一个简单示例。它需要进行优化和优化,如果您使用的是Python 3,则需要适应字节。