type
status
date
slug
tags
category
summary
icon
password
sourceUrl
Property
Created time
Aug 13, 2024 07:09 AM
本代码运行环境:python3 + Ubuntu161:运行代码前需要运行tftp服务器端,把要上传的文件放在和执行文件同一个文件夹里2:tftp服务器端ip要和ubuntu ip在同一网段
相关知识可以参考上一篇文章
粗糙的实现代码如下
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# coding=utf-8
from socket import *
import struct
import sys
# 接收命令传进来的参数
if len(sys.argv) != 2:
print('-'*30)
print('run tips:')
print('python3 filename destination_ip')
print('-'*30)
exit()
else:
ip = sys.argv[1]
tftp_udp = socket(AF_INET, SOCK_DGRAM)
tftp_data = struct.pack("!H8sb5sb", 1, b"test.jpg", 0, b"octet", 0) # python3
# tftp_data = struct.pack("!H8sb5sb", 1, "test.jpg", 0, "octet", 0) # python
# 发送下载请求
bind_address = (ip, 69)
tftp_udp.sendto(tftp_data, bind_address)
receive_file = ''
num = 1
# 接收数据
while True:
receive_data, receive_address = tftp_udp.recvfrom(1024)
data_len = len(receive_data)
data_unpack = struct.unpack('!HH', (receive_data[:4]))
# 数据包拆分
pack_num = data_unpack[0] # 操作码
return_num = data_unpack[1] # 块编号
# 判断数据操作码,进行操作
# 操作码3,下载数据
if pack_num == 3:
if return_num == 1:
receive_file = open('test.jpg', 'ba')
if num == return_num:
receive_file.write(receive_data[4:])
print('\r(%d)receive file' % num, end='')
# 构建ack确认接收成功
return_ack = struct.pack("!HH", 4, num)
tftp_udp.sendto(return_ack, receive_address) # 发送确认
num += 1
if data_len < 516:
receive_file.close()
print('Download successful !')
break
# 操作码5,错误
if pack_num == 5:
print('error num: %d' % return_num)
tftp_udp.close()
# coding=utf-8 from socket import * import struct import sys # 接收命令传进来的参数 if len(sys.argv) != 2: print('-'*30) print('run tips:') print('python3 filename destination_ip') print('-'*30) exit() else: ip = sys.argv[1] tftp_udp = socket(AF_INET, SOCK_DGRAM) tftp_data = struct.pack("!H8sb5sb", 1, b"test.jpg", 0, b"octet", 0) # python3 # tftp_data = struct.pack("!H8sb5sb", 1, "test.jpg", 0, "octet", 0) # python # 发送下载请求 bind_address = (ip, 69) tftp_udp.sendto(tftp_data, bind_address) receive_file = '' num = 1 # 接收数据 while True: receive_data, receive_address = tftp_udp.recvfrom(1024) data_len = len(receive_data) data_unpack = struct.unpack('!HH', (receive_data[:4])) # 数据包拆分 pack_num = data_unpack[0] # 操作码 return_num = data_unpack[1] # 块编号 # 判断数据操作码,进行操作 # 操作码3,下载数据 if pack_num == 3: if return_num == 1: receive_file = open('test.jpg', 'ba') if num == return_num: receive_file.write(receive_data[4:]) print('\r(%d)receive file' % num, end='') # 构建ack确认接收成功 return_ack = struct.pack("!HH", 4, num) tftp_udp.sendto(return_ack, receive_address) # 发送确认 num += 1 if data_len < 516: receive_file.close() print('Download successful !') break # 操作码5,错误 if pack_num == 5: print('error num: %d' % return_num) tftp_udp.close()
# coding=utf-8
from socket import *
import struct
import sys
# 接收命令传进来的参数
if len(sys.argv) != 2:
print('-'*30)
print('run tips:')
print('python3 filename destination_ip')
print('-'*30)
exit()
else:
ip = sys.argv[1]
tftp_udp = socket(AF_INET, SOCK_DGRAM)
tftp_data = struct.pack("!H8sb5sb", 1, b"test.jpg", 0, b"octet", 0) # python3
# tftp_data = struct.pack("!H8sb5sb", 1, "test.jpg", 0, "octet", 0) # python
# 发送下载请求
bind_address = (ip, 69)
tftp_udp.sendto(tftp_data, bind_address)
receive_file = ''
num = 1
# 接收数据
while True:
receive_data, receive_address = tftp_udp.recvfrom(1024)
data_len = len(receive_data)
data_unpack = struct.unpack('!HH', (receive_data[:4]))
# 判断数据操作码,进行操作
# 操作码3,下载数据
if pack_num == 3:
if return_num == 1:
receive_file = open('test.jpg', 'ba')
if num == return_num:
receive_file.write(receive_data[4:])
print('\r(%d)receive file' % num, end='')
# 构建ack确认接收成功
return_ack = struct.pack("!HH", 4, num)
tftp_udp.sendto(return_ack, receive_address) # 发送确认
num += 1
if data_len < 516:
receive_file.close()
print('Download successful !')
break
# 操作码5,错误
if pack_num == 5:
print('error num: %d' % return_num)
tftp_udp.close()
- 作者:学长的猫
- 链接:https://gpt123.eu.org/article/2497
- 声明:本文采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。