|
5 | 5 | from urllib import request |
6 | 6 | from http.server import BaseHTTPRequestHandler, HTTPServer |
7 | 7 |
|
8 | | -constConf = {'servAdd':'http://221.10.182.250:55555', 'locAdd':'127.0.0.1', 'locPort':5555} |
9 | | -contentHTML = '' |
| 8 | +conf = { |
| 9 | + 'servAddr':'http://221.10.182.250:55555', |
| 10 | + 'pubAddr':'http://127.0.0.1:5555', |
| 11 | + 'bindPort':5555 |
| 12 | +} |
| 13 | + |
| 14 | +allowAddr = [ |
| 15 | + '/BusService/Query_ByRouteID/', |
| 16 | + '/BusService/Require_AllRouteData/', |
| 17 | + '/BusService/Require_RouteStatData/' |
| 18 | +] |
| 19 | + |
| 20 | +HTML = b'' |
| 21 | +ICON = b'' |
10 | 22 |
|
11 | 23 | class BusService(BaseHTTPRequestHandler): |
12 | | - server_version = "BusService/1.0" |
| 24 | + server_version = 'BusService/1.0' |
| 25 | + sys_version = '' |
13 | 26 |
|
14 | 27 | def do_GET(self): |
15 | | - allowAddr = ['/', '/favicon.ico','/BusService/Query_ByRouteID/','/BusService/Require_AllRouteData/','/BusService/Require_RouteStatData/'] |
16 | | - |
17 | | - self.close_connection = True |
18 | 28 | fname=self.path.split('?')[0] |
19 | | - if fname in allowAddr: |
20 | | - if fname is '/': |
21 | | - self._sendHttpHeader(200,'text/html') |
22 | | - self._sendHttpBody(contentHTML) |
23 | | - elif fname is '/favicon.ico': |
24 | | - self._sendHttpHeader(200,'application/x-ico') |
25 | | - with open('favicon.ico','rb') as f: |
26 | | - self._sendHttpBody(f.read()) |
27 | | - else: |
28 | | - self._sendHttpHeader(200) |
29 | | - with request.urlopen(constConf['servAdd']+self.path) as f: |
30 | | - self._sendHttpBody(f.read()) |
| 29 | + if fname == '/': |
| 30 | + self._sendHttpHeader(200, 'text/html') |
| 31 | + self._sendHttpBody(HTML) |
| 32 | + elif fname == '/favicon.ico': |
| 33 | + self._sendHttpHeader(200, 'application/x-ico') |
| 34 | + self._sendHttpBody(ICON) |
| 35 | + elif fname in allowAddr: |
| 36 | + self._sendHttpHeader(200, 'application/json') |
| 37 | + with request.urlopen(conf['servAddr']+self.path) as f: |
| 38 | + self._sendHttpBody(f.read()) |
31 | 39 | else: |
32 | 40 | self._sendHttpHeader(404) |
33 | | - self._sendHttpBody({'msg':'Not Found'}) |
| 41 | + self._sendHttpBody(b'404 Not Found') |
34 | 42 |
|
35 | 43 | def do_POST(self): |
36 | | - self.close_connection = True |
37 | | - self.send_response(403) |
38 | | - self.end_headers() |
39 | | - self.wfile.write(b'Not Allowed') |
| 44 | + self._sendHttpHeader(403) |
| 45 | + self._sendHttpBody(b'403 Forbidden') |
40 | 46 |
|
41 | | - def _sendHttpHeader(self, code, contentType='application/json'): |
| 47 | + def _sendHttpHeader(self, code, contentType='text/plain'): |
| 48 | + self.close_connection = True |
42 | 49 | self.send_response(code) |
43 | 50 | self.send_header('Content-Type', contentType) |
44 | | - self.send_header('Content-Encoding','gzip') |
45 | 51 | self.end_headers() |
46 | 52 |
|
47 | 53 | def _sendHttpBody(self, data): |
48 | | - body = b'' |
49 | | - if isinstance(data, bytes): |
50 | | - body = data |
51 | | - elif isinstance(data, str): |
52 | | - body = data.encode('utf-8', errors='ignore') |
53 | | - else: |
54 | | - body = json.dumps(data).encode('utf-8', errors='ignore') |
55 | | - self.wfile.write(gzip.compress(body)) |
| 54 | + self.wfile.write(data) |
56 | 55 |
|
57 | 56 | def main(): |
58 | | - global contentHTML |
| 57 | + global HTML, ICON |
| 58 | + |
59 | 59 | with open('index.html','r',encoding='utf-8') as f: |
60 | | - contentHTML = f.read() |
61 | | - contentHTML = contentHTML.replace('{ADDR}',constConf['locAdd']).replace('{PORT}',str(constConf['locPort'])) |
62 | | - httpd = HTTPServer((constConf['locAdd'], constConf['locPort']), BusService) |
63 | | - print('Started BusService At %s:%s' % (constConf['locAdd'], constConf['locPort'])) |
| 60 | + HTML = f.read().replace('{ADDR}',conf['pubAddr']).encode('utf-8') |
| 61 | + |
| 62 | + with open('favicon.ico','rb') as f: |
| 63 | + ICON = f.read() |
| 64 | + |
| 65 | + print('Started BusService.') |
| 66 | + |
| 67 | + httpd = HTTPServer(('', conf['bindPort']), BusService) |
64 | 68 | httpd.serve_forever() |
65 | 69 |
|
66 | 70 | if __name__ == '__main__': |
|
0 commit comments