目录
打包参数加-F,将所有依赖包都打包进exe文件,这样只需要exe就可以运行了
打包后的路径读取(sys.executable返回打包后exe文件所在目录)
打包后运行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
}
]
}
}
命令行运行工具,可以发现工具运行时的错误,如果是本地已经占用的端口,转发时会报错