Chuyển đổi danh sách proxy bên ngoài thành các tệp JSON có hỗ trợ API
Sử dụng script Python này để chuyển danh sách proxy bên ngoài thành JSON có cấu trúc mà bạn có thể sử dụng lại trong các quy trình API Multilogin.
Thêm proxy theo định dạng host:port:username:password → chọn HTTP, HTTPS hoặc SOCKS5 → chạy script → kiểm tra tệp proxies.json được tạo.
Script có thể đọc thông tin đăng nhập proxy được dán trực tiếp vào tệp hoặc tải từ tệp .txt.
Trước khi bắt đầu
- Đảm bảo Python đã được cài đặt trên máy tính. Script sử dụng các mô-đun
jsonvàretích hợp sẵn của Python, vì vậy bạn không cần cài đặt thêm gói nào. - Lưu script
json_proxy_list.pyvào thư mục bạn muốn.
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()Chạy script
- Mở terminal của bạn và điều hướng đến thư mục chứa tập lệnh
- Chạy script
json_proxy_list.py
- Chọn nguồn danh sách chi tiết proxy mà bạn cần:
- Ở dòng 5, dán đường dẫn tệp.
txthoặc - Ở dòng 8, dán chuỗi chi tiết proxy
- Ở dòng 5, dán đường dẫn tệp.
- Chọn
HTTP,HTTPShoặcSOCKS5. Script sẽ áp dụng loại đã chọn cho tất cả proxy trong danh sách đầu vào. - Kiểm tra kết quả trong tệp
proxies.jsonđược lưu trữ trong cùng thư mục