近期學了一點Python,然後正好有一個手機同步工具的預研工作要完成。
要實現PC與手機的通信,首先要找到他們的通信協議,還好的是Android有完善的協議:ADB
ADB的程式碼是開源的,而且支援Windows平台,有現成的DLL可以呼叫: AdbWinApi.dll,AdbWinUsbApi.dll
好了,可以用VC搞定,但我想用Python試試一下,於是開始了苦逼的查資料+實驗的過程。
實驗過程就不多說了,由於上面的兩個DLL都是用C實現的,提供的頭文件也是C語言的,所以有了下面這個python測試程序(Python2.7):
import ctypes #自定义的GUID结构,有兴趣的可以自己研究用uuid模块 class GUID(ctypes.Structure): _fields_ = [("Data1", ctypes.c_ulong), ("Data2", ctypes.c_ushort), ("Data3", ctypes.c_ushort), ("Data4", ctypes.c_ubyte*8)] #自己定义的一个结构体,便于使用DLL接口 class AdbInterfaceInfo(ctypes.Structure): _fields_ = [("class_id", GUID), ("flags", ctypes.c_ulong), ("device_name", ctypes.c_wchar*800)] def strGUID(GUID): string = '' string = string + '%x' % buff.class_id.Data1 + '-%x' % buff.class_id.Data2 + '-%x' %buff.class_id.Data3 string = string + '-%x' % buff.class_id.Data4[0] string = string + '%x' % buff.class_id.Data4[1] string = string + '%x' % buff.class_id.Data4[2] string = string + '%x' % buff.class_id.Data4[3] string = string + '%x' % buff.class_id.Data4[4] string = string + '%x' % buff.class_id.Data4[5] string = string + '%x' % buff.class_id.Data4[6] string = string + '%x' % buff.class_id.Data4[7] return string dll = ctypes.cdll.LoadLibrary('AdbWinApi.dll') usb_class_id = GUID(0xF72FE0D4, 0xCBCB, 0x407d, (0x88, 0x14, 0x9e, 0xd6, 0x73, 0xd0, 0xdd, 0x6b)) enum_handle = dll.AdbEnumInterfaces(usb_class_id, ctypes.c_bool('true'), ctypes.c_bool('true'), ctypes.c_bool('true')) while(1): buff = AdbInterfaceInfo() size = ctypes.c_ulong(ctypes.sizeof(buff)) status = dll.AdbNextInterface(enum_handle, ctypes.byref(buff), ctypes.byref(size)) if status==1: #print "GUID = " + strGUID(buff.class_id) #print "status = " + str(status) #print "Name = " + str(buff.device_name) hAdbApi = dll.AdbCreateInterfaceByName(buff.device_name); if hAdbApi == 0: print 'AdbCreateInterfaceByName Fail' else: serial = ' '*128 pserial = ctypes.c_char_p() pserial.value = serial serial_len = ctypes.c_ulong(len(serial)) ret = dll.AdbGetSerialNumber(hAdbApi, pserial, ctypes.byref(serial_len), ctypes.c_bool('false')); if ret == 1: print 'Device Name: ' + '%s'% serial else: print 'Get Device Name Fail' else: print 'Finished' break import ctypes #自定义的GUID结构,有兴趣的可以自己研究用uuid模块 class GUID(ctypes.Structure): _fields_ = [("Data1", ctypes.c_ulong), ("Data2", ctypes.c_ushort), ("Data3", ctypes.c_ushort), ("Data4", ctypes.c_ubyte*8)] #自己定义的一个结构体,便于使用DLL接口 class AdbInterfaceInfo(ctypes.Structure): _fields_ = [("class_id", GUID), ("flags", ctypes.c_ulong), ("device_name", ctypes.c_wchar*800)] def strGUID(GUID): string = '' string = string + '%x' % buff.class_id.Data1 + '-%x' % buff.class_id.Data2 + '-%x' %buff.class_id.Data3 string = string + '-%x' % buff.class_id.Data4[0] string = string + '%x' % buff.class_id.Data4[1] string = string + '%x' % buff.class_id.Data4[2] string = string + '%x' % buff.class_id.Data4[3] string = string + '%x' % buff.class_id.Data4[4] string = string + '%x' % buff.class_id.Data4[5] string = string + '%x' % buff.class_id.Data4[6] string = string + '%x' % buff.class_id.Data4[7] return string dll = ctypes.cdll.LoadLibrary('AdbWinApi.dll') usb_class_id = GUID(0xF72FE0D4, 0xCBCB, 0x407d, (0x88, 0x14, 0x9e, 0xd6, 0x73, 0xd0, 0xdd, 0x6b)) enum_handle = dll.AdbEnumInterfaces(usb_class_id, ctypes.c_bool('true'), ctypes.c_bool('true'), ctypes.c_bool('true')) while(1): buff = AdbInterfaceInfo() size = ctypes.c_ulong(ctypes.sizeof(buff)) status = dll.AdbNextInterface(enum_handle, ctypes.byref(buff), ctypes.byref(size)) if status==1: #print "GUID = " + strGUID(buff.class_id) #print "status = " + str(status) #print "Name = " + str(buff.device_name) hAdbApi = dll.AdbCreateInterfaceByName(buff.device_name); if hAdbApi == 0: print 'AdbCreateInterfaceByName Fail' else: serial = ' '*128 pserial = ctypes.c_char_p() pserial.value = serial serial_len = ctypes.c_ulong(len(serial)) ret = dll.AdbGetSerialNumber(hAdbApi, pserial, ctypes.byref(serial_len), ctypes.c_bool('false')); if ret == 1: print 'Device Name: ' + '%s'% serial else: print 'Get Device Name Fail' else: print 'Finished' break
上面這個簡單的Python程式碼,可以透過AdbWinApi.dll和AdbWinUsbApi.dll這兩個DLL來找出你PC上正連接的Android裝置。
只呼叫了3個DLL接口,但目的已經達到,可以得出下面的結論:
使用Python調用DLL的方式來實現ADB工具是可行的,當然麻煩是不會少的了。
寫在最後,Python調用C寫的DLL還是比較麻煩的,特別是參數傳遞,尤其是指針的處理,這方面還得依靠ctypes模組。 。 。

2小時內可以學會Python的基本編程概念和技能。 1.學習變量和數據類型,2.掌握控制流(條件語句和循環),3.理解函數的定義和使用,4.通過簡單示例和代碼片段快速上手Python編程。

Python在web開發、數據科學、機器學習、自動化和腳本編寫等領域有廣泛應用。 1)在web開發中,Django和Flask框架簡化了開發過程。 2)數據科學和機器學習領域,NumPy、Pandas、Scikit-learn和TensorFlow庫提供了強大支持。 3)自動化和腳本編寫方面,Python適用於自動化測試和系統管理等任務。

兩小時內可以學到Python的基礎知識。 1.學習變量和數據類型,2.掌握控制結構如if語句和循環,3.了解函數的定義和使用。這些將幫助你開始編寫簡單的Python程序。

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Python3.6環境下加載Pickle文件報錯:ModuleNotFoundError:Nomodulenamed...

如何解決jieba分詞在景區評論分析中的問題?當我們在進行景區評論分析時,往往會使用jieba分詞工具來處理文�...

如何使用正則表達式匹配到第一個閉合標籤就停止?在處理HTML或其他標記語言時,常常需要使用正則表達式來�...


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3 Linux新版
SublimeText3 Linux最新版

Safe Exam Browser
Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。