-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebReplicate.py
executable file
·63 lines (56 loc) · 1.88 KB
/
webReplicate.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
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import urllib
import sys
import os
class myHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.end_headers()
path = directory+"/"+removeIllegal(self.path)
if os.path.isfile(path):
tempFile = open(path,"rb")
content = tempFile.read()
tempFile.close()
else:
try:
content = urllib.urlopen(url+self.path).read()
urllib.urlretrieve(url+self.path,path)
except:
content = '''
<html>
<head>
<title>
404 Error
</title>
</head>
<body>
<center>
<h1>404 Not Found</h1>
</center>
</body>
</html>
'''
self.wfile.write(content)
def getServer(port):
BaseHTTPRequestHandler.server_version = "Microsoft-IIS/8.0"
BaseHTTPRequestHandler.sys_version = ""
return HTTPServer(("",port), myHandler)
def verify(url,directory):
if not os.path.exists(directory):
try:
urllib.urlopen(url)
os.makedirs(directory)
except:
sys.exit()
def removeIllegal(path):
for item in '\/:*?"<>|':
path = path.replace(item,"_")
return path
if __name__ == "__main__":
url = raw_input("URL: ")
port = int(raw_input("Port: "))
directory = removeIllegal(url)
verify(url,directory)
server = getServer(port)
print "Replicating {} on port {}".format(url,port)
server.serve_forever()