You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python为每台ADB设备分配txt文件中唯一代理的问题求助

问题原因
  • 你仅在设备循环外执行了一次readline()读取代理,整个循环过程中代理变量没有更新,所有设备自然会复用同一个代理值
  • 没有提前把所有代理读取为列表和设备做一一匹配,自然无法实现每台设备分配独立代理的需求
修正代码
from ppadb.client import Client as AdbClient

client = AdbClient(host="127.0.0.1", port=5037)
devices = client.devices()

# 先读取所有代理到列表,自动去掉每行的换行符和空行
with open('F:/Appium/venv/proxy.txt', encoding='utf-8') as file:
    proxies = [line.strip() for line in file if line.strip()]

# 校验代理数量和设备数量是否匹配
if len(proxies) < len(devices):
    print(f"错误:代理数量({len(proxies)})少于设备数量({len(devices)}),无法完成分配")
else:
    # 遍历设备同时取索引,匹配对应位置的代理
    for index, device in enumerate(devices):
        current_proxy = proxies[index]
        device.shell(f"settings put global http_proxy {current_proxy}")
        print(device)
        print(current_proxy)
代码说明
  • 先一次性把所有代理行读取为列表,自动过滤空行、去掉每行末尾的换行符,避免设置代理时带多余空白字符出错
  • 增加了代理和设备的数量校验,避免代理不足时出现下标越界报错
  • 用enumerate遍历设备同时获取索引,直接取对应位置的代理实现一一分配
  • 用f-string优化了字符串拼接,代码可读性更高

内容的提问来源于stack exchange,提问作者user3934618

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 00:09:04