gpt4 book ai didi

python - 我简单的 tftp 客户端背后的逻辑有缺陷吗?

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:17:21 29 4
gpt4 key购买 nike

我还是个学生,所以我可能漏掉了一些非常明显的东西。

所以我为此非常紧张。

但无论如何,这是我的 TFTP python 代码,它所做的只是从我们讲师的服务器下载一个文本文件。

它生成的文件如下所示: http://pastebin.com/TP8hngxM

原始文件是这样的: http://pastebin.com/xDMjkABp

如果你通过差异检查器运行它,差异很小,而且只在一个特定的地方,我真的很难弄清楚为什么会这样。

下载的文件多了几个字。

如果你有5分钟的时间,请你看看我的嵌套while循环(其他都是讲师提供的,不能更改),看看有没有问题?

最糟糕的是,我已经让它工作了,但我丢失了内存棒,我丢失了运行 100% 正常的程序的最新版本。

所以正如我所说,它只是关于嵌套的 while 循环,我不允许更改它上面的任何内容。

    #!/usr/bin/python

import struct
import sys
import os
import select
import glamnetsim
from socket import *

serverHost = 'mcgreg.comp.glam.ac.uk'
serverPort = 69
timeoutSecs = 5
debugging = False
RRQ, WRQ, DATA, ACK, ERROR = range(1, 6)
codeDescriptions = {RRQ:"RRQ", WRQ:"WRQ", DATA:"DATA", ACK:"ACK", ERROR:"ERROR"}

def printf (format, *args):
print str(format) % args,

def finish ():

printf("you should see\n1e951df315d433aa4df2065a1ad31311\n")
os.system("md5sum newfile.txt")
sys.exit(0)

def sendPacket (packet, port):
global sock, debugging
global serverIp
if debugging:
for i in packet:
print ('%02x' % ord(i)),
print ''
sock.sendto(packet, (serverIp, port))

def sendReadRequest (filename, mode):
global serverPort
format = "!H%ds" % (len(filename)+1)
format += "%ds" % (len(mode)+1)
s = struct.pack(format, 1, filename, mode)
sendPacket(s, serverPort)

def sendRealAck(blockno, port):
s = struct.pack("!H", 4)
s += struct.pack("!H", blockno)
sendPacket(s, port)

def sendACK (blockno, port):
print " -> ACK:%d\n" % blockno
if blockno == 0:
sendReadRequest("the_machine_stops.txt", "octet")
else:
sendRealAck(blockno, port)

def stripPacket (s):
if len(s)>3:
code = struct.unpack("!H", s[:2])[0]
blockno = struct.unpack("!H", s[2:4])[0]
data = s[4:]
code, data = glamnetsim.simulatePossibleError (code, data)
return code,blockno,data
else:
debugPrint("corrupt packet")
return -1,-1,""

def debugPrint (s):
global debugging
if debugging:
print s

def getDesc (c):
global codeDescriptions
return codeDescriptions[c]

sock = socket(AF_INET, SOCK_DGRAM)
serverIp = gethostbyname(serverHost)
sock.setblocking(1)
sendReadRequest("the_machine_stops.txt", "netascii")
lastblock = 0
blockno = 0
port = serverPort
f = open("newfile.txt", "w")

while True:
while True:

if blockno == lastblock+1:
break

r, w, x = select.select([sock], [], [], 5.0)

if r == []:
sendACK(lastblock, port)

else:

(packet, (address, port)) = sock.recvfrom(512+4)
code, newblock, text = stripPacket(packet)
print code, blockno

if code is 3:
blockno = newblock
sendACK(blockno, port)

if code is 5:
sendACK(lastblock, port)

print "Bn: " + str(blockno) + " Lb: " + str(lastblock)
lastblock = blockno
f.write(text)
print "OK"
if len(text) < 512:
break
f.close()
finish()

最佳答案

f.write(text)

如果 blockno == lastblock+1,该行将以过时值运行。它可能应该在内循环中。

关于python - 我简单的 tftp 客户端背后的逻辑有缺陷吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42633923/

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