-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
116 lines (100 loc) · 3.46 KB
/
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from http import client
import socket
class FTPClient():
def __init__(self, host, port):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
self.host = host
self.port = int(port)
def connect(self):
print('Starting connection to', self.host, ':', self.port)
self.sock.connect((self.host, self.port))
self.sock.recv(1024)
def run(self):
while True:
command = input('Enter a command: ')
if len(command) == 0:
print('Try again.')
continue
if 'HELP' in command:
self.HELP()
elif 'LIST' in command:
self.LIST()
elif 'PWD' in command:
self.PWD()
elif 'QUIT' in command:
self.QUIT()
elif 'CD' in command:
self.CD(command[3:].strip())
elif 'DWLD' in command:
self.DWLD(command[4:].strip())
else:
print('Incorrect command, Try again.')
continue
def new_passive_connection(self):
self.pasv()
self.passive_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.passive_socket.connect((self.host, self.passive_port))
def pasv(self):
self.sock.send(b'PASV\r\n')
reply = self.sock.recv(1024).decode()
reply = reply.strip().strip('('+').').split(',')
self.passive_port = int(reply[4])*256+int(reply[5])
def LIST(self):
print('Requesting files...')
self.new_passive_connection()
self.sock.send(b'LIST\r\n')
self.sock.recv(1024)
files = ""
while True:
data = self.passive_socket.recv(1024).decode()
if len(data) == 0:
break
files += data
print(files.strip())
self.sock.recv(1024)
self.passive_socket.close()
def DWLD(self, file_path):
print('Downloading', file_path, 'from server...')
self.new_passive_connection()
self.sock.send(f'RETR {file_path}\r\n'.encode())
self.sock.recv(1024)
with open(file_path, 'wb') as file:
while True:
data = self.passive_socket.recv(1024)
if len(data) == 0:
break
file.write(data)
self.passive_socket.close()
self.sock.recv(1024)
print('Successfully downloaded: ', file_path)
def PWD(self):
print('Requesting path...')
self.sock.send(b'PWD\r\n')
path = self.sock.recv(1024).decode()
print(path[4:].strip())
def CD(self, dir_name):
if dir_name == '..':
self.sock.send(b'CDUP\r\n')
else:
self.sock.send(f'CWD {dir_name}\r\n'.encode())
result = self.sock.recv(1024).decode()
if '550' in result:
print("Operation Failed")
else:
self.PWD()
def QUIT(self):
print('Closing socket connection')
self.sock.close()
quit()
def HELP(self):
print('Enter one of the following functions:')
print('HELP\t\t', ': Show this text')
print('LIST\t\t', ': List files')
print('PWD\t\t', ': Show current directory')
print('CD dir_name\t', ': change directory')
print('DWLD file_path\t', ': Download file')
print('QUIT\t\t', ': Exit')
return
client = FTPClient('127.0.0.1', '2121')
client.connect()
client.run()