Skip to content

Commit

Permalink
Merge pull request #129 from Tencent/feature/adb-server-port
Browse files Browse the repository at this point in the history
支持从环境变量中获取ADB Server端口号
  • Loading branch information
drunkdream authored Jun 6, 2024
2 parents 8b703aa + 031f9c5 commit 0120202
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
19 changes: 11 additions & 8 deletions qt4a/androiddriver/adb.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@
general_encode,
time_clock,
get_command_path,
get_adb_server_port
)

try:
import _strptime # time.strptime() is not thread-safed, so import _strptime first, otherwise it raises an AttributeError: _strptime_time
except:
pass
# try:
# import _strptime # time.strptime() is not thread-safed, so import _strptime first, otherwise it raises an AttributeError: _strptime_time
# except:
# pass

cur_path = os.path.dirname(os.path.abspath(__file__))


Expand All @@ -62,14 +64,15 @@ def get_adb_path():
adb_path = get_adb_path()


def is_adb_server_opend(host="localhost"):
def is_adb_server_opend(host="localhost", port=None):
"""判断ADB Server是否开启
"""
import socket
port = port or get_adb_server_port()

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind((host, 5037))
sock.bind((host, port))
sock.close()
return False
except Exception:
Expand Down Expand Up @@ -181,9 +184,9 @@ def open_device(name):

return LocalADBBackend(device_host, name)

def __init__(self, device_host, device_name, port=5037):
def __init__(self, device_host, device_name, port=None):
self._device_host = device_host
self._device_host_port = port
self._device_host_port = port or get_adb_server_port()
self._device_name = device_name
self._adb_client = ADBClient.get_client(
self._device_host, self._device_host_port
Expand Down
8 changes: 4 additions & 4 deletions qt4a/androiddriver/adbclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import struct
import threading
from io import BytesIO
from qt4a.androiddriver.util import logger, utf8_encode, TimeoutError
from qt4a.androiddriver.util import logger, utf8_encode, get_adb_server_port, TimeoutError

SYNC_DATA_MAX = 64 * 1024

Expand Down Expand Up @@ -204,14 +204,14 @@ class ADBClient(object):

instance_dict = {}

def __init__(self, server_addr="127.0.0.1", server_port=5037):
def __init__(self, server_addr="127.0.0.1", server_port=None):
self._server_addr = server_addr
self._server_port = server_port
self._server_port = server_port or get_adb_server_port()
self._sock = None
self._lock = threading.Lock()

@staticmethod
def get_client(host, port=5037):
def get_client(host, port=None):
"""根据主机名获取ADBClient实例
"""
return ADBClient(host, port)
Expand Down
8 changes: 8 additions & 0 deletions qt4a/androiddriver/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,14 @@ def set_default_encoding(code="utf8"):
sys.setdefaultencoding(code)


def get_adb_server_port():
"""获取adb server端口"""
port = os.environ.get("ANDROID_ADB_SERVER_PORT")
if port:
return int(port)
return 5037


def get_command_path(command):
sep = ":"
if sys.platform == "win32":
Expand Down

0 comments on commit 0120202

Please sign in to comment.