python打包exe注意事项

目录

打包后运行exe发现窗口闪退,无法捕获到错误信

打包参数加-F,将所有依赖包都打包进exe文件,这样只需要exe就可以运行了

打包后的路径读取(sys.executable返回打包后exe文件所在目录)

最后新的ssh端口转发代码如下:(旧版路径)


打包后运行exe发现窗口闪退,无法捕获到错误信

可以打开cmd命令窗口,然后在exe对应目录下执行,例如

打包参数加-F,将所有依赖包都打包进exe文件,这样只需要exe就可以运行了

pyinstaller forward.py -F -i logo.ico 命令打包-F参数直接打包依赖到exe

打包后的路径读取(sys.executable返回打包后exe文件所在目录)

# 获取项目根目录(如果执行的是打包的exe文件,sys会带有frozen属性)
def app_path():
    if hasattr(sys, 'frozen'):
        return os.path.dirname(sys.executable) #使用pyinstaller打包后的exe目录
    return os.path.dirname(__file__)

最后新的ssh端口转发代码如下:(旧版路径

from sshtunnel import SSHTunnelForwarder
import threading
import socket
import os
import sys
import json

# 获取项目根目录
def app_path():
    if hasattr(sys, 'frozen'):
        return os.path.dirname(sys.executable) #使用pyinstaller打包后的exe目录
    return os.path.dirname(__file__)

def get_host_ip():
    """
    查询本机ip地址
    :return: ip
    """
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 80))
        ip = s.getsockname()[0]
    finally:
        s.close()
    return ip

app_path = app_path()

#局域网ip
local_host = get_host_ip()

def forwardSSH(user,password,ssh_host,ssh_port,local_host,local_port,remote_host,remote_port):
    server = SSHTunnelForwarder(
            ssh_username=user,
            ssh_password=password,
            ssh_address_or_host=(ssh_host, ssh_port),
            local_bind_address=(local_host, local_port),
            remote_bind_address=(remote_host, remote_port)
    )
    # 守护线程
    server.daemon_forward_servers=False
    server.start()
    if server.is_active:
        print('本地端口{}:{}已转发至远程端口{}:{}'.format(local_host,server.local_bind_port,remote_host,remote_port))
    else :
        print('本地端口{}:{}转发失败,请重试')


#读取json配置文件
try:
    with open(app_path + '/config.json','r') as f:
        load_dict = json.load(f)
        ssh_config = load_dict['ssh']
        user = ssh_config['user']
        password = ssh_config['password']
        ssh_host = ssh_config['ssh_host']
        ssh_port = ssh_config['ssh_port']
        if 'local_host' in ssh_config:
            local_host = ssh_config['local_host']
        t = locals()
        i = 1
        for item in ssh_config['list']:
            if 'user' in ssh_config:
                user = ssh_config['user']
            if 'password' in ssh_config:
                password = ssh_config['password']
            if 'ssh_host' in ssh_config:
                ssh_host = ssh_config['ssh_host']
            if 'ssh_port' in ssh_config:
                ssh_port = ssh_config['ssh_port']
            local_port = item['local_port']
            remote_host = item['remote_host']
            remote_port = item['remote_port']
            t[str(i)] = threading.Thread(forwardSSH(user, password, ssh_host, ssh_port, local_host, local_port, remote_host, remote_port))
            t[str(i)].start()
except IndexError as e:
    print('配置文件不合法: ', e)

config.json配置如下:(和exe文件同级目录下)

{
  "ssh": {
    "user": "",
    "password": "",
    "ssh_host": "",
    "ssh_port": 22,
    "local_host": "127.0.0.1",     #可选,不配置就默认使用局域网ip
    "list": [
      {
        "user": "",                #可选
        "password": "",            #可选
        "ssh_host": "",            #可选
        "ssh_port": 22,            #可选
        "local_port": 6379,
        "remote_host": "",
        "remote_port": 6379
      },
      {
        "local_port": 3306,
        "remote_host": "",
        "remote_port": 3306
      }
    ]
  }
}

命令行运行工具,可以发现工具运行时的错误,如果是本地已经占用的端口,转发时会报错

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

end for time

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值