-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
495 lines (445 loc) · 16.2 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#!/usr/bin/env python
# File: client.py
# Run with: "python ./client.py"
# Tested with: Python 2.7.3
# Cite: http://goo.gl/MuP4t
# Cite: http://goo.gl/Ql3f5
# Public Domain
import socket
import errno
import random
import sys
from multiprocessing import Process
import datetime
import signal
import os
import glob
from collections import namedtuple
from datetime import date
DEBUG = False
VERSION = "P2P-CI/1.0"
files = []
sport = 7734# Server's well-known port
OS = sys.platform
watch_dir = "./watch"
peer_rfc = namedtuple("peer_rfc", ["rfc_num", "rfc_title", "hostname", "port"])
rfc = namedtuple("rfc", ["num", "title", "mtime", "size", "data"])
# Find my IP
# Cite: http://is.gd/gLzpdS
stmp = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
stmp.connect(("gmail.com",80))
me = stmp.getsockname()[0]
stmp.close()
if DEBUG:
print "PEER: You are in debug mode"
# Setup signal SIGCHLD handler
signal.signal(signal.SIGINT, signal.SIG_IGN)
def SIGCHLD_handler(signal, frame):
"""Check exit status of children to avoid zombies"""
try:
tpid = os.waitpid(0, os.WNOHANG)
except:
if DEBUG:
print "PEER: Caught an error in SIGCHLD handler"
return
if DEBUG:
print "PEER: Child process with PID", tpid, "closed"
signal.signal(signal.SIGCHLD, SIGCHLD_handler)
# Discover RFCs
if not os.path.exists(watch_dir):
print "No watch directory exists.",
os.makedirs(watch_dir)
print "A new watch directory has been created."
os.chdir(watch_dir)
# Note: The modified time seems to not be very precise
def update_files():
"""Collect info of files in watch_dir"""
global files
files = []
for f in glob.glob("*"):
statbuf = os.stat(f)
fd = open(f, 'r')
files.append(rfc(num = f.split()[0], title = f.split()[1], \
mtime = statbuf.st_mtime, size = statbuf.st_size, data = fd.read()))
fd.close()
update_files()
def get_rfc(rfc_num):
"""Get the details of the RFC with the given number"""
global files
for i in range(len(files)):
if files[i].num == rfc_num:
return files[i]
return ""
# Spawn upload server process
uport = random.randint(45000, 60000)# Upload port
def ul_server(uport):
"""Manage all requests from peers"""
# Listen on uport
s = socket.socket()
host = socket.gethostname()
s.bind((me, uport))
s.listen(5)
print "Upload server started, listening on " + me + ":" + str(uport)
while True:
# Get message
try:
con, addr = s.accept()
except socket.error:
continue
except KeyboardInterrupt:
print "\nPEER: Shutting down"
s.close
sys.exit(0)
message = con.recv(4096)
marray = message.split()
if DEBUG:
print "PEER FROM PEER:\n" + message,
# Validate message from peer
if marray[0] != 'GET' or marray[1] != "RFC" or marray[4] != 'Host:' \
or marray[6] != 'OS:':
con.send(VERSION + "400 Bad Request\r\nDate: "
+ str(datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT"))
+ "\r\nOS: " + OS)
if DEBUG:
print "PEER: Message was invalid"
continue
if marray[3] != VERSION:
con.send(VERSION + "505 P2P-CI Version Not Supported\r\nDate: "
+ str(datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT"))
+ "\r\nOS: " + OS)
if DEBUG:
print "PEER: Version is not supported"
continue
if DEBUG:
print "PEER: Message is valid"
# Respond to peer
resp_rfc = get_rfc(rfc_num = marray[2])
if resp_rfc != "":
if DEBUG:
print "PEER: Sending RFC"
con.send(VERSION + " 200 OK\r\nDate: "
+ str(datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT"))
+ "\r\nOS: " + OS + "\r\nLast-Modified: "
+ str(date.fromtimestamp(float(resp_rfc.mtime)).strftime("%a, %d %b %Y %H:%M:%S GMT"))
+ "\r\nContent-Length: " + str(resp_rfc.size)
+ "\r\nContent-Type: text/plain\r\n\r\n" + str(resp_rfc.data))
if DEBUG:
print "PEER: Response sent\n=> ",
continue
else:
if DEBUG:
print "PEER: RFC not found, sending 404"
con.send(VERSION + " 404 Not Found\r\nDate: "
+ str(datetime.datetime.utcnow().strftime("%a, %d %b %Y %H:%M:%S GMT"))
+ "\r\nOS: " + OS)
continue
p = Process(target = ul_server, args = (uport,))
p.daemon = True
p.start()
# Open server connection
s = socket.socket()
server = "10.0.0.3"
try:
s.connect((server, sport))
except socket.error, e:
if e.errno == errno.ECONNREFUSED:
print "PEER ERROR: There is no server on", str(server) + ":" \
+ str(sport)
s.close()
p.terminate()
sys.exit(1)
else:
print "PEER ERROR: There was an IOError", e.errno
# Define messages to server
def do_add(rfc_num, title, host = me, port = uport):
"""Add a locally available RFC to the server's index"""
try:
s.send("ADD RFC " + str(rfc_num) + " " + VERSION + "\r\n" + "Host: "
+ str(host) + "\r\n" + "Port: " + str(port) + "\r\n" + "Title: "
+ str(title) + "\r\n\r\n")
except IOError, e:
if e.errno == errno.EPIPE:
print "PEER ERROR: There is no server on", str(server) + ":" \
+ str(sport)
s.close()
p.terminate()
sys.exit(1)
else:
print "PEER ERROR: There was an IOError", e.errno
response = str(s.recv(4096))
if DEBUG:
print "PEER FROM SERVER:\n" + response,
# Validate response
rarray = response.split()
if rarray[0] == VERSION and rarray[1] == "200" and rarray[2] == "OK":
return
elif rarray[0] == VERSION and rarray[1] == "400" and rarray[2] == "Bad" \
and rarray[3] == "Request":
print "Server responded with a '400 Bad Request'"
elif rarray[0] != VERSION:
print "Unsupported server version", rarray[0]
else:
print "Unknown response from server:\n" + response,
def do_lookup(rfc_num, title, host = me, port = uport):
"""Find peers that have the specified RFC"""
try:
s.send("LOOKUP RFC " + str(rfc_num) + " " + VERSION + "\r\n" + "Host: "
+ str(host) + "\r\n" + "Port: " + str(port) + "\r\n" + "Title: "
+ str(title) + "\r\n\r\n")
except IOError, e:
if e.errno == errno.EPIPE:
print "PEER ERROR: There is no server on", str(server) + ":" \
+ str(sport)
s.close()
p.terminate()
sys.exit(1)
else:
print "PEER ERROR: There was an IOError", e.errno
response = str(s.recv(4096))
if DEBUG:
print "PEER FROM SERVER:\n" + response,
# Validate response
rarray = response.split()
# If RFC found
if rarray[0] == VERSION and rarray[1] == "200" and rarray[2] == "OK":
# Display RFCs to user
rfcs = response.split("200 OK\r\n\r\n")[1]
return peer_rfc(rfc_num = rarray[3], rfc_title = rarray[4], \
hostname = rarray[5], port = rarray[6])
# If RFC not found
elif rarray[0] == VERSION and rarray[1] == "404" and rarray[2] == "Not" \
and rarray[3] == "Found":
return ""
elif rarray[0] == VERSION and rarray[1] == "400" and rarray[2] == "Bad" \
and rarray[3] == "Request":
print "Server responded with a '400 Bad Request'"
elif rarray[0] != VERSION:
print "Unsupported server version", rarray[0]
else:
print "Unknown response from server:\n" + response,
return ""
def do_list(host = me, port = uport):
"""Request the whole index of RFCs from the server"""
try:
s.send("LIST ALL " + VERSION + "\r\n" + "Host: " + str(host) + "\r\n" \
+ "Port: " + str(port) + "\r\n\r\n")
except IOError, e:
if e.errno == errno.EPIPE:
print "PEER ERROR: There is no server on", str(server) + ":" \
+ str(sport)
s.close()
p.terminate()
sys.exit(1)
else:
print "PEER ERROR: There was an IOError", e.errno
response = str(s.recv(4096))
if DEBUG:
print "PEER FROM SERVER:\n" + response,
# Validate response
rarray = response.split()
# If there are RFCs
if rarray[0] == VERSION and rarray[1] == "200" and rarray[2] == "OK":
# Display RFCs to user
rfcs = response.split("200 OK\r\n\r\n")[1]
print rfcs[:len(rfcs)-1],
# If there are no RFCs
elif rarray[0] == VERSION and rarray[1] == "404" and rarray[2] == "Not" \
and rarray[3] == "Found":
print "Server knows of no RFCs"
elif rarray[0] == VERSION and rarray[1] == "400" and rarray[2] == "Bad" \
and rarray[3] == "Request":
print "Server responded with a '400 Bad Request'"
elif rarray[0] != VERSION:
print "Unsupported server version", rarray[0]
else:
print "Unknown response from server:\n" + response,
def do_hello(host = me, OS = OS, uport = uport):
"""Send HELLO message to server, including hostname and upload port"""
try:
s.send("HELLO " + VERSION + "\r\n" + "Host: " \
+ str(host) + "\r\n" + "OS: " + str(OS) + "\r\n" + "Upload port: " \
+ str(uport) + "\r\n\r\n")
except IOError, e:
if e.errno == errno.EPIPE:
print "PEER ERROR: There is no server on", str(server) + ":" \
+ str(sport)
s.close()
p.terminate()
sys.exit(0)
else:
print "PEER ERROR: There was an IOError", e.errno
response = str(s.recv(4096))
if DEBUG:
print "PEER FROM SERVER:\n" + response,
# Validate response
rarray = response.split()
if rarray[0] == VERSION and rarray[1] == "200" and rarray[2] == "OK":
return
elif rarray[0] == VERSION and rarray[1] == "400" and rarray[2] == "Bad" \
and rarray[3] == "Request":
print "Server responded with a '400 Bad Request'"
elif rarray[0] != VERSION:
print "Unsupported server version", rarray[0]
else:
print "Unknown response from server:\n" + response,
def do_goodbye(host = me, OS = OS, uport = uport):
"""Send GOODBYE message to server, including hostname and upload port"""
try:
s.send("GOODBYE " + VERSION + "\r\n" + "Host: " \
+ str(host) + "\r\n" + "OS: " + str(OS) + "\r\n" + "Upload port: " \
+ str(uport) + "\r\n\r\n")
except IOError, e:
if e.errno == errno.EPIPE:
print "PEER ERROR: There is no server on", str(server) + ":" \
+ str(sport)
s.close()
p.terminate()
sys.exit(1)
else:
print "PEER ERROR: There was an IOError", e.errno
response = str(s.recv(4096))
if response == "":
print "PEER: The server has already left"
elif DEBUG:
print "PEER FROM SERVER: \n" + response,
# Validate response
rarray = response.split()
if rarray[0] == VERSION and rarray[1] == "200" and rarray[2] == "OK":
return
elif rarray[0] == VERSION and rarray[1] == "400" and rarray[2] == "Bad" \
and rarray[3] == "Request":
print "Server responded with a '400 Bad Request'"
elif rarray[0] != VERSION:
print "Unsupported server version", rarray[0]
else:
print "Unknown response from server:\n" + response,
# Define messages to peers
def do_get(rfc_num, rfc_title, host = me, OS = OS):
"""Request an RFC from a peer"""
# Check if peer already has this RFC
my_rfc = get_rfc(rfc_num)
if my_rfc != "":
print "You already have RFC", rfc_num
return
# Get hostname and upload port of some peer with RFC
prfc = do_lookup(rfc_num, rfc_title)
if prfc == "":
print "The server doesn't know where to find RFC", rfc_num
return
# Open connection to peer's upload port
p = socket.socket()
try:
print "Connecting to " + prfc.hostname + ":" + str(prfc.port)
p.connect((prfc.hostname, int(prfc.port)))
except socket.error, v:
print "PEER ERROR: There was a socket error"
errorcode = v[0]
if errorcode == errno.ECONNREFUSED:
print "PEER ERROR: There is no peer on " + prfc.hostname + ":" \
+ str(prfc.port)
return
# Send GET to peer
try:
p.send("GET RFC " + str(prfc.rfc_num) + " " + VERSION + "\r\n" \
+ "Host: " + str(prfc.hostname) + "\r\n" + "OS: " + str(OS) \
+ "\r\n\r\n")
except IOError, e:
if e.errno == errno.EPIPE:
print "PEER ERROR: There is no peer on", prfc.hostname + ":" \
+ str(prfc.port)
p.close()
return
else:
print "PEER ERROR: There was an IOError", e.errno
# Receive response
response = str(p.recv(4096))
if DEBUG:
print "PEER FROM PEER: \n" + response,
p.close()
# Validate response
rarray = response.split()
if rarray[0] == VERSION and rarray[1] == "200" and rarray[2] == "OK" \
and rarray[3] == "Date:" and rarray[10] == "OS:" \
and rarray[12] == "Last-Modified:" and rarray[19] == "Content-Length:" \
and rarray[21] == "Content-Type:" and rarray[22] == "text/plain":
pass
elif rarray[0] == VERSION and rarray[1] == "400" and rarray[2] == "Bad" \
and rarray[3] == "Request":
print "Peer responded with a '400 Bad Request'"
return
elif rarray[0] != VERSION:
print "Unsupported peer version", rarray[0]
return
else:
print "Unknown response from peer:\n" + response,
return
# Generate RFC file from response
new_rfc = response.split("Content-Type: text/plain\r\n\r\n")[1]
with open(str(rfc_num) + " " + rfc_title, "w") as fd:
fd.write(new_rfc)
fd.close()
# Update RFC collection and inform server of new RFC
update_files()
new_rfc = get_rfc(str(rfc_num))
do_add(new_rfc.num, new_rfc.title)
print "Added RFC", new_rfc.num, new_rfc.title
def SIGINT_handler(signal, frame):
"""Close down properly when SIGINT is thrown"""
print "\nShutting down client"
do_goodbye()
s.close
p.terminate()
sys.exit(0)
signal.signal(signal.SIGINT, SIGINT_handler)
# Send peer's hostname and upload port to server
do_hello()
# Send peer's RFCs
for i in range(len(files)):
do_add(files[i].num, files[i].title)
# Take commands from user via CLI and send messages to server and peers
while True:
# Take command from user
command = raw_input("=> ")
if command == "":
continue
command = command.lower().split()
# Handle command
if command[0] == "exit":
if len(command) != 1:
print "Usage: exit"
continue
print "Shutting down client"
do_goodbye()
s.close
p.terminate()
sys.exit(0)
elif command[0] == "get":
if len(command) != 3:
print "Usage: get <rfc_num> <rfc_title>"
continue
do_get(command[1], command[2])
elif command[0] == "list":
if len(command) != 1:
print "Usage: list"
continue
do_list()
elif command[0] == "lookup":
if len(command) != 3:
print "Usage: lookup <rfc_num> <rfc_title>"
continue
rrfc = do_lookup(command[1], command[2])
if rrfc == "":
print "The server doesn't know where to find RFC", command[1]
continue
print rrfc.rfc_num, rrfc.rfc_title, rrfc.hostname, rrfc.port
elif command[0] == "help":
if len(command) != 1:
print "Usage: help"
continue
print "help\t\t\t\t: Print this help message"
print "exit\t\t\t\t: Close the peer's connections and process"
print "get <rfc_num> <rfc_title>\t: Download the given RFC"
print "lookup <rfc_num> <rfc_title>\t: Get info of peers with given RFC"
print "list\t\t\t\t: Get info about all RFCs known to server"
else:
print "Invalid command, see 'help'"