-
Notifications
You must be signed in to change notification settings - Fork 0
/
TFTP_Client.py
44 lines (36 loc) · 1.83 KB
/
TFTP_Client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""This file provides convenience methods for client request and response processing.
It will be replaced with a robust TFTPClient in the future.
"""
import socket
import Packet
import TFTPCommon
def read(from_addr, to_addr, filename, mode):
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
packet_size: int = TFTPCommon.TFTP_MAX_PACKET_SIZE
# send initial request
sock.bind(from_addr)
rrq_pkt = Packet.RRQPacket(filename, mode)
sock.sendto(rrq_pkt.create_bytes(), to_addr)
# Recieve Data and send Ack loop
TFTPCommon.receive_file(filename, mode, to_addr, sock, isServer=False)
# with open(filename, "wb") as f:
# print("opened new file: " + filename)
# while packet_size == TFTPCommon.TFTP_MAX_PACKET_SIZE:
# (packet, addr) = s.recvfrom(TFTPCommon.TFTP_MAX_PACKET_SIZE)
# print("received data packet" )
# packet_size = len(packet)
# data_pkt = Packet.DATAPacket.create_from_bytes(packet)
# f.write(data_pkt.data)
# s.sendto(Packet.ACKPacket(data_pkt.block_num).create_bytes(), addr)
# print("sent ACK packet")
def write(from_addr, to_addr, filename, mode):
print(filename)
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as sock:
packet_size: int = TFTPCommon.TFTP_MAX_HEADER_SIZE + TFTPCommon.TFTP_MAX_DATA_SIZE
sock.bind(from_addr)
# send initial request
wrq_pkt = Packet.WRQPacket(filename, mode)
sock.sendto(wrq_pkt.create_bytes(), to_addr)
(pkt, addr) = sock.recvfrom(packet_size)
#assume packet is ACK packet
TFTPCommon.send_file(filename, mode, addr, sock)