-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
92 lines (91 loc) · 3.12 KB
/
index.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
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
#-*- coding:utf-8 -*-
from bae.core.wsgi import WSGIApplication
from wri_bsid_search import *
from cgi import parse_qs, escape
import sys
#import markdown
reload(sys)
sys.setdefaultencoding('utf-8')
#list = ('3610000E1103','46000185DE122','3610000E0092','46001A8042A01')
c = 0
head = ('BSID','经度','纬度','基站描述','覆盖半径')
body = ''
response_md = '''
<table>
<thead>
<tr>
<td>head1</td>
<td>head2</td>
<td>head3</td>
</tr>
</thead>
<tbody>
<tr>
<td>%s</td>
<td>%s</td>
<td>%s</td>
</tbody>
</table>
'''
def md2html(mkdn):
return markdown.markdown(mkdn)
def valid_bsid(bsid):
c_LEN = 12
g_LEN = 13
length = len(bsid)
if length < c_LEN or length > g_LEN:#非法长度
return False
elif length == c_LEN and bsid[0:1] != '3':#C网3开头,G网4开头
return False
elif length == g_LEN and bsid[0:1] != '4':
return False
else:
return True
def app(environ, start_response):
status = '200 OK'
#headers = [('Content-type', 'text/html')]
#start_response(status, headers)
#body=["Welcome to Baidu Cloud!\n"]
#return body
# HTTP POST
# try:
# request_body_size = int(environ.get('CONTENT_LENGTH',0))
# except(ValueError):
# request_body_size = 0
# request_body = environ['wsgi.input'].read(request_body_size)
# d = parse_qs(request_body)
# bsid = d.get('bsid',[''])[0]
# bsid = escape(bsid)
# response_body = response_md % (bsid,'2','3')
# response_header = [('Content-type', 'text/html'),('Content-Length',str(len(response_body)))]
# start_response(status,response_header)
# return response_body
d = parse_qs(environ['QUERY_STRING'])
bsid = d.get('bsid',[''])[0]
bsid = escape(bsid)
if not valid_bsid(bsid):
response_body = "<tr><td>"+bsid+"</td><td colspan=\"4\">invalid id!!</td></tr>"
response_header = [('Content-type', 'text/html'),('Content-Length',str(len(response_body)))]
start_response(status,response_header)
return response_body
bs1 = Bs(bsid)
try:
bs1.doSearch()
except socket.gaierror:
response_body = "<tr><td>"+bsid+"</td><td colspan=\"4\">Resolve host failed.</td></tr>"
except socket.timeout:
response_body = "<tr><td>"+bsid+"</td><td colspan=\"4\">Time out.</td></tr>"
except ValueError:
response_body = "<tr><td>"+bsid+"</td><td colspan=\"4\">Try it again.</td></tr>"
if bs1.resultcode == '200':
response_body = "<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>" %(bs1.BSID,bs1.lng,bs1.lat,bs1.address,bs1.precision)
else:
try:
response_body = "<tr><td>%s</td><td colspan=\"4\">%s</td></tr>" %(bs1.BSID,bs1.r_code[bs1.resultcode])
except KeyError:
response_body = "<tr><td>%s</td><td>%s</td><td colspan=\"3\">%s</td></tr>" %(bs1.BSID,bs1.resultcode,bs1.reason)
response_header = [('Content-type', 'text/html'),('Content-Length',str(len(response_body)))]
start_response(status,response_header)
return response_body
#return md2html(response_body)
application = WSGIApplication(app)