-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathclient.py
executable file
·298 lines (260 loc) · 10.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
#!/usr/bin/python # This is client.py file
import socket # Import socket module
import time # Import time module
import platform # Import platform module to get our OS
import os
import pickle
import random
from _thread import *
#test
def p2p_get_request(rfc_num, peer_host, peer_upload_port):
s = socket.socket() # Create a socket object
s.connect((peer_host, int(peer_upload_port)))
data = p2p_request_message(rfc_num, host)
s.send(bytes(data, 'utf-8'))
data_rec= pickle.loads(s.recv(1024))
print("Data_rec", str(data_rec))
#my_data = data_rec.decode('utf-8')
my_data = data_rec[1]
print(my_data)
current_path = os.getcwd()
filename = "rfc"+rfc_num+".txt"
OS = platform.system()
if OS == "Windows": # determine rfc path for two different system
filename = current_path + "\\rfc\\" + filename
else:
filename = current_path + "/rfc/" + filename
#f = open(filename,'w')
with open(filename, 'w') as file:
file.write(my_data)
#f.write(data_rec.decode('utf-8'))
#f.close()
s.close()
# display p2p response message
def p2p_response_message(rfc_num): # the parameter "rfc_num" should be str
filename = "rfc"+str(rfc_num)+".txt"
current_time = time.strftime("%a, %d %b %Y %X %Z", time.localtime())
OS = platform.system()
m = filename.split()
filename = "".join(m)
current_path = os.getcwd()
if OS == "Windows": # determine rfc path for two different system
filename = "rfc\\" + filename
else:
filename = "rfc/" + filename
#print (current_path+"/"+filename)
#print (os.path.exists(current_path+"/"+filename))
if os.path.exists(filename) == 0:
status = "404"
phrase = "Not Found"
message = "P2P-CI/1.0 "+ status + " "+ phrase + "\n"\
"Date:" + current_time + "\n"\
"OS: "+str(OS)+"\n"
else:
status = "200"
phrase = "OK"
txt = open(filename)
data = txt.read()
last_modified = time.ctime(os.path.getmtime(filename))
content_length = os.path.getsize(filename)
message = ["P2P-CI/1.0 "+ status + " "+ phrase + "\n"\
"Date: " + current_time + "\n"\
"OS: " + str(OS)+"\n"\
"Last-Modified: " + last_modified + "\n"\
"Content-Length: " + str(content_length) + "\n"\
"Content-Type: text/text \n", str(data)]
#+ str(data)
return message
# send rfcs to peers
def send_file(filename): # send the RFC to peers
txt = open(filename)
data = txt.read(1024)
while data:
s.send(data)
data = txt.read(1024)
s.close()
# display p2p request message
def p2p_request_message(rfc_num, host):
OS = platform.platform()
message = "GET RFC "+str(rfc_num)+" P2P-CI/1.0 \n"\
"Host: "+str(host)+"\n"\
"OS: "+str(OS)+"\n"
return message
# display p2s request message for ADD method
def p2s_add_message(rfc_num, host, port, title): # for ADD
message = "ADD" + " RFC " + str(rfc_num)+" P2P-CI/1.0 \n"\
"Host: " + str(host)+"\n"\
"Port: " + str(port)+"\n"\
"Title: " + str(title)+"\n"
return [message, rfc_num, host, port, title]
# display p2s request message for LOOKUP method
def p2s_lookup_message(rfc_num, host, port, title, get_or_lookup): # LOOKUP method
message = "LOOKUP" + " RFC " + str(rfc_num)+" P2P-CI/1.0 \n"\
"Host: " + str(host)+"\n"\
"Port: " + str(port)+"\n"\
"Title: " + str(title)+"\n"
return [message, rfc_num, get_or_lookup]
#display p2s request message for LIST methods
def p2s_list_request(host, port):
message = "LIST ALL P2P-CI/1.0 \n"\
"Host: "+str(host)+"\n"\
"Port: "+str(port)+"\n"
return message
#get the list of the local rfcs
def get_local_rfcs():
rfcs_path = os.getcwd() + "/rfc"
rfcs_num = [num[num.find("c")+1:num.find(".")] for num in os.listdir(rfcs_path) if 'rfc' in num]
return rfcs_num
#pass peer's hostname, port number and rfc_num, rfc_title
def peer_information():
keys = ["RFC Number", "RFC Title"]
rfcs_num = get_local_rfcs()
rfcs_title = get_local_rfcs() # ["title1", "title2", "title3"] we use rfcs_num to fill in title
for num, title in zip(rfcs_num, rfcs_title):
entry = [num, title]
dict_list_of_rfcs.insert(0, dict(zip(keys, entry)))
return [upload_port_num, dict_list_of_rfcs] # [port, rfcs_num, rfcs_title]
upload_port_num = 65000+random.randint(1, 500) # generate a upload port randomly in 65000~65500
dict_list_of_rfcs = [] # list of dictionaries of RFC numbers and Titles.
s=socket.socket() # Create a socket object
#s.setsockopt(socket.SOL_SOCKET, socket.SO_RESUEDADDR, 1)
#host = socket.gethostname() # Get local machine name
host = "10.139.68.102"
port = 7734 # Reserve a port for your service.
s.connect((host, port))
data = pickle.dumps(peer_information()) # send all the peer information to server
s.send(data)
data = s.recv(1024)
print(data.decode('utf-8'))
s.close
def print_combined_list(dictionary_list, keys):
for item in dictionary_list:
print(' '.join([item[key] for key in keys]))
def get_user_input():
#if key press, then close:
user_input = input("> Enter ADD, LIST, LOOKUP, GET, or EXIT: ")
if user_input == "EXIT":
data = pickle.dumps("EXIT")
#s.send(bytes('1', "utf-8"))
s.send(data)
s.close # Close the socket when done
elif user_input == "ADD":
user_input_rfc_number = input("> Enter the RFC Number: ")
user_input_rfc_title = input("> Enter the RFC Title: ")
data = pickle.dumps(p2s_add_message(user_input_rfc_number, host, upload_port_num, user_input_rfc_title))
s.send(data)
server_data = s.recv(1024)
print(server_data.decode('utf-8'))
get_user_input()
elif user_input == "LIST":
data = pickle.dumps(p2s_list_request(host, port))
s.send(data)
server_data = s.recv(1024)
print(server_data.decode('utf-8'), end="")
new_data = pickle.loads(s.recv(1024))
print_combined_list(new_data[0], new_data[1])
get_user_input()
elif user_input == "GET":
user_input_rfc_number = input("> Enter the RFC Number: ")
user_input_rfc_title = input("> Enter the RFC Title: ")
data = pickle.dumps(p2s_lookup_message(user_input_rfc_number, host, port, user_input_rfc_title, "0"))
s.send(data)
server_data = pickle.loads(s.recv(1024))
print("SERVER DATA:", server_data)
if not server_data[0]:
print(server_data[1])
else:
p2p_get_request(str(user_input_rfc_number), server_data[0]["Hostname"], server_data[0]["Port Number"])
get_user_input()
elif user_input == "LOOKUP":
user_input_rfc_number = input("> Enter the RFC Number: ")
user_input_rfc_title = input("> Enter the RFC Title: ")
data = pickle.dumps(p2s_lookup_message(user_input_rfc_number, host, port, user_input_rfc_title, "1"))
#print(p2s_lookup_message(user_input_rfc_number, host, port, user_input_rfc_title))
#print(data)
s.send(data)
server_data = pickle.loads(s.recv(1024))
#print(server_data[0][3])
#print(server_data[0][1])
print(server_data[1], end="")
keys = ['RFC Number', 'RFC Title', 'Hostname', 'Port Number']
print_combined_list(server_data[0], keys)
get_user_input()
else:
get_user_input()
def p2p_listen_thread(str, i):
upload_socket = socket.socket()
host = socket.gethostname()
upload_socket.bind((host, upload_port_num))
upload_socket.listen(5)
while True:
c, addr = upload_socket.accept()
data_p2p_undecode = c.recv(1024)
data_p2p = data_p2p_undecode.decode('utf-8')
indexP = data_p2p.index('P')
indexC = data_p2p.index('C')
rfc_num = data_p2p[indexC+1:indexP-1]# get the rfc_number
print (rfc_num)
print ('Got connection from', addr)
c.send(pickle.dumps(p2p_response_message(rfc_num)))
c.close()
# First we create new thread to handle upload listening event
# and then process the user input event, it looks like to be parrallel
# it works!
start_new_thread(p2p_listen_thread, ("hello", 1))
get_user_input()
# this is the upload socket for requesting from peers
#
# upload_socket= socket.socket()
# host = socket.gethostname()
# port = upload_port_num
# upload_socket.bind((host, port))
# upload_socket.listen(5)
# input=[upload_socket,sys.stdin]
# print ("hdhd")
# def peer_thread(conn, addr):
# data = c.recv(1024)
# indexP = data.index('P')
# indexC = data.index('C')
# rfc_num = data[indexC+1:indexP-1]# get the rfc_number
# print (rfc_num)
# print ('Got connection from', addr)
# c.send(p2p_response_message(rfc_num))
# c.close() # Close the connection
#
#
# while True:
# c, addr = upload_socket.accept()
# input.append(c)
# print("hello")
# while True:
# readyInput,readyOutput,readyException=select.select(input,[],[])
# for indata in readyInput:
# if indata == c:
# data = c.recv(1024)
# indexP = data.index('P')
# indexC = data.index('C')
# rfc_num = data[indexC+1:indexP-1]# get the rfc_number
# print(rfc_num)
# print('Got connection from', addr)
# c.send(p2p_response_message(rfc_num))
# if not data:
# break
# else:
# print("jj")
# get_user_input()
# c.close()
# peers_information()
# p2s_list_request("wfu.eedu", "787")
# #p2s_request_message("234", "34", "wfu.ncsu.edu", "ADD", "This is a test")
# #p2p_response_message("2")
# s = socket.socket() # Create a socket object
# host = socket.gethostname() # Get local machine name
# print host
# port = 38888 # Reserve a port for your service.
# #p2p_request_message(4434,host)
# s.connect((host, port))
# s.sendall(p2p_request_message("23444", "28888"))
# #s.sendall(p2p_response_message("234"))
# print s.recv(1024)
# s.close # Close the socket when done