-
Notifications
You must be signed in to change notification settings - Fork 0
/
wri_bsid_search.py
183 lines (163 loc) · 5.83 KB
/
wri_bsid_search.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
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
#!/bin/python
#-*-coding:UTF-8-*-
#Author: 褚夫元 <[email protected]>
__VERSION__ = 0.3
#import http.client
import httplib
import json
import socket
import random
class Bs:
net = '0'
BSID = '0'
#HOST = 'apis.juhe.cn'
HOST = 'v.juhe.cn'
__KEY__ = {'g':('f17e16be2871e09e7669b0c0abe03342','f17e16be2871e09e7669b0c0abe03342'),
'c':('d4ba46dd5a85d76a642350fcd62a951f','d4ba46dd5a85d76a642350fcd62a951f')
}
__HEX__ = '16'
Fatal_code = ('444','112')
G_rcode = { '101':'appKey错误','203':'查询不到记录','200':'成功的返回',
'112':'当前请求可能已超过限制,请稍后再试或与客服联系!','105':'应用审核未通过!'
}
C_rcode = { '101':'appKey错误','201':'SID错误','202':'Nid错误',
'203':'Cellid错误','204':'系统出错','205':'查询不到记录',
'200':'成功的返回','112':'当前请求可能已超过限制,请稍后再试或与客服联系!','105':'应用审核未通过!'
}
r_code = {}
aid = '460310000'
lng = ''
lat = ''
o_lat = ''
o_lng = ''
address = ''
precision = ''
reason = ''
resultcode = ''
def __init__(self,BSID):
self.BSID = BSID
if BSID[0:1] == '4':
self.net = 'g'
self.r_code = self.G_rcode
elif BSID[0:1] == '3':
self.net = 'c'
self.r_code = self.C_rcode
else:
self.net = 'g'
self.r_code = self.G_rcode
def getUrl(self):
length = len(self.__KEY__[self.net])
rand = random.randrange(0,length)
KEY = self.__KEY__[self.net][rand]
if self.net == 'c':
sid = self.BSID[0:4]
nid = self.BSID[4:8]
cellid = self.BSID[8:12]
GET = '/cdma/?sid='+sid+'&cellid='+cellid+'&nid='+nid+'&hex='+self.__HEX__+'&key='+KEY
elif self.net == 'g':
cell = self.BSID[9:13]
lac = self.BSID[5:9]
GET = '/cell/get?cell='+cell+'&lac='+lac+'&hex='+self.__HEX__+'&key='+KEY
return GET
def fetchJson(self):
try:
#conn = http.client.HTTPConnection(self.HOST)
conn = httplib.HTTPConnection(self.HOST)
GET = self.getUrl()
conn.request('GET',GET)
res = conn.getresponse()
data = res.read().decode('utf-8')
conn.close()
except socket.gaierror:
raise socket.gaierror
except socket.timeout:
raise socket.timeout
return data
def doSearch(self):
try:
jsonData = self.fetchJson()
except socket.gaierror:
raise socket.gaierror
except socket.timeout:
raise socket.timeout
info = json.loads(jsonData)
self.resultcode = info['resultcode']
if self.resultcode == '200':
if self.net == 'g':
self.reason = info['reason']
result = info['result']['data']
#result的长度
result = result[0]
self.lng = str(result['LNG'])
self.lat = str(result['LAT'])
self.o_lng = str(result['O_LNG'])
self.o_lat = str(result['O_LAT'])
self.address =str(result['ADDRESS'])
self.precision = str(result['PRECISION'])+'米'
elif self.net == 'c':
self.reason = info['reason']
result = info['result']
self.lng = str(result['lon'])
self.lat = str(result['lat'])
self.address = str(result['address'])
self.precision = str(result['raggio'])+'米'
else:
self.reason = info['reason']
def resultSet(self):
try:
self.doSearch()
except socket.gaierror:
self.resultcode = '444'
return 'Resolve host failed.'
except socket.timeout:
self.resultcode = '444'
return 'Time out.'
if self.resultcode == '200':
details = (self.BSID,self.aid,self.lng,self.lat,self.address,self.precision)
rSet = '\t'.join(details)
return rSet
else:
try:
details = (self.BSID,self.r_code[self.resultcode])
rSet = ':'.join(details)
except KeyError:
details = (self.BSID,'Code('+self.resultcode+')',self.reason)
rSet = ':'.join(details)
finally:
return rSet
if __name__ == '__main__':
#BSID = '3610000E1103'
#BSID = '3610000E0092'
#BSID = '46000185DE122'
#BSID = '46001A8042A01'
import sys
import time
reload(sys)
sys.setdefaultencoding('utf-8')
list = ('3610000E1103','46000185DE122','3610000E0092','46001A8042A01')
c = 0
head = ('BSID','经度','纬度','基站描述','覆盖半径')
for i in list:
if c == 0:
print '\t'.join(head).encode('gb2312')
c = c+1
bs1 = Bs(i)
result = bs1.resultSet()
print result.encode('gb2312')
''' try:
bs1.doSearch()
except socket.gaierror:
print 'Resolve host failed.'
break
except socket.timeout:
print 'Time out.'
break
if bs1.resultcode == '200':
print "%s\t%s\t%s\t%s\t%s" %(bs1.BSID,bs1.lng,bs1.lat,bs1.address,bs1.precision)
else:
try:
print "%s:%s" %(bs1.BSID,bs1.r_code[bs1.resultcode])
except KeyError:
print "%s:%s" %(bs1.resultcode,bs1.reason)
break
'''