Skip to content

Commit a1831f8

Browse files
committed
190330 improve: main logic
1 parent 3468eae commit a1831f8

File tree

3 files changed

+55
-47
lines changed

3 files changed

+55
-47
lines changed

README.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# 花城智慧公交 WebApplication
22

3+
Github: https://github.com/nutcore-net/PZH-BusService
4+
5+
Gitee: https://gitee.com/dtsdao/PZH-BusService/
6+
37
## 简介
48

59
> [chainsx](https://github.com/chainsx) 所托写的工具,感谢他提供的API
@@ -24,15 +28,15 @@
2428

2529
或者你也可以下载release里的压缩HTML后的压缩包,然后自行解压
2630

27-
3. 更改 `server.py``constConf` 的相关值
31+
3. 更改 `server.py``conf` 的相关值
2832

29-
- `servAdd` 为远端API地址
30-
- `locAdd` 为你想绑定的本地地址(支持域名)
31-
- `locPort` 为你想绑定的本地端口
33+
- `servAddr` 为远端API地址
34+
- `pubAddr` 为你想绑定的本地地址
35+
- `bindPort` 为你想绑定的端口
3236

33-
4. 使用 `python3 server.py` 运行代理服务器
37+
4. 使用 Python 运行 `server.py` 启动代理服务器
3438

35-
5. 打开浏览器访问你设置的 `locAdd``locPort` 就可以看到HTML页面
39+
5. 打开浏览器访问你设置的 `pubAddr` 就可以看到HTML页面
3640

3741
## 注意事项
3842

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,9 @@
7070
<script>
7171
var $$ = mdui.JQ,
7272
Assets = {
73-
lineUrl: 'http://{ADDR}:{PORT}/BusService/Require_AllRouteData/',
74-
dirUrl: 'http://{ADDR}:{PORT}/BusService/Require_RouteStatData/',
75-
staUrl: 'http://{ADDR}:{PORT}/BusService/Query_ByRouteID/',
73+
lineUrl: '{ADDR}/BusService/Require_AllRouteData/',
74+
dirUrl: '{ADDR}/BusService/Require_RouteStatData/',
75+
staUrl: '{ADDR}/BusService/Query_ByRouteID/',
7676
linDir: 0
7777
};
7878

server.py

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,62 +5,66 @@
55
from urllib import request
66
from http.server import BaseHTTPRequestHandler, HTTPServer
77

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''
1022

1123
class BusService(BaseHTTPRequestHandler):
12-
server_version = "BusService/1.0"
24+
server_version = 'BusService/1.0'
25+
sys_version = ''
1326

1427
def do_GET(self):
15-
allowAddr = ['/', '/favicon.ico','/BusService/Query_ByRouteID/','/BusService/Require_AllRouteData/','/BusService/Require_RouteStatData/']
16-
17-
self.close_connection = True
1828
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())
3139
else:
3240
self._sendHttpHeader(404)
33-
self._sendHttpBody({'msg':'Not Found'})
41+
self._sendHttpBody(b'404 Not Found')
3442

3543
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')
4046

41-
def _sendHttpHeader(self, code, contentType='application/json'):
47+
def _sendHttpHeader(self, code, contentType='text/plain'):
48+
self.close_connection = True
4249
self.send_response(code)
4350
self.send_header('Content-Type', contentType)
44-
self.send_header('Content-Encoding','gzip')
4551
self.end_headers()
4652

4753
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)
5655

5756
def main():
58-
global contentHTML
57+
global HTML, ICON
58+
5959
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)
6468
httpd.serve_forever()
6569

6670
if __name__ == '__main__':

0 commit comments

Comments
 (0)