2012年5月30日星期三

JSON方式Google API基站


# -*- coding: utf-8 -*-    
      
import os    
import sys    
import json    
import httplib    
s1='{\
 "version": "1.1.0",\
 "host": "maps.google.com",\
 "access_token": "2:k7j3G6LaL6u_lafw:4iXOeOpTh1glSXe",\
 "request_address": true,\
 "address_language": "zh_CN",\
 "cell_towers": [\
 {\
 "cell_id": 21320851,\
 "location_area_code": 42245,\
 "mobile_country_code": 460,\
 "mobile_network_code": 1\
 }\
 ]\
}'
print s1    

class TestJSON():    
 server_url = "www.google.com"
 def __init__(self):
  print u"start"
 def run(self):
  global s1
  #socket.setdefaulttimeout(10)
  print u"Start connection"
  self.conn = httplib.HTTPConnection(self.server_url)
  #self.conn.set_debuglevel(5)
  request_url = "/loc/json"
  req_headers = { "Content-Type" : "application/json" }
  req_body = s1
  self.conn.request("POST", request_url, body = req_body, headers = req_headers )
  res = self.conn.getresponse()
  http_status = res.status
  http_reason = res.reason
  print res
  msg = res.read()
  print u"msg=", msg

if __name__ == "__main__":
 app = TestJSON()
 app.run()
代码执行结果: start Start connection msg= {"location":{"latitude":23.1150726,"longitude":113.3412833,"address":{"country":"中国","country_code":"CN","region":"广东省","city":"广州市","street":"海风路","street_number":"46号"},"accuracy":1034.0},"access_token":"2:tB17oYZlvht_7f3y:a62_bNZJN3t5pA6y"} 

据说,Google对基站位置的查询结果比运营商自己的数据还准确。

标签: , ,


评论: 发表评论

订阅 博文评论 [Atom]





<< 主页

This page is powered by Blogger. Isn't yours?

订阅 博文 [Atom]