![]() 88 89 if cmd = 3: # whether the data packet 90 if curPackNum = 1 :ĩ1 # can additionally open the file 92 saveFile = open(self.filename, " ab " )ĩ3 94 # packet number is equal to the last and whether the 95 if locPackNum + 1 = curPackNum:ĩ6 saveFile.write(recvData) # write data 97 locPackNum += 1ĩ8 99 # Send ACK response 100 ackBuf = struct.pack(b " !HH ", 4, locPackNum)ġ01 (ackBuf, recvAddr)ġ02 103 print( " (%d) times received data " % locPackNum)ġ04 105 # recognized as the last packet 106 if recvDataLen < 516 :ġ08 print( " has completed downloading " )ġ09 break 110 111 elif cmd = 5: # Whether an error response 112 print( " error num:%d " % curPackNum)ġ16 """ the TFTP upload """ 117 print( " upload start. 79 saveFile = '' # save the file handle 80 while True:Ĩ1 recvData, recvAddr = (1024 )Ĩ3 84 # 解包 85 cmdTuple = struct.unpack(b " !HH ", recvData)Ĩ6 cmd = cmdTuple # Command 87 curPackNum = cmdTuple # current packet No. " )Ħ7 68 # build data download request 69 # first parameter! H7sb5sb ="! H "+ STR (len (filename)) +" sb5sb " 70 filenameLen = str(len(self.filename))ħ1 cmdBuf = struct.pack(( " !H%ssb5sb " % filenameLen).encode( " utf-8 "), 1, ( " utf-8 "), 0, b " octet ", 0)ħ2 73 # Send download file request data to the specified server 74 (cmdBuf, (self.remoteIp, 69 ))ħ5 76 # self.show() 77 78 locPackNum = 0 # request packet No. Mainly analyze the first parameter fmt: such as "!H7sb5sb" => įmt explains the next few parameters, where H represents 1, 7s represents a string of length 7, etc.ġ # -*- coding:utf-8 -*- 2 3 """ 4 Realize TFTP upload and download functionsĥ Need to cooperate with tftpd software testġ6 -ġ8 -Ģ5 -Ģ8 -ģ1 3 Represents the data packet, that is, the Dataģ4 -ģ7 # read parameter 38 if len(sys.argv) != 4 :Ĥ1 print( " python xxx.py 1 127.0.0.1 test.png " )Ĥ5 self.mid = sys.argv # implemented method, a download or upload 2 46 self.remoteIp = sys.argv # server the IP 47 self.filename = sys.argv # the download file name 48 49 # Create instance socket 50 self.socketClient = socket(AF_INET, SOCK_DGRAM)ĥ4 """ initiate execution """ 55 if self.mid = " 1 " :Ħ1 print( " Parameter input error : python xxx.py 1 127.0.0.1 test.png " )Ħ5 """ TFTP download """ 66 print( " Download starts. About the parameter description of struct.pack() and struct.unpack(): Run the script and pass in two parameters: server IP and file nameĤ. The running tftpd server is as follows:Ĭhoose the download path and configure IP program operation diagram and analysis fmtġ. After so many times, when the client file is completed, the upload can be exited, and the upload is complete.ģ. The basic upload process: the client sends a write request (operation code 2) to the server, if the upload can be performed, the server will return an ACK response packet, and the client can send the first data packet after receiving it, and then serve After receiving it, the client will return an ACK to be packaged. TFTP upload process analysis (a simple file description is made here, you can refer to the source code below or search by yourself): Then send the ACK response packet to the server, so many times, one send and one answer, that is, the file download is realized.ĥ. The client sends the file name to be downloaded to the server for the first time, (the file exists) the server will return the first package of the file after receiving it, and the client will save it locally after receiving it. The downloading data flow process is shown in the figure above. When opening a tftpd as a server, it will listen to port 69 by default, so the client sends data to the server through port 69. TFTP (Trivial File Transfer Protocol) is a protocol in the TCP/IP protocol suite for file transfer between the client and the server (C/S architecture). Introduction of TFTP protocol (refer to network, search for details) Main content: Introduction of TFTP protocol, program operation diagram and analysis of fmt, source code.Ģ. Tftpd download: choose to download according to your own environment, address: Test environment: Win10/Python3.5/tftpd64. ![]() This article mainly implements TFTP file upload and download based on socket.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |