def selenium_login(): from selenium import webdriver from selenium.webdriver.chrome.options import Options options = Options() # options.add_experimental_option("debuggerAddress", "127.0.0.1:9222") driver = webdriver.Chrome(options=options) print('caps:', driver.caps) debugger_address = driver.caps['goog:chromeOptions']['debuggerAddress'] print('debugger_address:', debugger_address) debug_host, debug_port = driver.caps['goog:chromeOptions']['debuggerAddress'].split(':') with open('debug_port.txt', 'w', encoding='utf-8') as f: f.write(debug_port + '\n')
def kill_port(): import os import subprocess """根据端口号杀死对应的进程""" if os.path.exists('debug_port.txt'): with open('debug_port.txt', 'r', encoding='utf-8') as f: port = f.read().strip() else: return # 根据端口号查询pid find_port = 'netstat -aon | findstr %s' % port process = subprocess.Popen(find_port, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="gbk", shell=True) text = process.stdout.read() # 提取pid text = [i.split(' ') for i in text.split('\n') if i] pids = [] for i in text: pid = [u for u in i if u] if str(port) in pid[1]: pids.append(pid[-1]) pids = list(set(pids)) # 杀死占用端口的pid for pid in pids: find_kill = 'taskkill -f -pid %s' % pid subprocess.Popen(find_kill)
def kill_port(): import os import subprocess import psutil """根据端口号杀死对应的进程""" if os.path.exists('debug_port.txt'): with open('debug_port.txt', 'r', encoding='utf-8') as f: port = f.read().strip() else: return port_pid = {} for i in psutil.net_connections(): pid = i.pid status = i.status port = i.laddr.port port_pid[port] = pid # 关闭占用端口的pid if port_pid.get(int(port)): find_kill = 'taskkill -f -pid %s' % port_pid[int(port)] subprocess.Popen(find_kill)
def kill_progress_pid(soft_name=None): import psutil # 根据程序名获取进程ID,并关闭进程,soft_name='chrome.exe' for p in psutil.process_iter(): try: if p.name().lower() != soft_name.lower(): continue process = psutil.Process(p.pid) process.kill() except: pass
文章来源地址https://www.toymoban.com/news/detail-440750.html
文章来源:https://www.toymoban.com/news/detail-440750.html
到了这里,关于python selenium 通过端口关闭进程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!