Hi List,
Can some one help me with pack and unpack of structs.I get some problem at runtime.
(a, b,errorCode, latitude, longitude, c, d, e) = unpack(">hBiiiiih",result)
The bove statement in the code gives bellow error
"unpack Requires a string argument of length 25"
from httplib import HTTP
from struct import *
import location
import urllib2
import sysinfo
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()
print'IMEI :',sysinfo.imei()
print "MCC :" + str(mcc)
print "MNC :" + str(mnc)
print "LAC :" + str(lac)
print "CID :" + str(cellId)
print 'Battery Level : ',sysinfo.battery()
print 'Signal Strength :',sysinfo.signal_bars()
(latitude, longitude) = doLookup(cellId, lac, "www.google.com", 80)
print latitude
print longitude

Reply With Quote



