How to Import External Proxy Lists with Python
In this article, we'll show you how to convert your external proxy lists into API-ready JSON files. This approach allows you to save all your credentials in a convenient JSON format, making it easier to integrate with API endpoints.
Before You Start
Ensure you have a Python environment set up with the following packages installed:
jsonre
You will also need to save the Python script provided below.
The json_proxy_list.py Script
Save the following Python script as json_proxy_list.py in your desired folder. This script will prompt you for your proxy list source (either a file path or a direct paste) and the proxy type, then generate a proxies.json file.
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 None # Return None to indicate failure
# 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() # Recursive call until valid input
return proxy_type
# Detect line separator from block of credentials (Note: This function is defined but not used in the current main() logic)
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()
proxy_list_content = None
# 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:
print("Failed to read proxies from file. Exiting.")
return
elif choice == '1':
proxy_list_content = paste_list
else:
print("Invalid choice for list source. Exiting.")
return
# Check if HTTP/SOCKS5
proxy_type = get_proxy_type()
# Split the proxy list based on detected separator
# Using re.split to handle multiple separators (newline, comma, space, slash) robustly
proxy_lines_raw = re.split(r'[\n, /]+', proxy_list_content.strip())
# Add the proxy type to each line and filter out empty lines
proxy_lines = [f"{proxy_type}:{line.strip()}" for line in proxy_lines_raw 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.
# Each line should now be in format: type:host:port:username:password
for line in proxy_lines:
parts = line.split(':')
if len(parts) != 5: # type, host, port, username, password
print(f"Skipping invalid line: '{line}'. Expected 5 parts (type:host:port:username:password) but got {len(parts)}. Please check your proxy credentials format.")
continue
proxy = {
"type": parts[0],
"host": parts[1],
"port": parts[2],
"username": parts[3],
"password": parts[4]
}
proxies_json["proxies"]["proxy"].append(proxy)
if not proxies_json["proxies"]["proxy"]:
print("No valid proxy entries were processed. Output file will be empty or not reflect your full list.")
# Create the JSON.dumps, save it on proxies.json file.
try:
with open('proxies.json', 'w') as json_file:
json.dump(proxies_json, json_file, indent=2)
print("File proxies.json was written successfully.")
except IOError as e:
print(f"Error writing to proxies.json: {e}")
if __name__ == "__main__":
main()
Running the Script
-
Navigate to the Script Directory: Open your terminal or command prompt and navigate to the folder where you saved
json_proxy_list.py. -
Run the Script: Execute the script using the following command:
python json_proxy_list.py -
Choose Proxy List Source: The script will prompt you to select the source of your proxy list:
- Option (1) from TEXT: If you select this, the script will use the proxy data pasted directly into the
paste_listvariable within the script (around line 8).tipModify the
paste_listvariable in the script before running if you choose this option. Each proxy should be in the formathost:port:username:password. Proxies can be separated by newlines, commas, spaces, or slashes. Example forpaste_list:paste_list = """
host1:port1:user1:pass1
host2:port2:user2:pass2, host3:port3:user3:pass3
""" - Option (2) from PATH: If you select this, the script will attempt to read proxies from the file specified in the
file_pathvariable (around line 5).infoEnsure the
file_pathvariable in the script points to your actual.txtfile containing the proxy list. The format within the file should be one proxy per line (or separated by supported delimiters), likehost:port:username:password. Example forfile_path:file_path = "C:/Users/YourUser/Documents/my_proxies.txt"warningIf the script encounters an error reading the file (e.g., incorrect path), it will print an error message and exit. Double-check the
file_path.
- Option (1) from TEXT: If you select this, the script will use the proxy data pasted directly into the
-
Select Proxy Type: Next, you'll be prompted to enter the proxy type:
(1) HTTP(2) HTTPS(3) SOCKS5This selection is crucial as most external proxy lists don't specify the type. The script will prepend this chosen type to each proxy entry in the output JSON.
-
Check the Results: After the script finishes, it will create a file named
proxies.jsonin the same directory. This file contains your proxy list formatted in JSON.Example
proxies.jsonoutput:{
"proxies": {
"proxy": [
{
"type": "http",
"host": "host1",
"port": "port1",
"username": "user1",
"password": "pass1"
},
{
"type": "http",
"host": "host2",
"port": "port2",
"username": "user2",
"password": "pass2"
}
]
}
}
This structured JSON file is now ready for easier integration with API endpoints or other tools that consume proxy lists in this format.