首先,TFTP协议遵循以下格式:
我们只需要根据这些协议, 来决定我们每次请求数据是什么
构造请求数据时的注意点:(struct的格式)
例如
fileName = "123456.jpg"
dlreq = struct.pack("!H10sb5sb", 1, fileName.encode("utf-8"), 0, b"octet", 0)
注意: "!H10sb5sb"中, H标示两个字节, s表示一个字节, b标示1个字节. 具体含义如下图:
注1.q和Q只在机器支持64位操作时有意思
注2.每个格式前可以有一个数字,表示个数
注3.s格式表示一定长度的字符串,4s表示长度为4的字符串,但是p表示的是pascal字符串
注4.P用来转换一个指针,其长度和机器字长相关
注5.最后一个可以用来表示指针类型的,占4个字节
然后上代码:
from socket import *
import struct
import sys
from threading import Thread
g_ip = ""
g_port = 0
def downLoad():
g_socket = socket(AF_INET, SOCK_DGRAM)
g_socket.bind(("", 9090))
fileName = input("请输入要下载的文件名: ")
fileNameLen = len(fileName)
# 下载请求格式控制字符串
dlreqStr = "!H" + str(fileNameLen) + "sb5sb"
# 构造下载请求数据
dlreq = struct.pack(dlreqStr, 1, fileName.encode("utf-8"), 0, b"octet", 0)
sendAdd = (g_ip, g_port)
g_socket.sendto(dlreq, sendAdd)
p_Num = 0
recvfile = ""
while True:
recvData, recvAddr = g_socket.recvfrom(1024)
recvdataLen = len(recvData)
cmd_tuple = struct.unpack("!HH", recvData[:4])
cmd = cmd_tuple[0]
currentPackNum = cmd_tuple[1]
if cmd == 3:
if currentPackNum == 1:
recvfile = open(fileName, "wb")
if p_Num + 1 == currentPackNum:
recvfile.write(recvData[4:])
p_Num += 1
print("第 %d 次接收到数据"%p_Num)
ackBuf = struct.pack("!HH", 4, p_Num)
g_socket.sendto(ackBuf, recvAddr)
if recvdataLen < 516:
recvfile.close()
print("已经下载成功!")
break
elif cmd == 5:
print("错误!! 错误块: %d"%currentPackNum)
break
recvfile.close()
g_socket.close()
def upLoad():
upSocket = socket(AF_INET, SOCK_DGRAM)
num = 0
fileName = input("输入要上传的文件:")
sendAdd = (g_ip, g_port)
fileNameLen = len(fileName)
# 上传请求格式控制字符串
ulreqStr = "!H" + str(fileNameLen) + "sb5sb"
# 构造下载请求数据
ulreq = struct.pack(ulreqStr, 2, fileName.encode("utf-8"), 0, b"octet", 0)
upSocket.sendto(ulreq, sendAdd)
recvData1 = upSocket.recvfrom(1024)
rand_port = recvData1[1][1]
ack_num = struct.unpack("!H",recvData1[0][2:4])
sendAdd = (g_ip, rand_port)
try:
fileRead = open(fileName, 'rb')
except:
errorData = struct.pack("!HHHb", 5, 5, 5, num)
upSocket.sendto(errorData, sendAdd)
exit() # 退出线程
while True:
readData = fileRead.read(512)
sendData = struct.pack("!HH", 3, num) + readData
upSocket.sendto(sendData, sendAdd)
if len(sendData) < 516:
print("传输成功!!")
break
recv_ack = upSocket.recvfrom(1024)[0]
recv_cmd, ack_num = struct.unpack("!HH", recv_ack)
print("接收到了 %d", ack_num)
num +=1
if int(recv_cmd) != 4 or int(ack_num != num - 1):
break
upSocket.close()
fileRead.close()
def main():
global g_ip
global g_port
g_ip = input("请输入ip地址: ")
g_port = int(input("请输入端口号: "))
t1 = Thread(target = upLoad)
t1.start()
t1.join()
if __name__ == "__main__":
main()