forked from f-prime/MatchBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
87 lines (79 loc) · 3.75 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
import urllib, time, os, getpass, thread, sys, hashlib
class MatchBoxClient:
def __init__(self):
self.url = raw_input("URL: ")
self.username = raw_input("Username: ")
self.password = getpass.getpass("Password: ")
self.password = hashlib.sha1(self.password).hexdigest()
self.files = {}
self.verify_username_password()
thread.start_new_thread(self.shell, ())
self.main_loop()
def main_loop(self):
while True:
try:
time.sleep(1)
this_dir = os.listdir(os.getcwd())
that_dir = eval(urllib.urlopen(self.url+"/list/"+self.username+"/"+self.password).read())
if str(this_dir) != str(that_dir):
for this in this_dir:
if this not in self.files and this != sys.argv[0]:
with open(this, 'rb') as md5file:
print "added", this
self.files[this] = hashlib.md5(md5file.read()).hexdigest()
if this not in that_dir and this != sys.argv[0]:
thread.start_new_thread(self.upload, (this,))
for that in that_dir:
if that not in this_dir:
thread.start_new_thread(self.download, (that,))
for file in self.files:
try:
with open(file, 'rb') as check_file:
check = hashlib.md5(check_file.read()).hexdigest()
if check != self.files[file]:
print file, "changed"
urllib.urlopen(self.url+"/delete/"+self.username+"/"+self.password+"/"+file)
self.files[file] = check
thread.start_new_thread(self.upload, (file,))
except IOError:
pass
except IOError:
print "It seems as though your server is down, please check it."
time.sleep(60)
def upload(self, file):
with open(file, 'rb') as upload:
print "Uploading", file
for letter in upload.readlines():
line = []
for x in letter:
line.append(str(ord(x)))
urllib.urlopen(self.url+"/upload/"+self.username+"/"+self.password+"/"+file+"/"+' '.join(line))
print "Done uploading", file
def download(self, file):
with open(file, 'wb') as download:
print "Downloading", file
download.write(urllib.urlopen(self.url+"/download/"+self.username+"/"+self.password+"/"+file).read())
print "Done downloading", file
def delete(self, file):
os.remove(file)
del self.files[file]
urllib.urlopen(self.url+"/delete/"+self.username+"/"+self.password+"/"+file)
def shell(self):
while True:
cmd = raw_input('> ')
if cmd.startswith("rm"):
cmd = cmd.split()
if cmd[1] not in os.listdir(os.getcwd()):
print "File doesn't exist"
elif cmd[1] == sys.argv[0]:
print "Don't delete MatchBox!"
else:
thread.start_new_thread(self.delete, (cmd[1],))
if cmd == "ls":
print os.listdir(os.getcwd())
def verify_username_password(self):
if urllib.urlopen(self.url+"/list/"+self.username+"/"+self.password).read() == "Login Failed":
print "Username or password not correct."
exit()
if __name__ == "__main__":
MatchBoxClient()