1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
"""
device_upgrade_server.py
python3, ubuntu16.04, mysql.
简单的升级服务,无数据校验等,仅供参考。 升级的配置存在mysql,是由另外的网页程序实现的。
需要安装sqlalchemy和gevent pip3 install sqlalchemy pip3 install gevent
mysql的python接口 apt-get install python3-mysql.connector
"""
from gevent.server import DatagramServer from gevent import monkey monkey.patch_all()
import gevent import os import sys import time import datetime import re import traceback import logging
logging.basicConfig(level = logging.DEBUG) dbg = logging.debug
def print_exception_info(): traceback.print_exc()
import sqlalchemy from sqlalchemy import * from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker
DB = 'mysql+mysqlconnector://name:[email protected]:3306/device_framework_upgrade' dbengine = create_engine(DB, echo = False, pool_size = 512, max_overflow = 0, pool_timeout = 0, pool_recycle = 5) Session = sessionmaker(bind = dbengine)
'''
device's data look like "862991443753386,A9335_V1723_B3633_WD1,1.1.8" and "Get6,12"
数据格式
1.recv [imei,project_name,version] 2.send [LUAUPDATE,upgrade_id,package_count,last_package_size]
3.recv [Getn,upgrade_id] 4.send [data]
...
'''
PACKAGE_SIZE = 1022
start_cmd_pattern = re.compile("^(\d{15}),(\w+),(\d+\.\d+\.\d+)$") get_cmd_pattern = re.compile("^Get(\d+),(\d+)$")
class SingleUpgradeServer(DatagramServer): def __init__(self, *args, **kwargs): DatagramServer.__init__(self, *args, **kwargs) def handle(self, data_org, address): try: dbg('=================================================') dbg(('=== %s %s: got data_org') % (time.ctime(), address[0])) dbg('=================================================') dbsession = Session() data = data_org.decode('ascii') dbg(data) function = None reply = bytearray()
match = start_cmd_pattern.match(data) if match: function = 0 else: match = get_cmd_pattern.match(data) if match: function = 1
dbg('function = {}'.format(function)) if function == 0: imei, project_name, version = match.groups() upgrade = dbsession.execute(text("select * from device_framework_upgrade.single_upgrade where project_name = :project_name and status = 1"), {"project_name":project_name}).fetchone() if upgrade is None: reply = "no upgrade".encode('ascii') dbg("no upgrade")
in_range = False imei_ranges = dbsession.execute(text("select * from device_framework_upgrade.single_upgrade_imei_range where upgrade_id = :upgrade_id"), {"upgrade_id":upgrade.id}).fetchall()
for imei_range in imei_ranges: if imei_range.starting_imei <= imei and imei <= imei_range.ending_imei: in_range = True break
if not in_range: reply = "imei range error".encode('ascii') dbg("range error") v1, v2, v3 = upgrade["version"].split('.') server_version = (int(v1) << 16) + (int(v2) << 8) + int(v3) v1, v2, v3 = version.split('.') device_version = (int(v1) << 16) + (int(v2) << 8) + int(v3) if device_version >= server_version: reply = "version error".encode('ascii') dbg("version error") else: file_path = upgrade['file_path'] dbg(file_path) file_size = os.path.getsize(file_path) last_package_size = file_size % PACKAGE_SIZE package_count = int(file_size / PACKAGE_SIZE) if last_package_size != 0: package_count += 1 dbg((file_size, package_count, last_package_size)) reply = ("LUAUPDATE,%d,%d,%d" % (upgrade["id"], package_count, last_package_size)).encode('ascii') elif function == 1: index, upgrade_id = match.groups() dbg("get") upgrade = dbsession.execute(text("select * from device_framework_upgrade.single_upgrade where id = :id and status = 1"), {"id":int(upgrade_id)}).fetchone() file_path = upgrade['file_path'] dbg("file is %s" % file_path) fd = open(file_path, 'rb') fd.seek((int(index) - 1) * PACKAGE_SIZE) reply = bytearray([int(int(index) / 256), int(index) % 256]) + fd.read(PACKAGE_SIZE) else: reply = "unknown function".encode('ascii') except: dbg("error") print_exception_info() reply = "error all".encode('ascii') finally: self.socket.sendto(reply, address) if dbsession: dbsession.close()
if __name__ == '__main__': dbg('device_upgrade_server.py receiving datagrams on %s:%d' % ('', 2234)) try: SingleUpgradeServer('%s:%d' % ('', 2234)).serve_forever() except: print_exception_info()
|