In some cases, you may need to set a global proxy IP in your Python script to ensure that all network requests go through the specified proxy server. Next, I will detail how to set a global proxy IP in Python.
Method 1: Use environment variables
One of the easiest ways to configure the global agent is by setting an environment variable. Here are the exact steps:
Step 1: Setting Environment Variables
Before running a Python script, you can set environment variables from the command line:
export http_proxy=http://proxy_ip:proxy_port
export https_proxy=https://proxy_ip:proxy_port
Example:
export http_proxy=http://123.45.67.89:8080
export https_proxy=https://123.45.67.89:8080
On Windows systems, you can use the following commands to set environment variables:
set http_proxy=http://123.45.67.89:8080
set https_proxy=https://123.45.67.89:8080
Step 2: Run the Python Script
After setting the environment variables, run your Python script:
python your_script.py
At this point, all web requests made through the requests library will automatically use the specified proxy server.
Method 2: Set the global proxy in the code
Another way is to set the global proxy directly in the code. Here are the exact steps:
Step 1: Import the necessary modules
First, import the os and requests modules:
import os
import requests
Step 2: Setting Environment Variables
Setting environment variables in code:
os.environ['http_proxy'] = 'http://123.45.67.89:8080'
os.environ['https_proxy'] = 'https://123.45.67.89:8080'
Step 3: Send a network request
After setting the environment variable, all web requests made through the requests library will automatically use the specified proxy server:
response = requests.get('http://example.com')
print(response.text)
Method 3: Use the session object of the requests library
You can also use the session object of the requests library to set up a global proxy. Here are the exact steps:
Step 1: Create a session object
First, create a requests session object:
import requests
session = requests.Session()
Step 2: Setting up the proxy
Set the proxy in the session object:
proxies = {
'http': 'http://123.45.67.89:8080',
'https': 'https://123.45.67.89:8080',
}
session.proxies.update(proxies)
Step 3: Send a network request
Use the session object to send network requests:
response = session.get('http://example.com')
print(response.text)
Method 4: Use the urllib library
If you are using the urllib library, you can set up the global proxy in the following way:
Step 1: Import the necessary modules
First, import the urllib module:
import urllib.request
Step 2: Setting up the proxy
Create an agent processor and install it globally:
proxy_handler = urllib.request.ProxyHandler({
'http': 'http://123.45.67.89:8080',
'https': 'https://123.45.67.89:8080',
})
opener = urllib.request.build_opener(proxy_handler)
urllib.request.install_opener(opener)
Step 3: Send a network request
Use urllib to send network requests:
response = urllib.request.urlopen('http://example.com')
print(response.read().decode('utf-8'))
caveat
There are a few things to keep in mind when using a proxy IP:
- Proxy IP quality:Make sure that the proxy IP you are using is reliable, otherwise it may result in failed or slow access.
- Privacy and Security:There is a risk of data theft or tampering when using public proxy IPs. For sensitive operations, it is recommended to use a reputable paid proxy service.
- The geographic location of the proxy server:Choose a proxy server in the right geographic location to ensure access speed and stability.
summarize
With the above method, you can set a global proxy IP in Python to ensure that all network requests go through the specified proxy server. Whether it's for privacy or to improve access speed, proxy IPs are a technical tool worth trying.