Of course! The term "Python request agent" can mean a few different things. I'll cover the most common interpretations, from the fundamental concept to more advanced and practical implementations.

The Fundamental Concept: User-Agent in an HTTP Request
At its core, a "request agent" usually refers to the User-Agent string. This is a piece of text that a web browser or other HTTP client sends to a server to identify itself.
- What it is: A string like
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36. - What it does: It tells the server what kind of client is making the request (e.g., Chrome on Windows, a Python script, a mobile app). Servers use this to:
- Serve different content (e.g., a mobile-optimized website).
- Block requests from bots they don't want.
- Provide compatibility for older browsers.
When you use the popular requests library in Python, it sets a default User-Agent.
Example: Seeing the Default User-Agent
import requests
# Make a simple request
response = requests.get('https://httpbin.org/user-agent')
# The response shows the User-Agent that the server received
print(response.json())
Output:
{
"user-agent": "python-requests/2.31.0 (https://requests.readthedocs.io/en/latest/)"
}
As you can see, it identifies itself as python-requests. This is a dead giveaway that you're a script, and some websites might block this.

How to Set and Change Your User-Agent (The Practical "Agent")
To avoid being blocked or to access mobile/desktop versions of a site, you need to customize your User-Agent string. You do this by passing a headers dictionary to your requests call.
Example: Faking a Chrome Browser
This is the most common use case. You can find User-Agent strings for any browser by searching for "What is my User-Agent?".
import requests
# Define the User-Agent string for a recent version of Chrome on Windows
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
}
# Make the request with the custom headers
response = requests.get('https://httpbin.org/user-agent', headers=headers)
# The server now sees us as a Chrome browser
print(response.json())
Output:
{
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
}
Now the server thinks we are a regular Chrome user!

Advanced "Agents": Using Proxies and Rotating User-Agents
Sometimes, just changing the User-Agent isn't enough. Websites can also block you based on your IP address. This is where a more advanced "agent" setup comes in, involving proxies and rotating your identity.
a) Using a Proxy Server
A proxy server acts as an intermediary. You send your request to the proxy, which then forwards it to the target website. The website sees the proxy's IP address, not yours.
import requests
# Replace with a real, working proxy
# Format: 'http://ip_address:port' or 'socks5://ip_address:port'
proxies = {
'http': 'http://your_proxy_ip:port',
'https': 'http://your_proxy_ip:port',
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
}
try:
response = requests.get('https://httpbin.org/ip', headers=headers, proxies=proxies, timeout=10)
print("Request successful!")
print(f"IP Address seen by the server: {response.json()}")
except requests.exceptions.ProxyError:
print("Error: Could not connect to the proxy.")
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
⚠️ Important: Free public proxies are often slow, unreliable, and can be security risks. For serious scraping, it's better to use a reputable proxy service.
b) Rotating User-Agents and Proxies
The most robust "agent" setup involves rotating both your User-Agent and your proxy with each request to avoid detection.
import requests
import random
# A list of User-Agent strings to rotate from
USER_AGENTS = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36',
'Mozilla/5.0 (iPhone; CPU iPhone OS 15_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.6 Mobile/15E148 Safari/604.1'
]
# A list of proxies to rotate from
# In a real scenario, you would get these from a proxy provider
PROXIES = [
{'http': 'http://proxy1_ip:port', 'https': 'http://proxy1_ip:port'},
{'http': 'http://proxy2_ip:port', 'https': 'http://proxy2_ip:port'},
# ... more proxies
]
def make_request(url):
"""Makes a request with a random User-Agent and proxy."""
# Select a random User-Agent and proxy
headers = {'User-Agent': random.choice(USER_AGENTS)}
proxy = random.choice(PROXIES) if PROXIES else None
print(f"Using User-Agent: {headers['User-Agent']}")
if proxy:
print(f"Using Proxy: {proxy['http']}")
try:
response = requests.get(url, headers=headers, proxies=proxy, timeout=10)
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
return response.text
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
return None
# --- Example Usage ---
if __name__ == "__main__":
target_url = 'https://httpbin.org/user-agent'
content = make_request(target_url)
if content:
print("\n--- Response from Server ---")
# The response will show one of the User-Agents from our list
print(content)
Summary and Best Practices
| Technique | Purpose | How to do it with requests |
|---|---|---|
Custom User-Agent |
Impersonate a browser to avoid simple bot detection. | requests.get(url, headers={'User-Agent': '...'}) |
| Using a Proxy | Hide your real IP address to avoid IP-based blocking. | requests.get(url, proxies={'http': '...'}) |
| Rotating Identities | Avoid detection by changing your User-Agent and IP frequently. |
Combine random.choice() with lists of User-Agents and proxies. |
Best Practices:
- Check
robots.txt: Always checkhttps://example.com/robots.txtto see which parts of the site the owner asks bots not to visit. - Be Respectful: Add delays between your requests using
time.sleep()to avoid overwhelming the server. - Handle Errors: Use
try...exceptblocks to handle network errors, timeouts, and HTTP errors gracefully. - Use Libraries for Scraping: For more complex tasks, consider libraries like Scrapy, which have built-in support for rotating User-Agents, proxies, and handling cookies.
