Python在win环境下杀掉指定端口示例代码

import os
import re

'''
功能:python实现杀掉指定端口
win10系统环境下
python版本:3.6
'''
def kill_port(port):
    # 查找端口的pid
    find_port = 'netstat -aon | findstr %s' % port
    result = os.popen(find_port)
    text = result.read()
    pid = re.findall("LISTENING\s+([\s\S+]*?)\s+", text)
    if pid:
        # 占用端口的pid
        find_kill = 'taskkill -f -pid %s' % pid[0]
        print(find_kill)
        result = os.popen(find_kill)
        print(result.read())
        
if __name__ == "__main__":
    kill_port(5001)
发布了130 篇原创文章 · 获赞 84 · 访问量 94万+

猜你喜欢

转载自blog.csdn.net/qq_32502511/article/details/103729734
今日推荐