python HTTP Client - 代理请求作为GET参数

问题描述 投票:0回答:1

我在防火墙后面,需要以http://the-proxy-gateway.set?url=https%3A%2F%2Fapi.stripe.com的格式做所有请求。

所以我需要将所需的URL作为GET参数传递(并且还要对请求进行uri编码)。

如何配置标准Python http客户端(requests,urllib2或其他)来执行此操作?

python proxy python-requests
1个回答
0
投票

我可能会遗漏一些东西,但这种似乎只是一个常规的request.getparams设置。

此示例在代理URL是伪造的意义上不起作用,但如果您查看错误消息(为简洁起见而修改),则该参数似乎已正确编码。

In[2]: import requests
  ...:
  ...: PROXY_URL = 'http://the-proxy-gateway.set'
  ...:
  ...:
  ...: def proxy_as_get(url):
  ...:     r = requests.get(PROXY_URL, params={'url': url})
  ...:     r.raise_for_status()
  ...:     return r.json()  # ???
  ...:
In[3]: r = proxy_as_get('https://api.stripe.com')
Traceback (most recent call last):
    ...
    Max retries exceeded with
    *****
    url: /?url=https%3A%2F%2Fapi.stripe.com
    *****
    ...

另一个例子:

In[2]: import requests
In[3]: PROXY_URL = 'http://the-proxy-gateway.set'
In[4]: params = {'url': 'https://api.stripe.com'}
In[5]: req = requests.Request('GET', PROXY_URL, params=params)
In[6]: prepped = req.prepare()
In[7]: prepped.url
Out[7]: 'http://the-proxy-gateway.set/?url=https%3A%2F%2Fapi.stripe.com'
© www.soinside.com 2019 - 2024. All rights reserved.