Skip to content

多开管理

概述

PyWeWork 支持同时管理多个企业微信实例,每个实例通过 client_id 标识。

初始化参数

python
wework = WeWork(
    version="5.0.3.6005",
    smart=True,          # 自动接管/多开企业微信
    mutex=True,          # 启动时终止已有的 ww.exe 进程
    host="127.0.0.1",    # API 服务地址
    port=19089,          # API 服务端口
    server_host="127.0.0.1",  # 回调服务地址
    server_port=19000,         # 回调服务端口
    max_retry=10,        # 最大重试次数
    retry_interval=1,    # 重试间隔(秒)
    timeout=10           # 请求超时时间(秒)
)

smart 模式

smart=True 时,PyWeWork 会自动:

  1. 尝试接管已运行的企业微信 (open)
  2. 如果失败,尝试多开一个新实例 (multi_open)

手动管理

设置 smart=False 后可手动控制:

python
wework = WeWork("5.0.3.6005", smart=False)

# 接管已运行的企业微信
result = wework.open()
print(result)

# 或多开新实例
result = wework.multi_open()
print(result)

# 注入到指定 PID
result = wework.inject(pid=12345)
print(result)

获取客户端 ID

python
# 按索引获取
client_id = wework.get_client_id("index", 0)

# 按账号获取
client_id = wework.get_client_id("account", "zhangsan")

# 按用户 ID 获取
client_id = wework.get_client_id("user_id", "user123")

# 按昵称获取
client_id = wework.get_client_id("nickname", "张三")

# 按 PID 获取
client_id = wework.get_client_id("pid", 12345)

客户端列表

python
with wework.clients_lock:
    for client in wework.clients:
        print(f"ID: {client['id']}, 昵称: {client['nickname']}")

每个客户端包含以下信息:

python
{
    "id": 1,                           # 客户端 ID
    "pid": 12345,                      # 进程 PID
    "user_id": "user123",              # 用户 ID
    "account": "zhangsan",             # 账号
    "nickname": "张三",                 # 昵称
    "username": "zhangsan@corp",       # 用户名
    "create_time": "2024-01-01 12:00:00"  # 接管时间
}

销毁注入

python
wework.destroy()

Released under the MIT License.