设为首页 加入收藏

TOP

Python实现的基于ADB的Android远程工具(五)
2015-04-07 15:30:41 来源: 作者: 【 】 浏览:223
Tags:Python 实现 基于 ADB Android 远程 工具
,? 'srch',
? ? ? ? ? ? 'call',? ? '^',? ? 'end',? 'none',
? ? ? ? ? ? ? '<',? 'ok',? ? ? '>',? 'vol+',
? ? ? ? ? ? 'none',? ? 'v',? 'none',? 'vol-',
? ? ? ? ? ? ? '1',? ? '2',? ? ? '3',? 'none',
? ? ? ? ? ? ? '4',? ? '5',? ? ? '6',? 'cam',
? ? ? ? ? ? ? '7',? ? '8',? ? ? '9', 'enter',
? ? ? ? ? ? ? '*',? ? '0',? ? ? '#'
? ? ? ? ? ? ]


# keyvalues is the key value list map with the keynames
# 0: no keys here
keyvalues = [ 3, 82, 4, 84,
? ? ? ? ? ? ? 5, 19, 6,? 0,
? ? ? ? ? ? ? 21,23,22, 24,
? ? ? ? ? ? ? 0, 20, 0, 25,
? ? ? ? ? ? ? 8,? 9,10, 0,
? ? ? ? ? ? ? 11,12,13, 27,
? ? ? ? ? ? ? 14,15,16, 66,
? ? ? ? ? ? ? 17, 7,18
? ? ? ? ? ? ]


# Print Hex Buffer
def hexdump(buf = None):
? ? if buf != None:
? ? ? ? pstr = ''
? ? ? ? cnt = 0
? ? ? ? for x in buf:
? ? ? ? ? ? if (cnt + 1) % 8 == 0:
? ? ? ? ? ? ? ? pstr = '%s%02X\n' % (pstr, x)
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? pstr = '%s%02X ' % (pstr, x)
? ? ? ? ? ? cnt = cnt + 1
? ? ? ? print pstr


# Read adb response, if 'OKAY' turn true
def readAdbResponse(s):
? ? if s != None:
? ? ? ? resp = s.recv(4)
? ? ? ? if DEBUG:
? ? ? ? ? ? print 'resp: %s' % repr(resp)


? ? if len(resp) != 4:
? ? ? ? print 'protocol fault (no status)'
? ? ? ? return False
? ?
? ? if resp == 'OKAY':
? ? ? ? return True
? ? elif resp == 'FAIL':
? ? ? ? resp = s.recv(4)
? ? ? ? if len(resp) < 4:
? ? ? ? ? ? print 'protocol fault (status len)'
? ? ? ? ? ? return False
? ? ? ? else:
? ? ? ? ? ? length = int(resp, 16)
? ? ? ? ? ? resp = s.recv(length)
? ? ? ? ? ? if len(resp) != length:
? ? ? ? ? ? ? ? print 'protocol fault (status read)'
? ? ? ? ? ? ? ? return False
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? print resp
? ? ? ? ? ? ? ? return False
? ? else:
? ? ? ? print "protocol fault (status %02x %02x %02x %02x?!)", (resp[0], resp[1], resp[2], resp[3])
? ? ? ? return False


? ? return False


# Send adb shell command
def adbshellcommand(cmd):
? ? reply = None
? ? s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


? ? # waiting adb server start
? ? while True:
? ? ? ? try:
? ? ? ? ? ? s.connect(('127.0.0.1', 5037))
? ? ? ? except:
? ? ? ? ? ? os.system('adb start-server')
? ? ? ? ? ? time.sleep(2)
? ? ? ? ? ? continue
? ? ? ? else:
? ? ? ? ? ? break


? ? req_msg = 'host:transport-any'
? ? s.sendall('%04x' % len(req_msg))
? ? s.sendall(req_msg)
? ? if not readAdbResponse(s):
? ? ? ? return None


? ? req_msg = 'shell:%s' % cmd
? ? if DEBUG:
? ? ? ? print '%s' % req_msg
? ? s.sendall('%04x' % len(req_msg))
? ? s.sendall(req_msg)
? ? if readAdbResponse(s):
? ? ? ? reply = s.recv(4096)
? ? ? ? if DEBUG:
? ? ? ? ? ? hexdump(bytearray(reply))
? ? s.close()
? ?
? ? return reply


# Convert buffer to Int
def getInt(tbuf = None):
? ? if (tbuf != None):
? ? ? ? if DEBUG:
? ? ? ? ? ? hexdump(bytearray(tbuf))
? ? ? ? if len(tbuf) < 4:
? ? ? ? ? ? print 'buff len < 4'
? ? ? ? ? ? return 0
? ? ? ? else:
? ? ? ? ? ? if DEBUG:
? ? ? ? ? ? ? ? print 'parse: %02x %02x %02x %02x' % (tbuf[0],tbuf[1],tbuf[2],tbuf[3])
? ? ? ? ? ? intnum = tbuf[0]
? ? ? ? ? ? intnum = intnum + tbuf[1]*0x100
? ? ? ? ? ? intnum = intnum + tbuf[2]*0x10000
? ? ? ? ? ? intnum = intnum + tbuf[3]*0x1000000
? ? ? ? ? ? if DEBUG:
? ? ? ? ? ? ? ? print 'INT: %08x' % intnum
? ? ? ? ? ? return intnum
? ? else:
? ? ? ? return 0


# Parse fb header from buffer
def readHeader(tfb, ver, buf):
? ? if DEBUG:
? ? ? ? print 'readHeader: ver = %d' % ver
? ? if ver == 16:
? ? ? ? tfb.fb_bpp = 16
? ? ? ? tfb.fb_size = getInt(buf[0:4])
? ? ? ? tfb.fb_width = getInt(buf[4:8])
? ? ? ? tfb.fb_height = getInt(buf[8:12])
? ? ? ? tfb.red_offset = 11
? ? ? ? tfb.red_length = 5
? ? ? ? tfb.blue_offset = 5
? ? ? ? tfb.blue_length = 6
? ? ? ? tfb.green_offset = 0
? ? ? ? tfb.green_length = 5
? ? ? ? tfb.alpha_offset = 0
? ? ? ? tfb.alpha_length = 0
? ? elif ver == 1:
? ? ? ? tfb.fb_bpp = getInt(bytearray(buf[0:4]))
? ? ? ? tfb.fb_size = getInt(bytearray(buf[4:8]))
? ? ? ? tfb.fb_width = getInt(bytearray(buf[8:12]))
? ? ? ? tfb.fb_height = getInt(bytearray(buf[12:16]))
? ? ? ? tfb.red_offset = getInt(bytearray(buf[16:20]))
? ? ? ? tfb.red_lengt

首页 上一页 2 3 4 5 6 7 8 下一页 尾页 5/11/11
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Python基础教程 - global关键字及.. 下一篇Python基础教程 - lambda关键字

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: