
Hands-on with Python to play with API authentication
The old iron engaged in crawling should have encountered this situation: the request interface with the requests library, often blocked by the server 403 outside. This timeProxy IP + Proper Authentication Configurationis your life saver. Today let's take ipipgo's API service as an example and break down how to handle these authentication processes correctly with Python.
What's the best way to choose a certification method?
There are two genres of common API certifications:
| Type of Certification | Scenario | security level |
|---|---|---|
| API Key | Quick Access | ★★★★★ |
| JWT Token | Long-term services | ★★★★★ |
The ipipgo interface is recommended fortwo-factor authenticationThe key is passed in the header and the traffic is distributed through the proxy IP. This combination can effectively avoid a single point of failure, especially suitable for scenarios that require stable data collection.
Real-world code written in this way is stable
Look at this authentication template with proxies:
import requests
proxies = {
'http': 'http://用户名:密码@gateway.ipipgo.com:9020',
'https': 'http://用户名:密码@gateway.ipipgo.com:9020'
}
headers = {
'Authorization': 'Bearer your ipipgo key',
'Content-Type': 'application/json'
}
try.
resp = requests.get(
'https://api.ipipgo.com/v1/endpoint',
headers=headers,
headers=headers, proxies=proxies, timeout=10
timeout=10
)
print(resp.json())
except Exception as e.
print(f'Request went bad: {str(e)}')
Watch out for the three pits:
1. Don't write the wrong port in the proxy address. ipipgo's channel port is 9020.
2. the key should be placed after Bearer with a space in between
3. Don't set the timeout more than 15 seconds, otherwise it will affect the efficiency of acquisition
First Aid Guide for Certification Failures
When a 401 error is encountered, troubleshoot in this order:
- Check if the key is expired (ipipgo console can check the expiration date)
- Confirmation of sufficient agent account balance
- Grab the packet to see if the Authorization field in the header is formatted correctly.
- Try switching proxy nodes (use ipipgo's smart routing feature)
High-frequency questions and answers
Q: Why is it still blocked after using a proxy?
A: may be using a shared IP pool, it is recommended to change to ipipgo's exclusive IP package, each request to go to a fixed exit IP
Q: How do I handle certificate validation issues?
A: In the requests request addverify=FalseCan be temporarily skipped, but long-term use is recommended to download the root certificate in the ipipgo background to do local configuration
Q: How can I optimize the speed of asynchronous requests?
A: Use the aiohttp library with ipipgo's concurrent proxy channel, and remember to use different sub-accounts for each request to avoid triggering rate limiting
Why ipipgo?
Having tested and compared multiple service providers, ipipgo has three major killers:
- Millisecond IP switching response (others average 3-5 seconds)
- Automatic retry mechanism for failed requests
- Supports simultaneous binding of 5 terminal devices
Especially theirIntelligent Routing FunctionIt can automatically select the node with the lowest latency. Last time I helped a customer to do price monitoring, after using the collection success rate from 78% directly soared to 99%, the effect is immediately visible.
Finally, a piece of cold knowledge: many people forget to close the connection after header authentication, causing the server to accumulate a large amount of TIME_WAIT. remember to add the following to the codewith requests.Session() as s::to automatically manage the connection pool, a detail that improves the efficiency of requests by at least 301 TP3T.

