python實(shí)現(xiàn)簡(jiǎn)單tftp(基于udp協(xié)議)
本文實(shí)例為大家分享了python實(shí)現(xiàn)簡(jiǎn)單tftp的具體代碼,供大家參考,具體內(nèi)容如下

- tftp是基于udp的協(xié)議
- 實(shí)現(xiàn)簡(jiǎn)單的tftp,首先要有tftp的協(xié)議圖。
- tftp默認(rèn)接收端口為69,但每次有連接過(guò)來(lái)后,tftp會(huì)隨機(jī)分配一個(gè)端口來(lái)專(zhuān)門(mén)為這個(gè)連接來(lái)服務(wù)。
- 操作碼:1.上傳 2.下載 3.傳數(shù)據(jù) 4.接收確認(rèn) 5.錯(cuò)誤碼
tftp服務(wù)器簡(jiǎn)單實(shí)現(xiàn):
from threading import Thread
from socket import *
import struct
def upload(filename,user_ip,user_port):
num = 0
f = open(filename,'ab')
s_up = socket(AF_INET,SOCK_DGRAM)
send_data_1 = struct.pack("!HH",4,num)
s_up.sendto(send_data_1,(user_ip,user_port)) #第一次用隨機(jī)端口發(fā)送
while True:
recv_data,user_info = s_up.recvfrom(1024) #第二次客戶(hù)連接我隨機(jī)端口
caozuohao_up,ack_num = struct.unpack('!HH',recv_data[:4])
print(caozuohao_up,ack_num,num)
if int(caozuohao_up) == 3 and ack_num == num :
f.write(recv_data[4:])
send_data = struct.pack("!HH",4,num)
s_up.sendto(send_data,(user_ip,user_port)) #第二次我用隨機(jī)端口發(fā)
num = num + 1
if len(recv_data) < 516:
print(user_ip+'上傳文件'+filename+':完成')
f.close()
exit()
def download(filename,user_ip,user_port):
s_down = socket(AF_INET, SOCK_DGRAM)
num = 0
try:
f = open(filename,'rb')
except:
error_data = struct.pack('!HHHb',5,5,5,num)
s_down.sendto(error_data, (user_ip,user_port)) #文件不存在時(shí)發(fā)送
exit() #只會(huì)退出此線程
while True:
read_data = f.read(512)
send_data = struct.pack('!HH',3,num) + read_data
s_down.sendto(send_data, (user_ip,user_port)) #數(shù)據(jù)第一次發(fā)送
if len(read_data) < 512:
print('傳輸完成, 對(duì)方下載成功')
exit()
recv_ack = s_down.recv(1024) #第二次接收
caozuoma,ack_num = struct.unpack("!HH", recv_ack)
# print(caozuoma,ack_num,len(read_data))
num += 1
if int(caozuoma) != 4 or int(ack_num) != num-1 :
exit()
f.close()
s = socket(AF_INET,SOCK_DGRAM)
s.bind(('',69))
def main():
while 1:
recv_data,(user_ip,user_port) = s.recvfrom(1024) #第一次客戶(hù)連接69端口
print(recv_data, user_ip, user_port)
if struct.unpack('!b5sb',recv_data[-7:]) == (0, b'octet', 0):
caozuoma = struct.unpack('!H',recv_data[:2])
filename = recv_data[2:-7].decode('gb2312')
if caozuoma[0] == 1:
print('對(duì)方想下載數(shù)據(jù)',filename)
t = Thread(target = download, args = (filename,user_ip,user_port))
t.start()
elif caozuoma[0] == 2:
print('對(duì)方想上傳數(shù)據(jù)',filename)
t = Thread(target = upload, args = (filename,user_ip,user_port))
t.start()
if __name__ == '__main__':
main()
上傳數(shù)據(jù)簡(jiǎn)單實(shí)現(xiàn):
#!/usr/bin/env python3
#coding=utf-8
import struct
from socket import *
server_ip = '192.168.119.157'
send_data_1 = struct.pack('!H8sb5sb',2,'王輝.jpg'.encode('gb2312'),0,b'octet',0)
s = socket(AF_INET,SOCK_DGRAM)
s.sendto(send_data_1,(server_ip,69)) #第一次發(fā)給服務(wù)器69端口
f = open('王輝.jpg','rb')
recv_data = s.recvfrom(1024) #第一次接收數(shù)據(jù)
rand_port = recv_data[1][1]
print()
ack_num = struct.unpack("!HH",recv_data[0][:4])
num = 0
while True:
read_data = f.read(512)
send_data = struct.pack('!HH',3,num) + read_data
s.sendto(send_data,(server_ip,rand_port)) #第二次發(fā)給服務(wù)器的隨機(jī)端口
recv_data_2,userinfo = s.recvfrom(1024)
print(recv_data_2)
ack_num = struct.unpack('!H',recv_data_2[2:4])
print(len(read_data),num,ack_num[0],rand_port)
if len(read_data) < 512 or ack_num[0] != num :
break
num = num + 1
下載數(shù)據(jù)簡(jiǎn)單實(shí)現(xiàn):
#!/usr/bin/env python3
#coding=utf-8
import struct
from socket import *
filename = 'test.jpg'
server_ip = '192.168.1.113'
send_data = struct.pack('!H%dsb5sb'%len(filename),1,filename.encode('gb2312'),0,'octet'.encode('gb2312'),0)
s = socket(AF_INET,SOCK_DGRAM)
s.sendto(send_data,(server_ip,69)) #第一次發(fā)送, 連接服務(wù)器69端口
f = open(filename,'ab')
while 1:
recv_data = s.recvfrom(1024) #接收數(shù)據(jù)
caozuoma,ack_num = struct.unpack('!HH',recv_data[0][:4]) #獲取數(shù)據(jù)塊編號(hào)
rand_port = recv_data[1][1] #獲取服務(wù)器的隨機(jī)端口
if int(caozuoma) == 5:
print('服務(wù)器返回: 文件不存在...')
break
print(caozuoma,ack_num,rand_port,len(recv_data[0]))
f.write(recv_data[0][4:])
if len(recv_data[0]) < 516:
break
ack_data = struct.pack("!HH",4,ack_num)
s.sendto(ack_data,(server_ip,rand_port)) #回復(fù)ACK確認(rèn)包
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)UDP協(xié)議下的文件傳輸
- python網(wǎng)絡(luò)編程 使用UDP、TCP協(xié)議收發(fā)信息詳解
- python UDP(udp)協(xié)議發(fā)送和接收的實(shí)例
- 使用python編寫(xiě)udp協(xié)議的ping程序方法
- Python實(shí)現(xiàn)基于TCP UDP協(xié)議的IPv4 IPv6模式客戶(hù)端和服務(wù)端功能示例
- Python網(wǎng)絡(luò)編程之TCP與UDP協(xié)議套接字用法示例
- Python+Socket實(shí)現(xiàn)基于UDP協(xié)議的局域網(wǎng)廣播功能示例
- python3實(shí)現(xiàn)UDP協(xié)議的服務(wù)器和客戶(hù)端
- python實(shí)現(xiàn)的udp協(xié)議Server和Client代碼實(shí)例
- python 基于UDP協(xié)議套接字通信的實(shí)現(xiàn)
相關(guān)文章
python二維列表一維列表的互相轉(zhuǎn)換實(shí)例
今天小編就為大家分享一篇python二維列表一維列表的互相轉(zhuǎn)換實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07
Python新手必讀bytearray對(duì)象使用技巧掌握
Python中的bytearray是一個(gè)可變序列,通常用于存儲(chǔ)二進(jìn)制數(shù)據(jù),它允許在不創(chuàng)建新的對(duì)象的情況下就地修改數(shù)據(jù),非常適用于處理字節(jié)數(shù)據(jù),本文將深入學(xué)習(xí)bytearray對(duì)象的使用,包括創(chuàng)建、修改、切片和常見(jiàn)應(yīng)用場(chǎng)景2023-12-12
python實(shí)現(xiàn)股票歷史數(shù)據(jù)可視化分析案例
股票交易數(shù)據(jù)分析可直觀股市走向,對(duì)于如何把握股票行情,快速解讀股票交易數(shù)據(jù)有不可替代的作用,感興趣的可以了解一下2021-06-06
Python實(shí)現(xiàn)手繪圖效果實(shí)例分享
在本篇文章里小編給大家整理了關(guān)于Python實(shí)現(xiàn)手繪圖效果,有需要的朋友們可以學(xué)習(xí)下。2020-07-07
Python異常處理如何才能寫(xiě)得優(yōu)雅(retrying模塊)
異常就是程序運(yùn)行時(shí)發(fā)生錯(cuò)誤的信號(hào),下面這篇文章主要給大家介紹了關(guān)于Python異常處理的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-03-03
解決python xx.py文件點(diǎn)擊完之后一閃而過(guò)的問(wèn)題
今天小編就為大家分享一篇解決python xx.py文件點(diǎn)擊完之后一閃而過(guò)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-06-06

