You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python中通过代理使用Tumblr API?

Using pytumblr with an Authenticated Proxy

Alright, I’ve dealt with this exact scenario before—getting pytumblr to work through an authenticated proxy is totally doable since the library relies on requests under the hood. Here’s how to make it happen step by step:

Background

The TumblrRestClient uses a requests.Session object to handle all API calls. This means we can directly modify the session’s proxy settings to route traffic through your authenticated proxy, no need to monkey-patch the entire library.

Step 1: Basic Setup with Proxy Authentication

There are two safe ways to configure proxy authentication with requests (and by extension, pytumblr):

Option 1: Embed Credentials in the Proxy URL (Simple but Watch for Special Characters)

If your proxy accepts credentials directly in the URL, you can format it like this. Just note that if your password has special characters (like @, :, or %), you’ll need to URL-encode it first to avoid parsing errors.

import pytumblr
from urllib.parse import quote_plus

# Initialize the Tumblr client as usual
client = pytumblr.TumblrRestClient(
    '<consumer_key>',
    '<consumer_secret>',
    '<oauth_token>',
    '<oauth_secret>',
)

# Proxy configuration
proxy_host = "your-proxy-address.com"
proxy_port = 8080  # Adjust to your proxy's port
proxy_username = "your-proxy-username"
proxy_password = "your-proxy-password"

# URL-encode the password to handle special characters
encoded_password = quote_plus(proxy_password)

# Build proxy URLs for HTTP and HTTPS
proxies = {
    "http": f"http://{proxy_username}:{encoded_password}@{proxy_host}:{proxy_port}",
    "https": f"http://{proxy_username}:{encoded_password}@{proxy_host}:{proxy_port}",
}

# Apply the proxy settings to the client's session
client.session.proxies.update(proxies)

# Test the setup
try:
    user_info = client.info()
    print("Proxy connection successful! User info:", user_info["user"]["name"])
except Exception as e:
    print(f"Failed to connect via proxy: {str(e)}")

Option 2: Use HTTPProxyAuth (More Secure, No Credentials in URL)

For better security (avoiding plaintext credentials in URLs), use requests’ built-in HTTPProxyAuth class to handle authentication separately:

import pytumblr
from requests.auth import HTTPProxyAuth

client = pytumblr.TumblrRestClient(
    '<consumer_key>',
    '<consumer_secret>',
    '<oauth_token>',
    '<oauth_secret>',
)

# Proxy configuration (no credentials in URL)
proxy_host = "your-proxy-address.com"
proxy_port = 8080
proxy_username = "your-proxy-username"
proxy_password = "your-proxy-password"

# Define proxy URLs
proxies = {
    "http": f"http://{proxy_host}:{proxy_port}",
    "https": f"http://{proxy_host}:{proxy_port}",
}

# Set up proxy authentication
client.session.auth = HTTPProxyAuth(proxy_username, proxy_password)
# Apply proxy URLs to the session
client.session.proxies.update(proxies)

# Test the request
try:
    dashboard = client.dashboard()
    print("Dashboard loaded successfully via proxy!")
except Exception as e:
    print(f"Proxy request failed: {str(e)}")

Key Notes

  • HTTPS Support: Make sure your proxy handles HTTPS traffic (most modern proxies do). The examples use http:// for the proxy URL even for HTTPS requests—this is correct because the proxy acts as a tunnel for encrypted HTTPS traffic.
  • SOCKS Proxies: If you’re using a SOCKS5 proxy, install requests[socks] first (pip install requests[socks]), then use socks5:// in your proxy URLs instead of http://.
  • Debugging: If you run into issues, enable requests debugging to see detailed traffic logs:
    import logging
    logging.basicConfig(level=logging.DEBUG)
    

内容的提问来源于stack exchange,提问作者Drunk Codes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 07:08:37