-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
149 lines (140 loc) · 4.27 KB
/
server.py
File metadata and controls
149 lines (140 loc) · 4.27 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/usr/bin/env python
import socket
import selectors
import types
import sys
import os
# Create a TCP server
def tcp_server(addr, sel):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(addr)
s.setblocking(False)
data = types.SimpleNamespace(type='conn', addr=addr, inb=b'', outb=b'', end=False, client=None)
sel.register(s, selectors.EVENT_READ, data=data)
s.listen()
print('Listening on', addr)
return s
# Accept FTP Control connection
def ftpctrl_accept(s):
conn, addr = s.accept()
print()
print('Got a control connection', addr)
conn.setblocking(False)
data = types.SimpleNamespace(type='ctrl', addr=addr, inb=b'', outb=b'', end=False, client=None)
events = selectors.EVENT_READ | selectors.EVENT_WRITE
sel.register(conn, events, data=data)
# Service FTP Control connection
def ftpctrl_service(key, mask):
conn = key.fileobj
data = key.data
if mask & selectors.EVENT_READ:
recv_data = conn.recv(1024)
if recv_data:
data.inb += recv_data
ftpctrl_process(data)
else:
print('Connection broke', data.addr)
sel.unregister(conn)
conn.close()
if mask & selectors.EVENT_WRITE:
if data.outb:
print('Sending', len(data.outb), 'bytes to', data.addr)
sent = conn.send(data.outb)
data.outb = data.outb[sent:]
# Process FTP Control commands
def ftpctrl_process(data):
while b'\r\n' in data.inb:
line_end = data.inb.index(b'\r\n')
line = data.inb[0:line_end].decode('ascii')
print(line)
data.inb = data.inb[line_end+2:]
args = line.split(' ')
if args[0]=='PASV':
resp = '227 Entering Passive Mode (127,0,0,1,{},{})\r\n'.format(port_data//256, port_data%256)
data.outb += resp.encode('ascii')
shared.server = data
elif args[0]=='RETR':
if data.client is not None:
data.client.end = True
else:
print('Error: No client to retrieve!')
break
path = args[1]
if path[0:1]=='/':
path = path[1:]
if len(path)==0:
path = 'index.html'
if os.path.isfile(path):
print('Retrieving', path)
file_id = open(path, 'rb')
data.client.outb += file_id.read()
file_id.close()
data.outb += b'226 Closing data connection\r\n'
else:
print('File', path, 'not found')
data.outb += b'426 Connection closed; transfer aborted\r\n'
# Accept FTP Data connection
def ftpdata_accept(s):
conn, addr = s.accept()
print('Got a data connection', addr)
conn.setblocking(False)
data = types.SimpleNamespace(type='data', addr=addr, inb=b'', outb=b'', end=False, client=None)
events = selectors.EVENT_READ | selectors.EVENT_WRITE
sel.register(conn, events, data=data)
if shared.server is None:
sel.unregister(conn)
conn.close()
else:
shared.server.client = data
shared.server = None
# Service FTP Data connection
def ftpdata_service(key, mask):
conn = key.fileobj
data = key.data
if mask & selectors.EVENT_READ:
recv_data = conn.recv(1024)
if recv_data:
print('Recieved from', data.addr, recv_data)
else:
print('Connection broke', data.addr)
sel.unregister(conn)
conn.close()
if mask & selectors.EVENT_WRITE:
if data.outb:
print('Sending', len(data.outb), 'bytes to', data.addr)
sent = conn.send(data.outb)
data.outb = data.outb[sent:]
elif data.end:
print('Closing connection', data.addr)
sel.unregister(conn)
conn.close()
# Main
port_ctrl = 2001
if len(sys.argv)>1:
port_ctrl = int(float(sys.argv[1]))
port_data = port_ctrl-1
if len(sys.argv)>2:
port_data = int(float(sys.argv[2]))
print('Starting FTP on {}, {} (data)'.format(port_ctrl, port_data))
shared = types.SimpleNamespace(server=None)
addr_ctrl = ('', port_ctrl)
addr_data = ('', port_data)
sel = selectors.DefaultSelector()
sel = selectors.DefaultSelector()
s_ctrl = tcp_server(addr_ctrl, sel)
s_data = tcp_server(addr_data, sel)
while True:
events = sel.select(timeout=None)
for key, mask in events:
data = key.data
if data.type=='conn':
if data.addr==addr_ctrl:
ftpctrl_accept(key.fileobj)
else:
ftpdata_accept(key.fileobj)
elif data.type=='ctrl':
ftpctrl_service(key, mask)
elif data.type=='data':
ftpdata_service(key, mask)
else:
print('Error: Unknown event!')