GitHub Actions工作流执行成功但未生成valid_proxies.txt文件求助
问题解决思路
一、仓库中看不到valid_proxies.txt的原因
你的工作流仅将文件作为**工作流产物(Artifact)**上传,并没有把生成的文件提交并推送到GitHub仓库。GitHub Actions的运行环境是临时容器,所有生成的文件默认不会自动同步到仓库,需要手动添加提交、推送步骤。
二、valid_proxies.txt为空的原因
你的脚本使用的是SOCKS5代理,但默认的requests库不支持SOCKS协议,导致所有代理校验都失败,无有效代理写入文件。另外脚本未设置超时,代理连接超时会直接被except捕获跳过,也会导致文件无内容。
具体修复方案
1. 修改脚本,支持SOCKS5代理并添加超时
首先在requirements.txt中添加依赖:
requests>=2.31.0 requests[socks]>=2.31.0
然后修改Python脚本:
import requests filename = "proxy_list.txt" valid_proxies_filename = "valid_proxies.txt" # 下载代理列表 response = requests.get("https://raw.githubusercontent.com/TheSpeedX/SOCKS-List/master/socks5.txt") with open(filename, "wb") as file: file.write(response.content) # 校验代理 with open(filename, "r") as file, open(valid_proxies_filename, "w") as valid_file: for proxy in file: proxy_addr = proxy.strip() if not proxy_addr: continue try: # 明确指定socks5协议,添加超时时间 res = requests.get( "https://httpbin.org/ip", proxies={ "http": f"socks5://{proxy_addr}", "https": f"socks5://{proxy_addr}" }, timeout=5 # 5秒超时,避免长时间等待 ) if res.status_code == 200: valid_file.write(f"{proxy_addr}\n") print(f"有效代理: {proxy_addr}") except Exception as e: print(f"无效代理 {proxy_addr}: {str(e)}") pass print("代理校验完成。")
2. 修改工作流,添加提交推送步骤
更新GitHub Actions配置,在上传产物后添加提交、推送步骤(使用默认token授权):
name: Run Proxy Checker on: schedule: - cron: '*/10 * * * *' # 每10分钟运行一次 jobs: build: runs-on: ubuntu-latest steps: - name: 拉取代码 uses: actions/checkout@v4 # 建议使用最新版本 - name: 配置Python环境 uses: actions/setup-python@v5 with: python-version: '3.9' - name: 安装依赖 run: | pip install --upgrade pip pip install -r requirements.txt - name: 运行代理校验脚本 run: python main.py - name: 上传有效代理作为工作流产物 uses: actions/upload-artifact@v4 with: name: valid-proxies path: valid_proxies.txt - name: 提交并推送有效代理文件 run: | git config --global user.name "GitHub Actions" git config --global user.email "actions@github.com" git add valid_proxies.txt # 仅当文件有变更时提交,避免空提交 git diff --quiet && git diff --staged --quiet || git commit -m "更新有效代理列表" git push
补充说明
- 工作流产物仅在工作流运行记录中可下载,不会自动同步到仓库,必须通过
git add/commit/push步骤才能将文件提交到仓库。 - SOCKS代理需要依赖
requests[socks],否则requests无法识别socks5://协议,导致所有代理校验失败。 - 添加超时时间可避免脚本因等待无响应代理而卡住,提升校验效率。
内容的提问来源于stack exchange,提问作者bSmall
相关产品推荐
相关产品推荐

