python编译exe文件

小demo:

# -*- coding: utf-8 -*-
"""第一个程序"""
import subprocess
print ("Hello World!" )
print ("Hello Again")
print ("I like typing this.")
print ("This is fun.")
print ('Yay! Printing.')
print ("I'd much rather you 'not'.")
print ('I "said" do not touch this.')
import  os
result = os.popen("ipconfig")
# print (result.read())
# raw_data = subprocess.Popen("netstat -ano", stdout=subprocess.PIPE, shell=True)
# raw_data = raw_data.stdout.read().decode("gbk").split('\r\n')[4:]  # 去掉4行头
# print("chuliqian", raw_data)
def get_port_info():
    raw_data = subprocess.Popen("netstat -ano", stdout=subprocess.PIPE, shell=True)
    raw_data = raw_data.stdout.read().decode("gbk").split('\r\n')[4:]  # 去掉4行头
    print("chuliqian", raw_data)
    for line in raw_data[:]:  # 要加个raw_data[:]
        lineline = line.split("  ")
        print(lineline)
        black_list = ["UDP", " [::]:0", "[::]:0", " TIME_WAIT", "TIME_WAIT", "CLOSE_WAIT", " CLOSE_WAIT",
                      " ESTABLISHED", "ESTABLISHED", " SYN_SENT", "SYN_SENT"]
        for k, v in enumerate(black_list):
            if v in lineline:
                raw_data.remove(line)

    while '' in raw_data:
        raw_data.remove('')
    print("chulihou:", raw_data)

    port_list = []
    for line in raw_data:
        port_dict = {}
        port_info = line.split()
        print(port_info)
        port_num = port_info[1].split(':')[-1]    # 本机开放的端口
        listen_addr = port_info[1].split(':')[0]  # 本机IP地址
        pid = int(port_info[4])  # windows的PID号
        cmd = 'tasklist | findstr "%s"' % (pid)  # 根据PID号执行命令
        process_data = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)  # 执行命令获取PID的进程信息
        process_name = process_data.stdout.read().decode("gbk").split(" ")[0].split(".")[0]  # 取出PID所对应的进程名字mysqld
    #    port = GetpidInfo(pid)  # 实现根据进程名字对应用名字和应用版本号提取
    #     exe_path = port.get_exe_path()
    #     service_name = port.get_exe_name(exe_path)
    #     service_version = port.get_exe_version(exe_path)
        port_dict['port_num'] = port_num
        port_dict['listen_addr'] = listen_addr
        port_dict['PID'] = str(pid)
        port_dict['process_name'] = process_name  # 进程的名字
        # port_dict['service_name'] = service_name  # 应用的名字
        # port_dict['service_version'] = service_version  # 应用的版本
        if port_dict not in port_list:
            port_list.append(port_dict)
    port_list = [i for i in port_list if i]  # 去掉空字典
    print(port_list)
if __name__ == '__main__':
    get_port_info()

pip install pyinstaller

Anaconda3\Scripts>pyinstaller.exe -F C:\Users\liangyi\Desktop\pythonexedemo\hello.py

在Scripts 下生成一个2个文件夹

python编译exe文件_第1张图片
build 不用去管,可以直接删除。打开dist 文件
python编译exe文件_第2张图片
结束!

你可能感兴趣的:(开发)