-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstart.py
More file actions
84 lines (67 loc) · 2.89 KB
/
start.py
File metadata and controls
84 lines (67 loc) · 2.89 KB
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
import string, cgi, time, os
import pymysql
import template
from http import server
class MyRequestHandler(server.BaseHTTPRequestHandler):
def do_GET(self):
try:
host = self.headers['Host'].split(':')[0]
print(self.headers['Host'], host, self.path)
found = False
conn = pymysql.connect(host='localhost', port=3306, user='ohmu', passwd='TGSTGSTGS', db='crts')
cur = conn.cursor()
cur.execute("SELECT * FROM sites WHERE domain LIKE '%{0}%'".format(host))
for row in cur.fetchall():
siteId, className, domain, filePath = row
found = True
break
#find host in DB
if found:
#print("found {0}".format(className))
#instantiate class
_class = getattr(template, className)
instance = _class(self.path)
if instance.IsValid():
#build headers
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
#write out the file loaded from the script
self.wfile.write(bytes(str(instance), 'UTF-8'))
else:
#find path
try:
#print("looking for file: webroot/{0}{1}".format(host, self.path))
f = open('webroot/{0}{1}'.format(host, self.path), 'rb')
self.wfile.write(f.read())
except IOError as e:
self.send_error(404, 'File Not Found %s' % self.path)
return
else:
#look for path
try:
#print("looking for file: webroot/{0}{1}".format(host, self.path))
f = open('webroot/{0}{1}'.format(host, self.path), 'rb')
self.wfile.write(f.read())
except IOError as e:
self.send_error(404, 'File Not Found %s' % self.path)
#if no path, fail
self.send_error(404, 'File Not Found %s' % self.path)
cur.close()
conn.close()
except Exception as e:
print(e)
self.send_error(500, 'Internal Server Error')
def do_POST(self):
self.do_GET() # currently same as post, but can be anything
def main():
try:
# you can specify any port you want by changing <q>80</q>
HttpServer = server.HTTPServer(('', 80), MyRequestHandler)
print ('starting httpserver...')
HttpServer.serve_forever()
except KeyboardInterrupt:
print ('^C received, shutting down server...')
HttpServer.socket.close()
if __name__ == '__main__':
main()