我已经写过我认为用Python套接字通过网络下载文件的相当简单的脚本,但服务器端挂在socket.sendall上。我可以获得我想要的文件,但后续调用senddata会导致崩溃。
在进一步研究之后,我相当肯定它与我如何打包标题数据有关,这意味着它可能是一个低级问题,超越我的。
对于上下文:' master'代码在x86计算机上运行,而' slave'代码在Raspberry Pi Zero(ARM)上运行。
代码中定义的标题,但这里是概述:
我很困惑为什么这不起作用,因为它似乎是一个例行的代码。
#takes filename and sends the data to the connected socket
def senddata(filename,socket):
try:
filehandle = open(filename,'rb')
except IOError:
socket.sendall('ABORT')
print 'File ' +filename+ ' not found; not sending...'
else:
#writes 8 byte header consisting of:
#length of file (including filename) in kb (4b)
#length of filename (4b)
numbytes = struct.pack("<I",math.ceil((os.stat(filename).st_size + len(filename)) / 1024))
#amount of KB (1024) to receive, written to 4-byte integer
filenamebytes = struct.pack("<I",len(filename))
print 'Sending '+filename+'...'
socket.sendall(numbytes + filenamebytes)
data = filename + filehandle.read(1024 - len(filename))
while True:
socket.sendall(data)
data = filehandle.read(1024)
if not data:
break
filehandle.close()
print 'Send complete.'
def ReceiveDownload(socket):
#gets header
data = socket.recv(8)
if data != 'ABORT':
filekb = struct.unpack("<I",data[:4])[0]#endianness may be affecting data transfer
filenamebytes = struct.unpack("<I",data[4:])[0]
data = socket.recv(1024)
filename = data[:filenamebytes]
curkb = 1
print 'Getting '+filename+"..."
writeto = open('./footage/'+filename,'wb')
while curkb <= filekb:
print '\r' + str(curkb) + "/" + str(filekb),
sys.stdout.flush()
writeto.write(data)
data = socket.recv(1024)
curkb += 1
writeto.close()
print 'Download of '+filename+' successful.'
else:
print 'Download failed.'
答案 0 :(得分:0)
我自己用jasonharper的建议解决了这个问题。
通过使用以字节为单位的更常规代码切换以kb为单位的代码,始终读取来自套接字的所有数据。我不确定这个故障的原因是由于浮点问题还是其他原因,但它确实有效。如果我需要更多容量,我将切换到使用Longs。
为了代码可读性并减少舍入错误的可能性,我还停止将文件名和数据集中到一个传输块中。
当更简单的代码可以完成这个技巧时,原始代码被过度设计。
修订代码:
def ReceiveDownload(socket):
#gets header
data = socket.recv(8)
if data != 'ABORT':
fileb = struct.unpack("<I",data[:4])[0]#endianness may be affecting data transfer
filenamebytes = struct.unpack("<I",data[4:])[0]
data = socket.recv(filenamebytes)
filename = data
curb = 0
print 'Getting '+filename+"..."
writeto = open('./footage/'+filename,'wb')
while curb < fileb:
sys.stdout.flush()
data = socket.recv(1024)
writeto.write(data)
curb += len(data)
print '\r' + str(curb) + "/" + str(fileb),
writeto.close()
print 'Download of '+filename+' successful.'
else:
print 'Download failed.'
def senddata(filename,socket):
try:
filehandle = open(filename,'rb')
except IOError:
socket.sendall('ABORT')
print 'File ' +filename+ ' not found; not sending...'
else:
#writes 8 byte header consisting of:
#length of file in kb (4b)
#length of filename (4b)
numbytes = struct.pack("<I",math.ceil(os.stat(filename).st_size))
#amount of KB (1024) to receive, written to 4-byte integer
filenamebytes = struct.pack("<I",len(filename))
print 'Sending '+filename+'...'
socket.sendall(numbytes + filenamebytes)
data = filename
socket.sendall(data)
while True:
data = filehandle.read(1024)
socket.sendall(data)
if not data:
break
filehandle.close()
print 'Send complete.'