PC上检测是否更新并获取泛泰手机OTA包,更新通用获取脚本(7.29)

首先看一下检测更新时手机发送的数据

POST /apkmanager/Process/sky_station_30_server.php HTTP/1.1
Connection: Keep-Alive
Content-Type: multipart/form-data;boundary=pkgname
User-Agent: Dalvik/1.4.0 (Linux; U; Android 2.3.5; IM-A820L Build/GRJ90)
Host: apkmanager.skyservice.co.kr
Content-Length: 616
Accept-Encoding: gzip


--pkgname
Content-Disposition:form-data;name="file";filename=GET_PKG_DETAIL_INFO.xml

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><SKY_REQUEST><SKY_CMD>GET_PKG_DETAIL_INFO</SKY_CMD><TERMINAL_NAME>IM-A820L</TERMINAL_NAME><TERMINAL_CARRIER>LGT-KOR</TERMINAL_CARRIER><TERMINAL_LOCATION>zh_CN</TERMINAL_LOCATION><TERMINAL_MANUFAC>PANTECH</TERMINAL_MANUFAC><BOARD_SOFT_VER>S1231133</BOARD_SOFT_VER><SKY_STATION_VER>875</SKY_STATION_VER><TERMINAL_SERIAL>******</TERMINAL_SERIAL><TERMINAL_IMEI>***************</TERMINAL_IMEI><PKG_NAME>com.pantech.firmware.bin.IM-A820L</PKG_NAME></SKY_REQUEST>
--pkgname--

其中*号是隐去的个人信息

得到的反馈是

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><SKY_RESPONSE><SKY_CMD>GET_PKG_DETAIL_INFO</SKY_CMD><APK_INFO><PKG_NAME>com.pantech.firmware.bin.IM-A820L</PKG_NAME><APP_NAME>IM-A820L</APP_NAME><PKG_VERSIONCODE>1.39</PKG_VERSIONCODE><PKG_VERSIONNAME>S1231139</PKG_VERSIONNAME><GET_INFO_URL>87b82857c5.apk</GET_INFO_URL><PKG_SIZE>16450257</PKG_SIZE><PKG_DATE>2012-02-10 09:44:04</PKG_DATE><DATA_INIT>0</DATA_INIT><UNZIP_SIZE>0</UNZIP_SIZE><CHECKSUM>07260af6cdde7c991f2f300270447fbc</CHECKSUM></APK_INFO></SKY_RESPONSE>

可以看出有升级到S1231139的包,大小是16450257

那么我们就只要在PC上构建出这个POST就行了

而其中必须的信息是

POST /apkmanager/Process/sky_station_30_server.php HTTP/1.1
Content-Type: multipart/form-data;boundary=pkgname
Host: apkmanager.skyservice.co.kr


--pkgname
Content-Disposition:form-data;name="file";filename=GET_PKG_DETAIL_INFO.xml

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><SKY_REQUEST><SKY_CMD>GET_PKG_DETAIL_INFO</SKY_CMD><TERMINAL_NAME>手机型号</TERMINAL_NAME><BOARD_SOFT_VER>待更新版本</BOARD_SOFT_VER><PKG_NAME>com.pantech.firmware.bin.手机型号</PKG_NAME></SKY_REQUEST>
--pkgname--
那么我们就可以针对自己所需的型号版本进行查询了

我利用的便是Python的httplib模块构建POST提交表单

这里先提供一个Python小脚本作示范,大家可以根据自己熟悉的语言实现

import httplib
params = ("--pkgname\nContent-Disposition:form-data;name=\"file\";filename=GET_PKG_DETAIL_INFO.xml\n\n<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><SKY_REQUEST><SKY_CMD>GET_PKG_DETAIL_INFO</SKY_CMD><TERMINAL_NAME>IM-A820L</TERMINAL_NAME><BOARD_SOFT_VER>S1231133</BOARD_SOFT_VER><PKG_NAME>com.pantech.firmware.bin.IM-A820L</PKG_NAME></SKY_REQUEST>\n--pkgname--")
headers = {"Content-Type":"multipart/form-data;boundary=pkgname", "Connection":"Keep-Alive"}
conn = httplib.HTTPConnection("apkmanager.skyservice.co.kr")
conn.request("POST","/apkmanager/Process/sky_station_30_server.php",params,headers)
response = conn.getresponse()
data = response.read()
print data
conn.close()

再看一下获取OTA包所发送的数据

POST /apkmanager/Process/sky_station_30_server.php HTTP/1.1
Connection: Keep-Alive
Content-Type: multipart/form-data;boundary=read_stream:com.pantech.firmware.bin.IM-A820L
Range: bytes=0-
User-Agent: Dalvik/1.4.0 (Linux; U; Android 2.3.5; IM-A820L Build/GRJ90)
Host: apkmanager.skyservice.co.kr
Content-Length: 792
Accept-Encoding: gzip


--read_stream:com.pantech.firmware.bin.IM-A820L
Content-Disposition:form-data;name="file";filename=GET_PKG_DOWN.xml

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><SKY_REQUEST><SKY_CMD>GET_PKG_DOWN</SKY_CMD><TERMINAL_NAME>IM-A820L</TERMINAL_NAME><TERMINAL_CARRIER>LGT-KOR</TERMINAL_CARRIER><TERMINAL_LOCATION>zh_CN</TERMINAL_LOCATION><TERMINAL_MANUFAC>PANTECH</TERMINAL_MANUFAC><BOARD_SOFT_VER>S1231133</BOARD_SOFT_VER><SKY_STATION_VER>875</SKY_STATION_VER><TERMINAL_SERIAL>>******</</TERMINAL_SERIAL><TERMINAL_IMEI>>***************</</TERMINAL_IMEI><SKIP_POSITION>0</SKIP_POSITION><PKG_NAME>com.pantech.firmware.bin.IM-A820L</PKG_NAME><PKG_VERSIONNAME>S1231139</PKG_VERSIONNAME><PKG_VERSIONCODE>1.39</PKG_VERSIONCODE></SKY_REQUEST>
--read_stream:com.pantech.firmware.bin.IM-A820L--

此时返回的就是133升级到139的OTA包了,格式为apk,其中就有update.zip,我们需要的就是它

其中必须的是

POST /apkmanager/Process/sky_station_30_server.php HTTP/1.1
Content-Type: multipart/form-data;boundary=read_stream:com.pantech.firmware.bin.手机型号
Host: apkmanager.skyservice.co.kr


--read_stream:com.pantech.firmware.bin.手机型号
Content-Disposition:form-data;name="file";filename=GET_PKG_DOWN.xml

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><SKY_REQUEST><SKY_CMD>GET_PKG_DOWN</SKY_CMD><TERMINAL_NAME>手机型号</TERMINAL_NAME><BOARD_SOFT_VER>带升级版本</BOARD_SOFT_VER><SKIP_POSITION>0</SKIP_POSITION><PKG_NAME>com.pantech.firmware.bin.手机型号</PKG_NAME><PKG_VERSIONNAME>可升级版本</PKG_VERSIONNAME></SKY_REQUEST>
--read_stream:com.pantech.firmware.bin.手机型号--

跟前面获取更新信息类似的,只不过这次就不能把收到的数据print出来了

再看一下获取OTA包的Python脚本

import httplib
params = ("--read_stream:com.pantech.firmware.bin.IM-A820L\nContent-Disposition:form-data;name=\"file\";filename=GET_PKG_DOWN.xml\n\n<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><SKY_REQUEST><SKY_CMD>GET_PKG_DOWN</SKY_CMD><TERMINAL_NAME>IM-A820L</TERMINAL_NAME><BOARD_SOFT_VER>S1231133</BOARD_SOFT_VER><SKIP_POSITION>0</SKIP_POSITION><PKG_NAME>com.pantech.firmware.bin.IM-A820L</PKG_NAME><PKG_VERSIONNAME>S1231139</PKG_VERSIONNAME></SKY_REQUEST>\n--read_stream:com.pantech.firmware.bin.IM-A820L--")
headers = {"Content-Type":"multipart/form-data;boundary=read_stream:com.pantech.firmware.bin.IM-A820L", "Connection":"Keep-Alive"}
conn = httplib.HTTPConnection("apkmanager.skyservice.co.kr")
conn.request("POST","/apkmanager/Process/sky_station_30_server.php",params,headers)
response = conn.getresponse()
f = open("update.apk", "wb")
while True:
	tmp = response.read(100*1024)
	if not tmp:break
	f.write(tmp)
f.close()
conn.close()

其中

while True:
	tmp = response.read(100*1024)
	if not tmp:break
	f.write(tmp)
是防止获取较大的OTA包时造成占用内存过大用的,每收到100K的数据便写入文件



这里再给大家一个只需要输入型号及版本就可获取OTA包的脚本

#-*-coding:utf-8-*-
import httplib
global model, version

def xmlInf(Elements):
	from xml.dom.minidom import getDOMImplementation
	impl = getDOMImplementation()
	SKY_REQUEST = impl.createDocument(None, "SKY_REQUEST", None)
	Request = SKY_REQUEST.documentElement
	for Element in Elements:
		Ele = SKY_REQUEST.createElement(Element[0])
		Ele.appendChild(SKY_REQUEST.createTextNode(Element[1]))
		Request.appendChild(Ele)
	SKY_REQUEST.appendChild(Request)
	return SKY_REQUEST.toxml(encoding='UTF-8')

def getinfo(ResponseStr):
	from xml.dom import minidom
	import time
	data = time.strftime('%X %x')+"\n"
	SKY_RESPONSE = minidom.parseString(ResponseStr).documentElement
	data += "---------------------------------------------------------\n"
	SKY_CMD = SKY_RESPONSE.getElementsByTagName("SKY_CMD")[0]
	data += SKY_CMD.childNodes[0].nodeValue + "\t" + model+"_"+version+"_to_\n"
	data += "---------------------------------------------------------\n"
	APK_INFO = SKY_RESPONSE.getElementsByTagName("APK_INFO")
	if str(APK_INFO) == "[]":
		print "无更新信息或型号版本输入错误\n"
		data = ""
		return data
	data += APK_INFO[0].nodeName + "\n"
	for node in APK_INFO[0].childNodes:
		if node.nodeType == node.ELEMENT_NODE:
			for info in APK_INFO:
				try:
					data += info.getElementsByTagName(node.nodeName)[0].nodeName
					if len(node.nodeName) >= 15:
						data += ":"
					else:
						data += ":\t" 
					data += str(info.getElementsByTagName(node.nodeName)[0].childNodes[0].nodeValue) + "\n"
				except:
					print "出现错误(可能有韩文),请查看Response.xml"
					o = open("Response.xml", 'wb')
					o.write(ResponseStr)
					o.close()
					data += "Error occured!!!\n"
	data += "---------------------------------------------------------\n"
	return data
	

if __name__ == "__main__":
	print "请输入手机型号(如IM-A820L):"
	model = raw_input()
	print "请输入待更新版本(如S1231150):"
	version = raw_input()
	RequestInf = [['SKY_CMD', 'GET_PKG_DETAIL_INFO'], ['TERMINAL_NAME', model], ['BOARD_SOFT_VER', version], ['PKG_NAME', 'com.pantech.firmware.bin.'+model]]
	xml = xmlInf(RequestInf)
	params = "--pkgname\nContent-Disposition:form-data;name=\"file\";filename=GET_PKG_DETAIL_INFO.xml\n\n" + xml + "\n--pkgname--"
	headers = {"Content-Type":"multipart/form-data;boundary=pkgname", "Connection":"Keep-Alive"}
	i = 0
	conn = httplib.HTTPConnection("apkmanager.skyservice.co.kr")
	data = ""
	while i < 10:
		try:
			conn.request(method="POST",url="/apkmanager/Process/sky_station_30_server.php",body=params,headers=headers)
			response = conn.getresponse()
			data = response.read()
			conn.close()
			break
		except:
			conn.close()
			i += 1
			print "连接出错,进行第"+str(i)+"次重试"
	if data != "":
		f = open("updateinfo.txt", "a")
		data = getinfo(data)
		f.write(data+"\n")
		f.close()
		
	if data != "":
		print data
		otaver = data[data.find("PKG_VERSIONNAME:")+16:][:data[data.find("PKG_VERSIONNAME:")+16:].find("\n")]
		size = data[data.rfind("PKG_SIZE:")+10:][:data[data.rfind("PKG_SIZE:")+10:].find("\n")]
		print "OTA升级版本:" + str(otaver) + "大小:" + str(size)
		RequestOTA = [['SKY_CMD', 'GET_PKG_DOWN'], ['TERMINAL_NAME', model], ['BOARD_SOFT_VER', version], ['PKG_NAME', 'com.pantech.firmware.bin.'+model], ['PKG_VERSIONNAME', otaver]]
		xml = xmlInf(RequestOTA)
		params = "--read_stream:com.pantech.firmware.bin." +model+ "\nContent-Disposition:form-data;name=\"file\";filename=GET_PKG_DOWN.xml\n\n" + xml + "\n--read_stream:com.pantech.firmware.bin." + model +"--"
		headers = {"Content-Type":"multipart/form-data;boundary=read_stream:com.pantech.firmware.bin." + model, "Connection":"Keep-Alive"};
		i = 0
		conn = httplib.HTTPConnection("apkmanager.skyservice.co.kr")
		data = ""
		while i < 10:
			try:
				conn.request(method="POST",url="/apkmanager/Process/sky_station_30_server.php",body=params,headers=headers)
				response = conn.getresponse()
				file = open(model+'_'+version+'_to_'+otaver+'.apk', 'wb')
				done = 0.
				while True:
					tmp = response.read(100*1024)
					if not tmp:break
					file.write(tmp)
					done += 100*1024
					if done/int(size)>1:
						print "\r100%    "
					else:
						print "\r" + str(round(done/int(size)*100,2)) + "%",
				file.close()
				conn.close()
				break
			except:
				conn.close()
				i += 1
				print "连接出错,进行第"+str(i)+"次重试"
		

运行效果如下

PC上检测是否更新并获取泛泰手机OTA包,更新通用获取脚本(7.29)_第1张图片



转载请注明出处:http://blog.csdn.net/su_ky/article/details/7728877


你可能感兴趣的:(Stream,python,脚本,手机,import,encoding)