跳转到内容
Multilogin帮助中心

如何用 Python 导入外部全局IP列表

使用此 Python 脚本,将外部全局IP列表转换为结构化 JSON,供 Multilogin API 工作流重复使用。

按 host:port:username:password 格式添加全局IP→选择 HTTP、HTTPS 或 SOCKS5→运行脚本→检查生成的 proxies.json 文件。

脚本可以读取直接粘贴到文件中的全局IP凭据,也可以从 .txt 文件加载。

开始之前

  1. 确认电脑上已安装 Python。脚本使用 Python 内置的 json 和 re 模块,无需安装额外软件包
  2. 将脚本 json_proxy_list.py 保存到所需文件夹 

json_proxy_list

import json
import re

# Input the proxy list path here, if any.
file_path = "C:/Users/.../input_list.txt"

# Paste the proxy list here. Supported separators: comma, bar, space, newline
paste_list = """ 
host:port:username:password
"""

# Reading the proxies from the file path, if any
def read_proxies_from_file(file_path):
    try:
        with open(file_path, 'r') as file:
            return file.read()
    except Exception as e:
        print("Error reading file - please check your file PATH.")
        print(f'Exception found: {e}')
        return

# User input needed: proxy type (HTTP/SOCKS5)
def get_proxy_type():
    print("Enter the proxy type:")
    print("(1) HTTP")
    print("(2) HTTPS")
    print("(3) SOCKS5")
    choose_type = input()

    if choose_type == "1":
        proxy_type = "http"
    elif choose_type == "2":
        proxy_type = "https"
    elif choose_type == "3":
        proxy_type = "socks5"
    else:
        print("Invalid proxy type. Enter a valid option number.")
        return get_proxy_type()
    
    return proxy_type

# Detect line separator from block of credentials
def get_line_separator(proxy_list):
    # Detect the most common line separators
    separators = ['\n', ',', '/', ' ']
    separator_counts = {sep: proxy_list.count(sep) for sep in separators}
    sorted_separators = sorted(separator_counts, key=separator_counts.get, reverse=True)
    most_likely_separator = sorted_separators[0]

    # Handle double values such as '\n,' by checking combinations of common separators
    combined_separators = ['\n,', ',\n', '\n/', '/\n', '\n ', ' \n', ', ', ' ,', '/ ', ' /']
    for combo in combined_separators:
        if combo in proxy_list:
            return combo

    return most_likely_separator

# Main Function - Inputs user for preferred proxy list source
def main():
    # Select proxy list source
    print("Select the list source:")
    print("(1) from TEXT")
    print("(2) from PATH")
    choice = input()
    
    # Take action based on the script source
    if choice == '2':
        proxy_list_content = read_proxies_from_file(file_path)
        if proxy_list_content is None:
            return
    else:
        proxy_list_content = paste_list

    # Check if HTTP/SOCKS5
    proxy_type = get_proxy_type()

    # Split the proxy list based on detected separator
    proxy_lines = re.split(r'[\n, /]+', proxy_list_content.strip())

    # Add the proxy type to each line
    proxy_lines = [f"{proxy_type}:{line.strip()}" for line in proxy_lines if line.strip()]

    # Create JSON object that is similar to API output for easy future integration
    proxies_json = {
        "proxies": {
            "proxy": []
            }
        }

    # For each proxy line contained in the proxy list, take each proxy element to assign it.
    for line in proxy_lines:
        parts = line.split(':')
        if len(parts) != 5:
            print(f"Skipping invalid line: {line}. Please check your proxy credentials file.")
            continue
        proxy = {
            "type": parts[0],
            "host": parts[1],
            "port": parts[2],
            "username": parts[3],
            "password": parts[4]
        }
        proxies_json["proxies"]["proxy"].append(proxy)

    # Create the JSON.dumps, save it on proxies.json file.
    with open('proxies.json', 'w') as json_file:
        json.dump(proxies_json, json_file, indent=2)

    print("File proxies.json was written succesfully.")

if __name__ == "__main__":
    main()

运行脚本

  1. 打开终端,进入脚本所在文件夹
  2. 运行脚本 json_proxy_list.py
  1. 选择所需的全局IP信息来源:
    1. 在第 5 行粘贴 .txt 文件路径,或
    2. 在第 8 行粘贴全局IP信息字符串
  2. 选择 HTTP、HTTPS 或 SOCKS5。脚本会将所选类型应用到输入列表中的每个全局IP。
  3. 在同一文件夹中的 proxies.json 文件中检查结果

随时掌握最新动态

获取最新动态,交流经验,了解实际使用场景。

获取产品更新Telegram频道向其他用户提问Telegram聊天查看使用场景Discord社区