Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解决打包exe无法调用wcf.exe #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions clients/python/wcferry/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from threading import Thread
from time import sleep
from typing import Callable, Dict, List, Optional
import subprocess


import pynng
import requests
Expand Down Expand Up @@ -65,7 +67,11 @@ def __init__(self, host: str = None, port: int = 10086, debug: bool = True) -> N
self._local_mode = False
self._is_running = False
self._is_receiving_msg = False
self._wcf_root = os.path.abspath(os.path.dirname(__file__))
if getattr(sys, 'frozen', False):
# 如果程序是被 PyInstaller 打包的,则使用这个路径
self._wcf_root = sys._MEIPASS
else:
self._wcf_root = os.path.abspath(os.path.dirname(__file__))
self._dl_path = f"{self._wcf_root}/.dl"
os.makedirs(self._dl_path, exist_ok=True)
self.LOG = logging.getLogger("WCF")
Expand All @@ -75,11 +81,20 @@ def __init__(self, host: str = None, port: int = 10086, debug: bool = True) -> N
if host is None:
self._local_mode = True
self.host = "127.0.0.1"
cmd = fr'"{self._wcf_root}\wcf.exe" start {self.port} {"debug" if debug else ""}'
if os.system(cmd) != 0:
# 构建命令
cmd = [f"{self._wcf_root}\\wcf.exe", "start", str(port)]
if debug:
cmd.append("debug")

# 使用 subprocess 执行命令
return_code = subprocess.call(cmd)

# 检查返回码
if return_code != 0:
self.LOG.error("初始化失败!")
os._exit(-1)


self.cmd_url = f"tcp://{self.host}:{self.port}"

# 连接 RPC
Expand Down