Created
February 15, 2010 05:30
-
-
Save jebu/304439 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from httplib import HTTP | |
import location | |
latitude = 0 | |
longitude = 0 | |
def doLookup(cellId, lac, host = "www.google.com", port = 80): | |
from string import replace | |
from struct import unpack | |
page = "/glm/mmap" | |
http = HTTP(host, port) | |
result = None | |
errorCode = 0 | |
content_type, body = encode_request(cellId, lac) | |
http.putrequest('POST', page) | |
http.putheader('Content-Type', content_type) | |
http.putheader('Content-Length', str(len(body))) | |
http.endheaders() | |
http.send(body) | |
errcode, errmsg, headers = http.getreply() | |
result = http.file.read() | |
# could need some modification to get the answer: here I just need | |
# to get the 5 first characters | |
if (errcode == 200): | |
(a, b,errorCode, latitude, longitude, c, d, e) = unpack(">hBiiiiih",result) | |
latitude = latitude / 1000000.0 | |
longitude = longitude / 1000000.0 | |
return latitude, longitude | |
def encode_request(cellId, lac): | |
from struct import pack | |
content_type = 'application/binary' | |
body = pack('>hqh2sh13sh5sh3sBiiihiiiiii', 21, 0, 2, 'in', 13, "Nokia N95 8Gb", 5,"1.3.1", 3, "Web", 27, 0, 0, 3, 0, cellId, lac, 0, 0, 0, 0) | |
return content_type, body | |
(mcc, mnc, lac, cellId) = location.gsm_location() | |
(latitude, longitude) = doLookup(cellId, lac, "www.google.com", 80) | |
print latitude | |
print longitude |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment