使用google map进行 gsm wcdma 基站定位获得及附近信息的代码

最近工作需要,写了一个下面脚本,作为备份
# -*- coding: cp936 -*-

#!/usr/bin/python

from struct import pack, unpack
from httplib import HTTP
import urllib2,json,fileinput,Queue,threading,time


#组件信息
proxy_handler = urllib2.ProxyHandler({'http': 'http://127.0.0.1:8080/'})
urlopener = urllib2.build_opener(urllib2.HTTPCookieProcessor())#,proxy_handler)


#http头
headers={'User-Agent':'Dalvik/1.6.0 (Linux; U; Android 4.0.4; ZTE N881F MIUI/3.7.7)'}

url=r'http://maps.google.com/maps/api/geocode/json?sensor=false&language=zh-cn&latlng=%s,%s'

def geturlcon(url,data=None,headers=headers,byte=None):
    request = urllib2.Request(url,data,headers)
    url = urlopener.open(request)
    page=url.read(byte).decode('utf8','ignore')
    return page


def fetch_latlong(query):
    http = HTTP('mobilemaps.clients.google.com', 80)
    http.putrequest('POST', '/glm/mmap')
    http.putheader('Content-Type', 'application/binary')
    http.putheader('Content-Length', str(len(query)))
    http.endheaders()
    http.send(query)
    code, msg, headers = http.getreply()
    result = http.file.read()
    return result

def fetch_latlong_urllib(query):
    headers = { 'User-Agent' : user_agent }
    req = urllib2.Request(mmap_url, query, headers)
    resp = urllib2.urlopen(req)
    response = resp.read()
    return response

def get_add_info(latitude,longitude):
    d=geturlcon(url%(latitude,longitude))
    a=json.loads(d)
    #pprint.pprint(a['results'])
    return a['results'][0]['formatted_address']+u'附近'
        

def get_location_by_cell(loc,cid):
    size=3
    if cid>65536:size=5
    b_string = pack('>h14ss3ss4s3sb2si2sHi4s4sI',
                    0xe,
                    '\x00',
                    '\x1b',
                    '\x00',
                    '\x01',
                    '\x00\x00\x01\xcc',
                    '\x00',
                    size, #5表示大cid wcdma   3表示小cid gsm
                    '\x00',
                    cid,
                    '\x00',
                    loc,
                    1,
                    '\x00\x00\x01\xcc',
                    '\xff\xff\xff\xff',0)

    bytes = fetch_latlong(b_string)
    print len(bytes),list(bytes)
    (a, b,errorCode, latitude, longitude, c, d, e) = unpack(">hBiiiiih",bytes)
    latitude = latitude / 1000000.0
    longitude = longitude / 1000000.0

    return latitude,longitude


def doit1(loc,cid):
    lat,lon=get_location_by_cell(loc,cid)
    addr=get_add_info(lat,lon)
    print lat,lon,addr

def doit(q,begin=1,stop=65536):
    
    while True:
        time.sleep(0.1)
        if q.empty():break
        loc=q.get()
        f=open(str(loc)+'.txt','a')
        f1=open('./err/'+str(loc)+'_err.txt','a')
        for cid in xrange(begin,stop):
            try:
                lat,lon=get_location_by_cell(loc,cid)
                addr=get_add_info(lat,lon)
                print loc,cid,lat,lon,addr
                print >>f,loc,cid,lat,lon,addr.encode('utf8')
                f.flush()
            except:
                print loc,cid,'error'
                print >>f1,loc,cid
                f1.flush()
        f1.close()
        f.close()
            
    

if __name__ == '__main__':
    doit1(28692, 20404)
    doit1(28692 ,20405)
    doit1(28692, 20406)
    '''
    q=Queue.Queue()
    for i in xrange(50):
        threading.Thread(target=doit,args=(q,)).start()
    
    for i in fileinput.FileInput('loc.txt'):
        q.put(int(i.strip()))
    while True:
        time.sleep(10)
        if q.empty():break
        '''

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值