-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
140 lines (114 loc) · 2.92 KB
/
server.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
import os
import sys
import socket
import hashlib
import pickle
from datetime import datetime, time
host_name = "localhost"
port_number = 5050
UDP_PORT = 5051
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print( "Server Activated...")
except socket.error :
print( "Unable to create Server!")
sys.exit()
try:
s.bind((host_name, port_number))
except socket.error:
print( 'Server binding failed!!')
sys.exit()
s.listen(1)
print( "Server listening...")
history = ""
def shortlist(val):
try:
sDate = datetime.strptime(val[2]+" "+val[3], '%d-%m-%Y %H:%M:%S')
eDate = datetime.strptime(val[4]+" "+val[5], '%d-%m-%Y %H:%M:%S')
strt_ts = datetime.timestamp(sDate)
end_ts = datetime.timestamp(eDate)
fileNameArr = os.popen("ls").read().split("\n")[:-1]
content = os.popen('ls -ogh --time-style=long-iso').read().split("\n")[1:-1]
timeArr = []
for i in fileNameArr:
timeArr.append(os.stat(i).st_mtime)
snd_name = []
snd_cntnt = []
for i in range(len(timeArr)):
if int(timeArr[i]) > strt_ts and int(timeArr[i]) < end_ts:
snd_name.append(fileNameArr[i])
snd_cntnt.append(content[i])
finl_rslt = [snd_name, snd_cntnt]
snd.sendall(pickle.dumps(finl_rslt))
except:
pass
return
def longlist():
ls = os.popen("ls -ogh --time-style=long-iso").read()
content = ls.split("\n")[1:-1]
fileNameArr = os.popen("ls").read().split("\n")
t5 = [content,fileNameArr]
data_long = pickle.dumps(t5)
snd.sendall(data_long)
return
def verify(tq1):
try:
snd.sendall(pickle.dumps(hashlib.md5(open(tq1,'rb').read()).hexdigest()))
except:
print("File not found on the server!")
return
def checkall():
addr = os.getcwd()
file_hash = {}
for f in os.listdir(addr):
if os.path.isfile(f):
file_hash[f] = hashlib.md5(open(f,'rb').read()).hexdigest()
hash_data = pickle.dumps(file_hash)
snd.sendall(hash_data)
return
def FileDownload(cmnd):
file_name = cmnd.split(" ")
try:
with open(file_name[1], 'rb') as file_down:
for data in file_down:
snd.sendall((data))
except:
print("File not found!")
return
def FileDownload_udp(cmnd):
udp_s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
dest = (host_name, UDP_PORT)
file_name = cmnd.split(" ")
try:
with open(file_name[1], 'rb') as file_down:
for data in file_down:
udp_s.sendto(data,dest)
except:
print("File not on server!")
udp_s.close()
return
while 1:
snd, num = s.accept()
print( "Got connected from ", num[0], ':', num[1])
data = pickle.loads(snd.recv(4096))
history = history + (data + "\n")
val = data.strip().split(" ")
if (data == 'Q' or data == 'q'):
snd.close()
break
elif (val[1] == "shortlist"):
shortlist(val)
elif (val[1] == "longlist"):
longlist()
elif (val[0] == "FileHash"):
if(val[1] == "verify"):
verify(val[2])
elif (val[1] == "checkall"):
checkall()
elif(val[0] == "FileDownload"):
if val[2]=='TCP':
FileDownload(data)
else:
FileDownload_udp(data)
snd.close()
s.close()