修改port_is_using()方法

This commit is contained in:
g1879 2023-01-13 11:36:24 +08:00
parent 8f1048da1d
commit 0b60c6c561
2 changed files with 10 additions and 15 deletions

View File

@ -10,7 +10,6 @@ from re import split, search, sub
from shutil import rmtree from shutil import rmtree
from subprocess import Popen from subprocess import Popen
from time import perf_counter, sleep from time import perf_counter, sleep
from typing import Union
from zipfile import ZipFile from zipfile import ZipFile
from urllib.parse import urlparse, urljoin, urlunparse from urllib.parse import urlparse, urljoin, urlunparse
from requests import get as requests_get from requests import get as requests_get
@ -494,24 +493,17 @@ def is_js_func(func):
return False return False
def _port_is_using(ip: str, port: str) -> Union[bool, None]: def port_is_using(ip, port):
"""检查端口是否被占用 \n """检查端口是否被占用 \n
:param ip: 浏览器地址 :param ip: 浏览器地址
:param port: 浏览器端口 :param port: 浏览器端口
:return: bool :return: bool
""" """
import socket from socket import socket, AF_INET, SOCK_STREAM
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket(AF_INET, SOCK_STREAM)
result = s.connect_ex((ip, int(port)))
try:
s.connect((ip, int(port)))
s.shutdown(2)
return True
except socket.error:
return False
finally:
if s:
s.close() s.close()
return True if result == 0 else False
def connect_browser(option): def connect_browser(option):
@ -528,7 +520,7 @@ def connect_browser(option):
if ip not in ('127.0.0.1', 'localhost'): if ip not in ('127.0.0.1', 'localhost'):
return None, None 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 chrome_path = get_exe_from_port(port) if chrome_path == 'chrome' and system_type == 'windows' else chrome_path
return chrome_path, None return chrome_path, None

View File

@ -54,6 +54,9 @@ def make_absolute_link(link, page: BasePage = None) -> str: ...
def is_js_func(func: str) -> bool: ... def is_js_func(func: str) -> bool: ...
def port_is_using(ip: str, port: str) -> bool: ...
def connect_browser(option: DriverOptions) -> tuple: ... def connect_browser(option: DriverOptions) -> tuple: ...