Requests 简明教程
Requests - Proxy
到目前为止,我们已经看到客户端直接连接并与服务器通信。使用代理,交互如下 −
So far, we have seen clients directly connecting and talking to the server. Using proxy, the interaction happens as follows −
-
The client sends a request to the proxy.
-
The proxy sends the request to the server.
-
The server sends back the response to the proxy.
-
The proxy will send a response back to the client.
使用 Http-proxy 是分配给管理客户端和服务器之间数据交换的附加安全措施。requests 库也有处理代理的规定,使用 proxies 参数,如下所示 −
Using Http-proxy is additional security assigned to manage the data exchange between client and server. The requests libraries also have provision to handle proxy, by using the proxies param as shown below −
Example
import requests
proxies = {
'http': 'http://localhost:8080'
}
res = requests.get('http://httpbin.org/', proxies=proxies)
print(res.status_code)
请求将路由到 http://localhost :8080 URL。
The request will route to http://localhost:8080 URL.