Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
194 lines (131 loc) · 4.87 KB
/
Copy pathserver.py
File metadata and controls
194 lines (131 loc) · 4.87 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import http.server
import socketserver
import logging
import json
import importlib
import smtplib
import sys
import os
import re
import urllib.parse as urlparse
from urllib.parse import parse_qs
PORT = 80
if(1 in range(len(sys.argv))): PORT = sys.argv[1]
API_FOLDER = 'api'
CONTROLLERS_FOLDER = 'ctrl'
PUBLIC_FOLDER = 'public'
TEMPLATES_FOLDER = 'templates'
API_PREFIX = '/__api/'
PUBLIC_PREFIX = '/__public/'
#emailer
SMTP_HOST = 'smtp.gmail.com'
SMTP_PORT = 465
SMTP_USER = ''
SMTP_PASSWORD = ''
with open('routes.json') as json_file:
routes = json.load(json_file)
print( 'Routes', routes )
print( os.getcwd() )
class Handler( http.server.SimpleHTTPRequestHandler ):
content_types = {
'html' : 'text/html',
'css' : 'text/css',
'jpg' : 'image/jpeg',
'jpeg' : 'image/jpeg',
'js' : 'text/javascript'
}
def respond( self, content = "", content_type = "html" ):
self.send_response( 200 )
self.send_header('Content-type', self.content_types[ content_type ] or 'text/html' )
self.end_headers()
ba = bytearray()
ba.extend(map(ord, content))
self.wfile.write( ba )
def respondJson( self, data = None ):
ba = bytearray()
ba.extend(map(ord, json.dumps( data ) ))
self.wfile.write( ba )
def respondFile( self, file_path ):
try:
extension = file_path.split('.')[1]
except:
extension = 'html'
f = open( file_path ,'rb')
self.send_response( 200 )
self.send_header('Content-type', self.content_types[extension] or 'text/html' )
self.end_headers()
self.wfile.write(f.read())
f.close()
def template( self, template, data = {} ):
f = open( TEMPLATES_FOLDER + '/' + template + '.html' )
text = f.read()
for key in data:
p = re.compile('{{'+key+' ( [^}]* ) }}', re.VERBOSE)
text = p.sub( str(data[key]), text)
return text
def api_handler( self ):
uri = str( self.path )
if( uri.startswith( API_PREFIX ) ):
try:
self.post_data_string = self.rfile.read(int(self.headers['Content-Length']))
postData = json.loads( self.post_data_string )
except:
postData = {}
parsed = urlparse.urlparse(uri)
getData = parse_qs(parsed.query)
_api = uri[len(API_PREFIX):].split('/')
module = _api[0]
method = 'default' if len(_api) == 1 else _api[1]
if( os.path.isfile( API_FOLDER + '/' + module + '.py' ) ):
sys.path.append( API_FOLDER + '/' )
api = importlib.import_module( module )
getattr( api, method )(*[ self, postData, getData ])
return True
return False
def email(self, to = [], subject = '', body = '' ):
user = SMTP_USER
password = SMTP_PASSWORD
sent_from = user
email = """\
From: %s
To: %s
Subject: %s
%s
""" % (sent_from, ", ".join(to), subject, body)
try:
server = smtplib.SMTP_SSL( SMTP_HOST , SMTP_PORT )
server.ehlo()
server.login( user, password )
server.sendmail( sent_from, to, email )
server.close()
print('Email sent!')
except Exception as e:
print('Email error :: ' + str(e) )
def do_POST(self):
try:
if( self.api_handler() ): return
self.respondJson()
except IOError:
self.send_error(404, 'File Not Found: %s' % self.path)
def do_GET(self):
try:
uri = str( self.path )
print('GET request,\nURI: ' + uri)
if( self.api_handler() ): return
file_path = PUBLIC_FOLDER + '/' + uri
if( uri == '/' ): file_path = PUBLIC_FOLDER + '/' + 'index.html'
for item in routes:
if(item['uri'] == uri):
if 'target' in item.keys() : file_path = PUBLIC_FOLDER + '/' + item['target']
if 'controller' in item.keys() :
sys.path.append( CONTROLLERS_FOLDER + '/' )
ctrl = importlib.import_module( item['controller'] )
ctrl.default( self )
return
self.respondFile( file_path )
return
except IOError:
self.respondFile( PUBLIC_FOLDER + '/404.html' )
with socketserver.TCPServer(('', PORT), Handler) as httpd:
print('serving at port', PORT)
httpd.serve_forever()