• python 脚本 解决 windows 端口占用问题


    使用Python执行脚本即可

    import os
    
    # 无论合适输入q 即退出  quit
    global_command_exit = 'q'
    
    
    def find_and_kill(port):
        """
        通过端口号查找指定的进程,并提供展示进程信息和 杀掉进程的功能.
        :param port:  待操作的端口号
        """
        # 无法获取返回值
        # result = os.system('netstat -ano | findstr "10000" ')
        result = os.popen(f'netstat -ano | findstr "{port}" ')
        find_result = result.read()
        if len(find_result.strip()) is 0:
            print("当前端口号未被占用")
            return None
        print(f'占用当前端口的程序如下:\n{find_result}')
        # 将所有的进程信息分割开.
        processors = find_result.split("\n")
        # 过滤掉空白行
        real_processors = []
        for processor in processors:
            if processor.strip() is "":
                continue
            # 将有效的进程信息放入最终的结果集中.
            real_processors.append(processor)
    
        # 提示要 kill 哪个 pid
        pid_str = input('请输入要查看的进程PID:')
    
        while pid_str.strip() is '':
            pid_str = input('pid 不能是空,请重新输入:')
    
        if pid_str.strip() is global_command_exit:
            print(f'exit process with command q')
            return None
        # tasklist | findstr 20648
        process_info = os.popen(f'qprocess {pid_str}')
        print(f'进程信息:********************************************\n{process_info.read()}')
    
        pid_str = input('请输入要杀掉的进程PID:')
        while pid_str.strip() is '':
            pid_str = input('pid 不能是空,请重新输入:')
    
        if pid_str.strip() is global_command_exit:
            print(f'exit process with command q')
            return None
        # 杀进程.
        # taskkill -t -f /pid
        process_info = os.popen(f'taskkill -t -f /pid  {pid_str}')
        print(f'进程信息:********************************************\n{process_info.read()}')
        print(f'任务结束')
    
    
    if __name__ == '__main__':
        target_pid = input('请输入端口号:')
        while target_pid.strip() is '':
            target_pid = input('端口号 不能是空,请重新输入:')
    
        if target_pid.strip() is global_command_exit:
            print(f'exit process with command q')
        else:
            find_and_kill(target_pid)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
  • 相关阅读:
    LyScript 批量搜索反汇编特征
    目标分割技术-语义分割总览
    OpenCV图像裁剪:使用&运算符在OpenCV图像裁剪时进行边界检查
    Qt5开发从入门到精通——第三篇(窗口篇——停靠窗口)
    前端面试题日常练-day40 【面试题】
    Docker - 安装
    10分钟搞定 Spring 批处理组件 —— spring-batch
    Docker之查看并获取最新Ubuntu镜像(十)
    测开 - 项目篇 - 细节狂魔
    -完全数-
  • 原文地址:https://blog.csdn.net/qq_39203959/article/details/126142360