Closed shadowstar2023 closed 7 months ago
import csv import requests from concurrent.futures import ThreadPoolExecutor
output_csv = "wlip.csv" output_txt = "wlip.txt" target_country_code = "SG" api_url_base = "https://reallyfreegeoip.org/json/"
清空或创建输出文件
open(output_csv, "w").close() open(output_txt, "w").close()
def process_ip_address(ip_address): try: # 发送 GET 请求获取响应数据 response = requests.get(f"{api_url_base}{ip_address}") response.raise_for_status()
# 解析响应数据 data = response.json() # 将数据写入 CSV 文件 with open(output_csv, "a", newline="", encoding="utf-8") as csvfile: writer = csv.writer(csvfile) writer.writerow([data["ip"], data["country_code"], data["city"], data["time_zone"]]) print(f"IP 地址 {ip_address} 成功添加到 {output_csv}") except Exception as e: print(f"处理 IP 地址 {ip_address} 时出现错误:", str(e))
读取 all.txt 文件的每一行
with open("all.txt", "r") as ip_file: ip_addresses = list(set(ip_file.read().splitlines()))
使用线程池处理每个 IP 地址
with ThreadPoolExecutor() as executor: executor.map(process_ip_address, ip_addresses)
从 wlip.csv 中筛选出目标国家的 IP 地址
selected_ips = [] with open(output_csv, "r", newline="", encoding="utf-8") as csvfile: reader = csv.reader(csvfile) for row in reader: if row[1] == target_country_code: ip_address = row[0] if ip_address not in selected_ips: selected_ips.append(ip_address)
将符合条件的 IP 地址写入 wlip.txt 文件
with open(output_txt, "w", encoding="utf-8") as txtfile: txtfile.write("\n".join(selected_ips))
print(f"Process complete. Results are stored in {output_csv} and {output_txt}.")
22 all.txt是把txt.zip的合并来的,用API去测就快了啊 chatgpt写的
可以给一个一键脚本嘛?
chcp 936 > nul @echo off
cd "%~dp0" setlocal EnableDelayedExpansion cls RD /S /Q txt > nul 2>&1 set updatezip=0
if exist "txt.zip" ( echo 是否更新本地txt.zip数据? set /p updatezip="0不更新、1更新(默认%updatezip%):" )
if %updatezip% == 1 ( echo 正在从 https://zip.baipiao.eu.org 下载数据文件 echo 如果长时间无法下载,请手动访问url并下载保存为 txt.zip 到本目录 curl -# https://zip.baipiao.eu.org -o txt.zip )
if not exist "txt.zip" ( echo 正在从 https://zip.baipiao.eu.org 下载数据文件 echo 如果长时间无法下载,请手动访问url并下载保存为 txt.zip 到本目录 curl -# https://zip.baipiao.eu.org -o txt.zip )
7za x txt.zip -otxt -y cls
set country_file=
:main set menu=2 echo 1.自治域或地区模式 echo 2.自定义端口模式 echo 0.退出 set /p menu="请选择模式(默认%menu%):"
if %menu% == 1 goto menu1 if %menu% == 2 goto menu2 exit
:menu1 echo 当前可用自治域或者地区 for /f "tokens=4 delims= " %%a in ('dir txt ^| findstr .txt') do ( for /f "tokens=1 delims=-" %%b in ('echo %%a') do ( if not defined AS%%b ( set AS%%b=asn echo %%b ) ) )
for /f "tokens=1 delims==" %%i in ('set ^| findstr =asn') do ( set %%i= )
set asn=45102 set /p asn="请输入上面的自治域或者地区(默认%asn%):"
for /f "tokens=4 delims= " %%a in ('dir txt ^| findstr /I %asn%-') do ( for /f "tokens=2 delims=-" %%b in ('echo %%a') do ( if not defined TLS-%%b set TLS-%%b=asntls ) )
set tls=1 for /f "delims=" %%a in ('set ^| findstr =asntls ^| find /c /v ""') do ( if %%a == 2 ( echo 是否启用TLS? set /p tls="0禁用、1启用(默认%tls%):" if !tls! == 1 (set tlsmode=true) else (set tlsmode=false) ) else ( for /f "delims=" %%b in ('set ^| findstr =asntls ^| find /c /v "0"') do ( if %%b == 1 (set tlsmode=true) else (set tlsmode=false) ) ) )
for /f "tokens=1 delims==" %%i in ('set ^| findstr =asntls') do ( set %%i= )
echo 当前可用端口 for /f "tokens=4 delims= " %%a in ('dir txt ^| findstr /I %asn%-%tls%-') do ( for /f "tokens=3 delims=-" %%b in ('echo %%a') do ( for /f "tokens=1 delims=." %%c in ('echo %%b') do ( echo %%c set port=%%c ) ) )
set /p port="请输入检测端口(默认%port%):"
set outputfile=ip!tls!-!port!.txt
copy txt!asn!-!tls!-!port!.txt ".!output_file!"
goto wlip
:menu2 set tls=1 echo 是否启用TLS? set /p tls="0禁用、1启用(默认%tls%):" if !tls! == 1 (set tlsmode=true) else (set tlsmode=false) echo 当前可用端口 for /f "tokens=4 delims= " %%a in ('dir txt ^| findstr .txt ^| findstr -%tls%-') do ( for /f "tokens=3 delims=-" %%b in ('echo %%a') do ( for /f "tokens=1 delims=." %%c in ('echo %%b') do ( if not defined PORT%%c ( set PORT%%c=asn echo %%c set port=%%c ) ) ) )
set /p port="请输入检测端口(默认%port%):" set outputfile=ip!tls!-!port!.txt copy txt*-!tls!-!port!.txt ".!output_file!"
:: 利用指定的 API 获取 IP 地址的国家地址并输出到 %country_file%
:wlip
@echo off set outputfile=ip!tls!-!port!.txt set countryfile=wlip!output_file!
for /f "tokens=*" %%a in (!output_file!) do ( set /a count+=1 echo 正在获取 %%a 的国家信息... (%%count%% / %%total_lines%%) timeout /t 10 /nobreak >nul curl "https://reallyfreegeoip.org/json/%%a" >> !country_file! if errorlevel 1 ( echo 请求超时,跳过当前行. ) )
echo 获取 !output_file! 的国家信息完成.
:: 创建菜单,选择 country_code 内数值 :select_country_menu
rem 清空存储不重复 country_code 的数组 setlocal enabledelayedexpansion
rem 清空存储不重复 country_code 的数组 set "codes="
echo 所有 IP 地址及其对应的 country_code 值如下:
rem 逐行读取文件内容 for /f "tokens=2 delims=:," %%a in ('type "!country_file!" ^| find "country_code"') do ( rem 移除首尾空格 set "code=%%a" set "code=!code:~2,-1!"
rem 检查是否已经存在于数组中
if "!codes:,%code%,=!"=="!codes!" (
rem 如果不存在则添加到数组中,并输出
set "codes=!codes!,%code%"
echo country_code: !code!
)
)
endlocal set /p country_choice=选择:
if not defined country_choice goto :select_country_menu
:: 保存用户选择的国家码到变量 set selected_country=!country_choice!
:: 更新 filtered_file 以包含用户选择的 country_code set filteredfile=CCIP!countryfile!!selected_country!
(for /f "tokens=2 delims=:," %%a in ('type "!country_file!" ^| findstr /r /c:"\"country_code\":\"!selected_country!\""') do ( echo %%~a )) > "!filtered_file!"
echo. echo 过滤后的 IP 地址已保存到: !filtered_file!
pause >nul
测速部分自己手动啊
甬哥侃侃侃ygkkk @.***> 于2023年12月5日周二 08:00写道:
Closed #24 https://github.com/yonggekkk/openwrt_win64-ddns-cdnip/issues/24 as completed.
— Reply to this email directly, view it on GitHub https://github.com/yonggekkk/openwrt_win64-ddns-cdnip/issues/24#event-11146208341, or unsubscribe https://github.com/notifications/unsubscribe-auth/A7CV4PXXCPG6F7VIOBWO47LYHZP2VAVCNFSM6AAAAABAAMSTV2VHI2DSMVQWIX3LMV45UABCJFZXG5LFIV3GK3TUJZXXI2LGNFRWC5DJN5XDWMJRGE2DMMRQHAZTIMI . You are receiving this because you authored the thread.Message ID: <yonggekkk/openwrt_win64-ddns-cdnip/issue/24/issue_event/11146208341@ github.com>
import csv import requests from concurrent.futures import ThreadPoolExecutor
output_csv = "wlip.csv" output_txt = "wlip.txt" target_country_code = "SG" api_url_base = "https://reallyfreegeoip.org/json/"
清空或创建输出文件
open(output_csv, "w").close() open(output_txt, "w").close()
def process_ip_address(ip_address): try:
发送 GET 请求获取响应数据
读取 all.txt 文件的每一行
with open("all.txt", "r") as ip_file: ip_addresses = list(set(ip_file.read().splitlines()))
使用线程池处理每个 IP 地址
with ThreadPoolExecutor() as executor: executor.map(process_ip_address, ip_addresses)
从 wlip.csv 中筛选出目标国家的 IP 地址
selected_ips = [] with open(output_csv, "r", newline="", encoding="utf-8") as csvfile: reader = csv.reader(csvfile) for row in reader: if row[1] == target_country_code: ip_address = row[0] if ip_address not in selected_ips: selected_ips.append(ip_address)
将符合条件的 IP 地址写入 wlip.txt 文件
with open(output_txt, "w", encoding="utf-8") as txtfile: txtfile.write("\n".join(selected_ips))
print(f"Process complete. Results are stored in {output_csv} and {output_txt}.")
22 all.txt是把txt.zip的合并来的,用API去测就快了啊 chatgpt写的