In this tutorial, we will build a proxy checker in Python to evaluate the speed and anonymity of proxies. This tool can help you assess the effectiveness of different proxy servers, including their latency and how well they hide your identity. We will use several libraries, including requests for HTTP requests and time for speed calculations.
Setting Up the Environment
Before you begin, ensure you have Python installed on your machine. You will also need to install the following libraries:
requests
– to send HTTP requests and check the proxy’s functionality.time
– to measure the response time for speed testing.json
– for handling and parsing JSON data.
To install these libraries, run the following command:
pip install requests
Proxy Checker Code
Now, let’s dive into the code. Below is a Python script that checks the proxy’s speed and its ability to maintain anonymity.
import requests
import time
import json
# Function to test speed of a proxy
def test_speed(proxy):
url = "http://httpbin.org/delay/3" # URL for testing response delay
proxies = {"http": proxy, "https": proxy}
start_time = time.time()
try:
response = requests.get(url, proxies=proxies, timeout=10)
response_time = time.time() - start_time
if response.status_code == 200:
return response_time
else:
return None
except requests.RequestException:
return None
# Function to test anonymity of a proxy
def test_anonymity(proxy):
url = "http://httpbin.org/ip" # URL to fetch the requester's IP
proxies = {"http": proxy, "https": proxy}
try:
response = requests.get(url, proxies=proxies, timeout=10)
response_data = json.loads(response.text)
proxy_ip = response_data["origin"]
return proxy_ip
except requests.RequestException:
return None
# Main function to check proxies
def check_proxy(proxy):
print(f"Testing proxy: {proxy}")
# Test speed
speed = test_speed(proxy)
if speed:
print(f"Speed Test: {speed:.2f} seconds")
else:
print("Speed Test: Failed")
# Test anonymity
ip = test_anonymity(proxy)
if ip:
print(f"Anonymity Test: Proxy IP is {ip}")
else:
print("Anonymity Test: Failed")
# Example proxies to test
proxies = [
"http://your_proxy_1",
"http://your_proxy_2",
]
for proxy in proxies:
check_proxy(proxy)
Explanation of the Code
The script is structured into three main functions:
- test_speed(proxy): This function checks the speed of a proxy by sending a request to
httpbin.org
with a delay. It measures the time taken to receive a response and returns it. - test_anonymity(proxy): This function tests the anonymity of a proxy by checking if the IP returned by the server matches the proxy IP or the original IP.
- check_proxy(proxy): The main function that calls the previous functions and prints the results.
Testing Multiple Proxies
To test multiple proxies, simply replace the proxy URLs in the proxies
list. The script will loop through each proxy and run the tests, providing results for each one. Make sure that the proxy URLs are formatted correctly, for example: http://username:password@proxyserver:port
for authenticated proxies.
Improving the Script
This basic script can be expanded in various ways, such as adding proxy rotation, handling different proxy types (SOCKS5, HTTP, HTTPS), or integrating with databases to store proxy results. Below are some ideas for improvements:
- Proxy Rotation: Use a proxy pool to rotate proxies for more comprehensive testing.
- Proxy Authentication: Handle proxies that require authentication by passing the credentials as part of the URL.
- Threading: Improve the performance of the script by checking multiple proxies in parallel using Python’s threading or multiprocessing modules.
Conclusion
The proxy checker script is a simple yet effective tool for testing the speed and anonymity of proxies. By understanding these factors, you can select the best proxies for your needs. Customize the script further to match your specific requirements, and explore additional features such as proxy rotation or advanced error handling.
We earn commissions using affiliate links.