python - 为什么 requests.post() 不在 python 中使用提供的代理?
问题描述
我正在尝试发送带有 http 代理列表的发布请求。有一段时间我认为代理工作正常,直到我在列表中添加一个假代理以确保一切都像这样工作:
def func_name(i):
url = 'https://www.some-url.com/Cart/ajax/page.php'
# Proxies to connect with
proxies_list = [
'http://1.1.1.1:2000',
'http://2.2.2.2:2000',
'http://1.2.3.0:2000' # This is the fake one
]
proxy_index = random.randint(0, len(proxies_list) - 1)
proxy = {"http": proxies_list[proxy_index]}
# List of user agents
headers_list = [
'Linux Mozilla 5/0',
'Linux Mozilla 5/0',
'Linux Mozilla 5/0'
]
headers_index = random.randint(0, len(headers_list) - 1)
headers = {'user-agent':headers_list[headers_index], 'Accept-Encoding':'none'}
payload = {'dataToValidate':str(i),'actionName':'nc_signup'}
answer = requests.post(url=url, headers=headers, proxies=proxy, data=payload).json()
print(answer)
When proxy_index = random.randint(0, len(proxies_list) - 1)
picks the fake one I get an answer anyways, the reason for that may be because the requests.post()
function doesn't even use the argument proxies=proxy
as expected.
解决方案
由于您仅proxy
使用选项构造对象http
,因此您尝试加载的 HTTPS URL 不会被代理。改变
proxy = {"http": proxies_list[proxy_index]}
至
proxy = {"http": proxies_list[proxy_index], "https": proxies_list[proxy_index]}
应该解决问题。
推荐阅读
- r - 更新 R 中的 plotly 数据(chloropleth),无需重新渲染整个地图
- python - 增加次要y轴和x轴之间的空间?
- html - browseVignettes:从输出中删除 `source` 和 `R code` 文件
- machine-learning - 启用 Eager Execution 时不支持 K.gradients。改用 GradientTape。我如何直接在我的代码中替换它
- java - 如何解析单个 json 对象响应?
- python - 使用 pysftp 成功启动 SFTP 后的 OSError
- reactjs - 为什么我的图像没有显示在我的反应项目中
- button - Plotly:如何为每个子图添加下拉菜单?
- oauth-2.0 - 如何在不通过 EWSManagedAPI 使用代理生成的类 (ExchangeServicePortTypeClient) 调用 EWS 端点时传递令牌
- r - ggplot饼图外的文本标签