python關閉佔用埠
阿新 • • 發佈:2018-12-12
# -*- coding: UTF-8 -*- import os import re port = 8080 def kill_process(): ret = os.popen("netstat -nao|findstr " + str(port)) #注意解碼方式和cmd要相同,即為"gbk",否則輸出亂碼 str_list = ret.read().decode('gbk') ret_list = re.split('',str_list) try: process_pid = list(ret_list[0].split())[-1] os.popen('taskkill /pid ' + str(process_pid) + ' /F') print "埠已被釋放" except: print "埠未被使用" if __name__ == '__main__': kill_process() # output1 = os.popen('ipconfig') # print output1.read().decode('gbk')