Introduction
Requests — An Overview
The requests library is a powerful and user-friendly HTTP library for Python. It simplifies sending HTTP requests, handling responses, and managing sessions, making it ideal for web scraping and API interactions. With requests, you can easily send GET, POST, PUT, DELETE, and other types of HTTP requests, handle cookies, and manage authentication. Its simplicity and flexibility make it a popular choice for developers working on web-related tasks. For more information, visit requests documentation.
Prerequisites
Install Python from the official website.
Check if Python is installed correctly, you can use the following command in your terminal or command prompt
python --versionThe output should be in the following format:
Python 3.11.2If you receive an error or see a version number starting with 2.x, you need to download Python 3.x and follow the installation instructions to set it up
Next, create a new Python project and install the requests package
pip install requestsPlanning to connect through the SOCKS5 port too? requests needs its optional SOCKS extra for that, which installs PySocks:
pip install "requests[socks]"After installing the requests library, let's make a request to get our IP address. This can be done by sending a GET request to a service that returns your public IP. Here’s how you can do it:
import requests
response = requests.get('https://httpbin.org/ip', timeout=30)
print(response.json())After running the script, it sends a GET request to https://httpbin.org/ip and retrieves your public IP address from the response. The IP address is then printed out in JSON format.
Now, we've used requests without integrating ProxyPanel proxies. Follow the next steps to configure your proxy with requests.
Configuring the Requests Package with ProxyPanel
Sign in to your dashboard and open My Proxies. If the proxy you want to use is marked Pending, press Start proxy first; a new proxy only begins working once it's started. Then collect these details from its row:
- Connection: the "HTTP proxy" entry shows the host and the HTTP port. The "SOCKS5 proxy" entry has the same host with its own SOCKS5 port.
- Credentials: Copy username copies the proxy username. The password is hidden; the eye button (Show / hide) displays it and Copy password copies it. You won't be asked for your account password.
Keep those values out of your code by setting them as environment variables in the terminal you'll run the script from:
# macOS / Linux
export PROXY_HOST="HOST"
export PROXY_HTTP_PORT="HTTP_PORT"
export PROXY_SOCKS5_PORT="SOCKS5_PORT" # only needed for SOCKS5
export PROXY_USERNAME="USERNAME"
export PROXY_PASSWORD="PASSWORD"
# Windows (PowerShell)
$env:PROXY_HOST = "HOST"
$env:PROXY_HTTP_PORT = "HTTP_PORT"
$env:PROXY_SOCKS5_PORT = "SOCKS5_PORT"
$env:PROXY_USERNAME = "USERNAME"
$env:PROXY_PASSWORD = "PASSWORD"To configure your ProxyPanel proxy with the requests library, use the following code:
import os
from urllib.parse import quote
import requests
# Proxy configuration, read from the environment variables set above
proxy_host = os.environ['PROXY_HOST']
proxy_port = os.environ['PROXY_HTTP_PORT']
proxy_login = quote(os.environ['PROXY_USERNAME'], safe='')
proxy_password = quote(os.environ['PROXY_PASSWORD'], safe='')
proxy = f'http://{proxy_login}:{proxy_password}@{proxy_host}:{proxy_port}'
proxies = {
'http': proxy,
'https': proxy
}
# Send a GET request using the proxy
response = requests.get('https://httpbin.org/ip', proxies=proxies, timeout=30)
print(response.json())There's nothing to replace in the script: it takes the host, the HTTP port and your login from the environment. quote() percent-encodes the username and password, so a password containing characters such as # or % can't break the proxy URL.
You will notice that the IP address with proxy configuration differs from the one without a proxy. Now you have successfully integrated the requests package with ProxyPanel.
To use the SOCKS5 port instead, install the SOCKS extra shown in the prerequisites and change the line that builds proxy to the one below. Write the scheme as socks5h: with the h, the proxy resolves website names itself, so your DNS lookups travel through it too.
proxy = f"socks5h://{proxy_login}:{proxy_password}@{proxy_host}:{os.environ['PROXY_SOCKS5_PORT']}"