From 0b60c6c56154228dfbc748c6f825123c388ff4de Mon Sep 17 00:00:00 2001 From: g1879 Date: Fri, 13 Jan 2023 11:36:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9port=5Fis=5Fusing()=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/common.py | 22 +++++++--------------- DrissionPage/common.pyi | 3 +++ 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/DrissionPage/common.py b/DrissionPage/common.py index 09d1bcb..f62d962 100644 --- a/DrissionPage/common.py +++ b/DrissionPage/common.py @@ -10,7 +10,6 @@ from re import split, search, sub from shutil import rmtree from subprocess import Popen from time import perf_counter, sleep -from typing import Union from zipfile import ZipFile from urllib.parse import urlparse, urljoin, urlunparse from requests import get as requests_get @@ -494,24 +493,17 @@ def is_js_func(func): return False -def _port_is_using(ip: str, port: str) -> Union[bool, None]: +def port_is_using(ip, port): """检查端口是否被占用 \n :param ip: 浏览器地址 :param port: 浏览器端口 :return: bool """ - import socket - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - - try: - s.connect((ip, int(port))) - s.shutdown(2) - return True - except socket.error: - return False - finally: - if s: - s.close() + from socket import socket, AF_INET, SOCK_STREAM + s = socket(AF_INET, SOCK_STREAM) + result = s.connect_ex((ip, int(port))) + s.close() + return True if result == 0 else False def connect_browser(option): @@ -528,7 +520,7 @@ def connect_browser(option): if ip not in ('127.0.0.1', 'localhost'): return None, None - if _port_is_using(ip, port): + if port_is_using(ip, port): chrome_path = get_exe_from_port(port) if chrome_path == 'chrome' and system_type == 'windows' else chrome_path return chrome_path, None diff --git a/DrissionPage/common.pyi b/DrissionPage/common.pyi index 6d62a4d..4d9551e 100644 --- a/DrissionPage/common.pyi +++ b/DrissionPage/common.pyi @@ -54,6 +54,9 @@ def make_absolute_link(link, page: BasePage = None) -> str: ... def is_js_func(func: str) -> bool: ... +def port_is_using(ip: str, port: str) -> bool: ... + + def connect_browser(option: DriverOptions) -> tuple: ...