Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Update client.py

  • Loading branch information...
commit f9c83ac4dc818e9c3f4db7b2ccaf162b7e69569a 1 parent d55eb8b
Frankie Primerano authored

Showing 1 changed file with 79 additions and 53 deletions. Show diff stats Hide diff stats

  1. +79 53 client.py
132 client.py
... ... @@ -1,61 +1,87 @@
1   -from flask import Flask
2   -import os, hashlib
  1 +import urllib, time, os, getpass, thread, sys, hashlib
3 2
4   -app = Flask(__name__)
  3 +class MatchBoxClient:
5 4
6   -users = {
  5 + def __init__(self):
  6 + self.url = raw_input("URL: ")
  7 + self.username = raw_input("Username: ")
  8 + self.password = getpass.getpass("Password: ")
  9 + self.password = hashlib.sha1(self.password).hexdigest()
  10 + self.files = {}
  11 + self.verify_username_password()
  12 + thread.start_new_thread(self.shell, ())
  13 + self.main_loop()
  14 + def main_loop(self):
  15 + while True:
  16 + try:
  17 + time.sleep(1)
  18 + this_dir = os.listdir(os.getcwd())
  19 + that_dir = eval(urllib.urlopen(self.url+"/list/"+self.username+"/"+self.password).read())
  20 + if str(this_dir) != str(that_dir):
  21 + for this in this_dir:
  22 + if this not in self.files and this != sys.argv[0]:
  23 + with open(this, 'rb') as md5file:
  24 + print "added", this
  25 + self.files[this] = hashlib.md5(md5file.read()).hexdigest()
  26 + if this not in that_dir and this != sys.argv[0]:
  27 + thread.start_new_thread(self.upload, (this,))
  28 + for that in that_dir:
  29 + if that not in this_dir:
  30 + thread.start_new_thread(self.download, (that,))
  31 + for file in self.files:
  32 + try:
  33 + with open(file, 'rb') as check_file:
  34 + check = hashlib.md5(check_file.read()).hexdigest()
  35 + if check != self.files[file]:
  36 + print file, "changed"
  37 + urllib.urlopen(self.url+"/delete/"+self.username+"/"+self.password+"/"+file)
  38 + self.files[file] = check
  39 + thread.start_new_thread(self.upload, (file,))
  40 + except IOError:
  41 + pass
  42 + except IOError:
  43 + print "It seems as though your server is down, please check it."
  44 + time.sleep(60)
  45 +
7 46
8   -'username':'password',
  47 + def upload(self, file):
  48 + with open(file, 'rb') as upload:
  49 + print "Uploading", file
  50 + for letter in upload.readlines():
  51 + line = []
  52 + for x in letter:
  53 + line.append(str(ord(x)))
  54 + urllib.urlopen(self.url+"/upload/"+self.username+"/"+self.password+"/"+file+"/"+' '.join(line))
  55 + print "Done uploading", file
9 56
10   - }
  57 + def download(self, file):
  58 + with open(file, 'wb') as download:
  59 + print "Downloading", file
  60 + download.write(urllib.urlopen(self.url+"/download/"+self.username+"/"+self.password+"/"+file).read())
  61 + print "Done downloading", file
11 62
12   -@app.route("/delete/<username>/<password>/<file>")
13   -def delete(username, password, file):
14   - if login(username, password) is False:
15   - return "Login Failed"
16   - else:
17   - os.remove("files/"+file)
18   - return "Success"
  63 + def delete(self, file):
  64 + os.remove(file)
  65 + del self.files[file]
  66 + urllib.urlopen(self.url+"/delete/"+self.username+"/"+self.password+"/"+file)
19 67
20   -@app.route("/upload/<username>/<password>/<file>/<data>")
21   -def upload(username, password, file, data):
22   - if login(username, password) is False:
23   - return "Login Failed"
24   - else:
25   - if file not in os.listdir("files"):
26   - with open("files/"+file, 'w') as file:
27   - pass
28   - upload = open("files/"+file, 'ab')
29   - data = data.split()
30   - for data in data:
31   - upload.write(chr(int(data)))
32   - upload.close()
33   - return "Success"
34   -
35   -@app.route("/download/<username>/<password>/<file>")
36   -def download(username, password, file):
37   - if login(username, password) is False:
38   - return "Login Failed"
39   - else:
40   - with open("files/"+file, 'rb') as file:
41   - return file.read()
  68 + def shell(self):
  69 + while True:
  70 + cmd = raw_input('> ')
  71 + if cmd.startswith("rm"):
  72 + cmd = cmd.split()
  73 + if cmd[1] not in os.listdir(os.getcwd()):
  74 + print "File doesn't exist"
  75 + elif cmd[1] == sys.argv[0]:
  76 + print "Don't delete MatchBox!"
  77 + else:
  78 + thread.start_new_thread(self.delete, (cmd[1],))
  79 + if cmd == "ls":
  80 + print os.listdir(os.getcwd())
  81 + def verify_username_password(self):
  82 + if urllib.urlopen(self.url+"/list/"+self.username+"/"+self.password).read() == "Login Failed":
  83 + print "Username or password not correct."
  84 + exit()
42 85
43   -@app.route("/list/<username>/<password>")
44   -def list(username, password):
45   - if login(username, password) is False:
46   - return "Login Failed"
47   - else:
48   - return str(os.listdir("files"))
49   -
50   -def login(username, password):
51   - if username not in users:
52   - return False
53   - elif hashlib.sha1(users[username]).hexdigest() == password:
54   - return True
55   - else:
56   - return False
57   -
58 86 if __name__ == "__main__":
59   - if "files" not in os.listdir(os.getcwd()):
60   - os.mkdir("files")
61   - app.run(host='0.0.0.0')
  87 + MatchBoxClient()

0 comments on commit f9c83ac

Please sign in to comment.
Something went wrong with that request. Please try again.