windows查看端口占用

查看谁占用端口

netstat -ano |findstr 6000

杀死pid

taskkill /pid XXXX

python中调用

import subprocess

def get_pid_by_port(port):
    try:
        pid = subprocess.check_output(f'netstat -ano |findstr {port}', shell=True).decode('utf-8').split('\n', 1)[0].split()[-1]
    except subprocess.CalledProcessError as e:
        #print("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
        pid = None
    return pid

def kill_process_by_pid(pid):
    if pid is None:
        return
    print(f'kill pid={pid}')
    try:
        subprocess.check_output(f'taskkill /F /FI "PID eq {pid}"', shell=True)
    except subprocess.CalledProcessError as e:
        print("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))

 

 
 
posted @ 2022-04-18 10:35  永远的幻想  阅读(49)  评论(0编辑  收藏  举报