-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
159 lines (136 loc) · 5.14 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import socket
import os.path,time
import hashlib
import datetime
import sys
from colorama import Fore, Back, Style
class server:
s = socket.socket()
host = ''
port = ''
client_is_active = 1
server_is_active = 1
def __init__(self,portnumber):
self.host = socket.gethostname()
self.port = portnumber
self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.s.bind((self.host, self.port))
self.s.listen(10)
def runserver(self):
print "Starting server"
while self.server_is_active:
print "waiting for a client"
c, addr = self.s.accept()
self.client_is_active = 1
print 'Got connection from', addr
while self.client_is_active:
#print "listening"
print "waiting"
x = c.recv(1024)
if x.endswith('EOM'):
#valid message
#c.send("okfineEOM")
print "received",x
command = x[:-3]
if command == 'exit':
print "client disconnected"
self.client_is_active = 0
self.server_is_active = 0
c.close()
elif command == 'FileHash checkall':
#print "going to execute check all function"
self.checkall(c)
elif ''.join(command.split(' ')[:-1]) == 'FileHashverify':
print "single file"
filename = command.split(' ')[-1]
result = self.verify(os.getcwd() + '/' + filename)
#print result
try :
c.send(result)
except:
c.send("yolo")
#print "sent"
c.send('EOM')
elif command.split(' ')[0] == 'FileDownload':
self.FileDownload(command.split(' ')[1],c)
elif ''.join(command.split(' ')) == 'IndexGetlonglist' :
print "hre iinkjfjsd "
self.longlist(c)
elif ''.join(command.split()[:-2]) == 'IndexGetshortlist':
startdate = command.split(' ')[2]
enddate = command.split(' ')[3]
self.shortlist(startdate, enddate, c)
print "killing server"
return
def md5(self, fname):
hash = hashlib.md5()
with open(fname, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hash.update(chunk)
return hash.hexdigest()
def last_modified(self,fname):
return time.ctime(os.path.getmtime(fname))
def verify(self,fname):
try:
return fname.split('/')[-1] + '\t' + self.md5(fname) + '\t' + self.last_modified(fname)
except IOError:
return fname.split('/')[-1] + " is a directory,or is not found"
def checkall(self,c):
base_dir = os.getcwd() + '/'
list = os.listdir(base_dir)
for l in list:
print self.verify(base_dir + l)
try:
c.send(self.verify(base_dir + l))
except:
c.send("Bad file")
c.send('EOM')
return
def FileDownload(self,fname,c):
try:
x = self.md5(fname)
c.send("okEOM")
f = open(fname, 'rb')
while True:
data = f.read()
if not data:
break
c.sendall(data)
f.close()
c.send("EOIF")
except IOError:
c.send(fname + " is not found.EOIF")
def longlist(self,c):
files = os.listdir(".")
for file in files:
filename = os.path.basename(file)
query = os.stat(file)
timestamp = query[8]
date = datetime.datetime.fromtimestamp(timestamp)
size = str(query.st_size) + ' KB'
type = file.split('.')[-1] + ' file'
final_ans = filename + " " + str(date) + " " + str(size) + ' ' + type + "\n"
c.sendall(final_ans)
c.send("EOM")
def shortlist(self,a,b,c):
files = os.listdir(".")
start = datetime.datetime.strptime(a,'%Y-%m-%d')
end = datetime.datetime.strptime(b,'%Y-%m-%d')
for file in files:
filename = os.path.basename(file)
query = os.stat(file)
timestamp = query[8]
date = datetime.datetime.fromtimestamp(timestamp)
size = str(query.st_size) + ' KB'
type = file.split('.')[-1] + ' file'
final_ans = filename + " " + str(date) + " " + str(size) + ' ' + type + "\n"
if date > start and date < end :
c.sendall(final_ans)
c.send("EOM")
def serverscript(portnumber):
server1 = server(portnumber)
server1.runserver()
if __name__ == '__main__':
print "server program started"
portnumber = int(sys.argv[1])
serverscript(portnumber)