杰瑞科技汇

Linux下Python如何配置代理?

Of course! Setting up and using a proxy in Linux with Python is a common task. The approach varies depending on whether you want to configure the system-wide proxy for all applications or just set it for a specific Python script.

Linux下Python如何配置代理?-图1
(图片来源网络,侵删)

Here’s a comprehensive guide covering all the main scenarios.


Summary of Methods

Method Scope Use Case How to Set
Environment Variables System-wide or per-session Best for general system use, tools like curl, wget, and many Python libraries that respect environment variables. export http_proxy="http://user:pass@host:port"
Python requests Library Per-script The most common and recommended method for Python scripts using the popular requests library. proxies={'http': 'http://host:port', 'https': 'http://host:port'}
Python urllib Library Per-script The built-in standard library alternative to requests. Useful if you can't install external libraries. ProxyHandler({'http': 'http://host:port'})
System-Level Proxy System-wide Sets the proxy for all graphical (GUI) and command-line applications on the system. GUI settings (GNOME, KDE) or /etc/environment file.

Method 1: Environment Variables (The Simple & Universal Way)

This is the easiest way to set a proxy for most applications, including many Python libraries. You set the variables in your shell, and any compliant application will pick them up.

For the Current Terminal Session

Open your terminal and run the following commands. Replace the values with your proxy details.

# For HTTP and HTTPS traffic
export http_proxy="http://user:password@proxy.server.com:8080"
export https_proxy="http://user:password@proxy.server.com:8080"
# For FTP traffic (if needed)
export ftp_proxy="http://user:password@proxy.server.com:8080"
# Optional: No proxy for these hosts/domains
export no_proxy="localhost,127.0.0.1,.local,.example.com"
# To make the settings permanent for the current session, add them to your shell's profile file
# For Bash: ~/.bashrc or ~/.bash_profile
# For Zsh: ~/.zshrc
echo 'export http_proxy="http://user:password@proxy.server.com:8080"' >> ~/.bashrc
source ~/.bashrc

Note: If your proxy doesn't require authentication, use the simpler format: http_proxy="http://proxy.server.com:8080".

Linux下Python如何配置代理?-图2
(图片来源网络,侵删)

System-wide for All Users (Persistent)

To make the proxy settings permanent for all users on the system, you can edit the /etc/environment file. You will need sudo to edit this file.

sudo nano /etc/environment

Add the following lines at the end of the file:

http_proxy="http://user:password@proxy.server.com:8080"
https_proxy="http://http://user:password@proxy.server.com:8080"
ftp_proxy="http://user:password@proxy.server.com:8080"
no_proxy="localhost,127.0.0.1"

Save the file and exit. The changes will take effect after you reboot or log out and back in.


Method 2: Using the requests Library in Python (Recommended for Scripts)

The requests library is the de facto standard for making HTTP requests in Python. It has excellent, built-in support for proxies.

Linux下Python如何配置代理?-图3
(图片来源网络,侵删)

First, ensure you have requests installed:

pip install requests

Here’s how to use it in your script:

import requests
# Define your proxy settings
proxies = {
  'http': 'http://user:password@proxy.server.com:8080',
  'https': 'http://user:password@proxy.server.com:8080',
}
# The URL you want to access
url = 'https://httpbin.org/ip' # A great site for testing proxies
try:
    # Use the 'proxies' argument in the get() method
    response = requests.get(url, proxies=proxies, timeout=10)
    # Check if the request was successful
    response.raise_for_status() 
    print("Successfully connected through the proxy!")
    print("Response content:", response.json())
except requests.exceptions.RequestException as e:
    print(f"Error connecting to the proxy: {e}")

For SOCKS Proxies: If you need to use a SOCKS proxy (e.g., with requests[socks]), the syntax is similar.

pip install requests[socks]
import requests
proxies = {
    'http': 'socks5://user:password@socks.server.com:1080',
    'https': 'socks5://user:password@socks.server.com:1080',
}
response = requests.get('https://httpbin.org/ip', proxies=proxies)
print(response.json())

Method 3: Using Python's Built-in urllib Library

If you can't use external libraries like requests, urllib is the way to go. It's part of the Python standard library.

The process is slightly more manual. You need to create a proxy handler and install it into an opener.

import urllib.request
import urllib.error
# Define your proxy settings
proxy_address = "http://user:password@proxy.server.com:8080"
# Create a proxy handler
proxy_handler = urllib.request.ProxyHandler({
    'http': proxy_address,
    'https': proxy_address
})
# Create an opener that uses the proxy handler
opener = urllib.request.build_opener(proxy_handler)
# Install the opener globally so that all urllib.request calls use it
urllib.request.install_opener(opener)
# Now you can use urllib.request as usual, and it will go through the proxy
url = 'https://httpbin.org/ip'
try:
    with urllib.request.urlopen(url, timeout=10) as response:
        content = response.read()
        print("Successfully connected through the proxy!")
        print("Response content:", content.decode('utf-8'))
except urllib.error.URLError as e:
    print(f"Error connecting to the proxy: {e}")

Method 4: System-Level Proxy Configuration (GUI & System Services)

This method configures the proxy for the entire Linux desktop environment and for services that read system settings.

For GNOME / Ubuntu / Debian / Fedora (using gsettings)

This sets the proxy for GNOME-based desktops and command-line tools that respect these settings.

# Set the proxy server
gsettings set org.gnome.system.proxy.http "http://proxy.server.com:8080"
gsettings set org.gnome.system.proxy.https "http://proxy.server.com:8080"
gsettings set org.gnome.system.proxy.ftp "http://proxy.server.com:8080"
gsettings set org.gnome.system.proxy.socks "http://proxy.server.com:1080"
# Set authentication if needed
gsettings set org.gnome.system.proxy.http-authentication "user:password"
# Set the ignore list (same as no_proxy)
gsettings set org.gnome.system.proxy ignore-hosts "['localhost', '127.0.0.1', '.local']"
# To disable the proxy
gsettings set org.gnome.system.proxy mode 'none'

For KDE / Plasma (using kwriteconfig5)

# Set the proxy server
kwriteconfig5 --file "$HOME/.config/kioslaverc" "httpProxy" "proxy.server.com:8080"
kwriteconfig5 --file "$HOME/.config/kioslaverc" "httpsProxy" "proxy.server.com:8080"
# Set authentication
kwriteconfig5 --file "$HOME/.config/kioslaverc" "httpProxyUser" "user"
kwriteconfig5 --file "$HOME/.config/kioslaverc" "httpProxyPassword" "password"
# Apply the settings (this command refreshes KDE's configuration)
kbuildsycoca5

Troubleshooting

  • Connection Refused/Timeout: Double-check the proxy host and port. Ensure the proxy server is running and accessible from your machine.
  • Authentication Failed: Verify your username and password. Some proxies use different authentication schemes.
  • SSL/TLS Errors: If you see certificate errors, the proxy might be intercepting HTTPS traffic (a common practice in corporate environments). You might need to disable SSL verification in your script (not recommended for production) or download the proxy's CA certificate and add it to your system's trust store.
  • Script Works, but Other Apps Don't: You likely set the proxy in the script (Method 2/3) but not for the system (Method 1/4). Use the appropriate method for your goal.
  • requests.exceptions.ProxyError: This is a clear sign that your proxy settings in the script are incorrect or the proxy server is not reachable. Check the proxies dictionary in your Python code.
分享:
扫描分享到社交APP
上一篇
下一篇