我正在使用 adb_shell python 库通过 python 运行一些 adb 命令。
虽然在常规版本上一切正常,但异步版本无法连接。
这是同步版本中的代码:
adbkey = 'adbkey'
with open(adbkey) as f:
priv = f.read()
with open(adbkey + '.pub') as f:
pub = f.read()
signer = PythonRSASigner(pub, priv)
# Connect
device1 = AdbDeviceTcp('192.168.0.166', 5555, default_transport_timeout_s=9)
device1.connect(rsa_keys=[signer], auth_timeout_s=0.1)
device.shell("echo Hello")
以上按预期工作。
但是以下版本中断:
adbkey = 'adb_key'
with open(adbkey) as f:
priv = f.read()
with open(adbkey + '.pub') as f:
pub = f.read()
signer = PythonRSASigner(pub, priv)
# Connect
tcp = TcpTransportAsync('192.168.0.166', port=5555)
device = AdbDeviceAsync(tcp)
await device.connect(rsa_keys=[signer], auth_timeout_s=60)
它生成一个 TcpTimeoutException:
Traceback (most recent call last):
File "C:\Users\User\Documents\AdbProject\asyncadb.py", line 77, in <module>
asyncio.get_event_loop().run_until_complete(main())
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 642, in run_until_complete
return future.result()
File "C:\Users\User\Documents\AdbProject\asyncadb.py", line 23, in main
await device.connect(rsa_keys=[signer], auth_timeout_s=60)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\adb_shell\adb_device_async.py", line 642, in connect
self._available, self._maxdata = await self._io_manager.connect(self._banner, rsa_keys, auth_timeout_s, auth_callback, adb_info)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\adb_shell\adb_device_async.py", line 235, in connect
_, _, maxdata, _ = await self._read_expected_packet_from_device([constants.CNXN], adb_info)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\adb_shell\adb_device_async.py", line 373, in _read_expected_packet_from_device
cmd, arg0, arg1, data = await self._read_packet_from_device(adb_info)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\adb_shell\adb_device_async.py", line 451, in _read_packet_from_device
msg = await self._read_bytes_from_device(constants.MESSAGE_SIZE, adb_info)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\adb_shell\adb_device_async.py", line 407, in _read_bytes_from_device
temp = await self._transport.bulk_read(length, adb_info.transport_timeout_s)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\adb_shell\transport\tcp_transport_async.py", line 106, in bulk_read
raise TcpTimeoutException(msg) from exc
adb_shell.exceptions.TcpTimeoutException: Reading from 192.168.0.166:5555 timed out (60 seconds)
因为它是异步的,我是否打算做一些不同的事情?
PS 两个版本都适用于模拟器。