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

is the logic behind my trivial tftp client flawed?

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

所以我为此压力很大。

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

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

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

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

下载的文件多了几个字

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

最糟糕的是,我已经让它工作了,但我丢失了记忆棒,我丢失了程序的最新版本,运行ning 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,该行是 运行,具有陈旧的值。它可能应该在内循环中。