请求:如何禁用/绕过代理 [英] requests: how to disable / bypass proxy

查看:120
本文介绍了请求:如何禁用/绕过代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个网址:

r = requests.get("http://myserver.com")

正如我在myserver.com"的access.log"中看到的,使用了客户端的系统代理.但我想完全禁用 requests 使用代理.

As I can see in the 'access.log' of "myserver.com", the client's system proxy is used. But I want to disable using proxies at all with requests.

推荐答案

我目前知道的禁用代理的唯一方法完全如下:

The only way I'm currently aware of for disabling proxies entirely is the following:

  • 创建会话
  • session.trust_env 设置为 False
  • 使用该会话创建您的请求
import requests

session = requests.Session()
session.trust_env = False

response = session.get('http://www.stackoverflow.com')

这是基于 此评论来自 Lukasa 和(有限) requests.Session.trust_env.

This is based on this comment by Lukasa and the (limited) documentation for requests.Session.trust_env.

注意:trust_env 设置为 False 也会忽略以下内容:

Note: Setting trust_env to False also ignores the following:

  • 来自 .netrc 的身份验证信息(代码)
  • REQUESTS_CA_BUNDLECURL_CA_BUNDLE (代码)
  • Authentication information from .netrc (code)
  • CA bundles defined in REQUESTS_CA_BUNDLE or CURL_CA_BUNDLE (code)

但是,如果您只想禁用特定域的代理(例如 localhost),您可以使用 NO_PROXY 环境变量:

If however you only want to disable proxies for a particular domain (like localhost), you can use the NO_PROXY environment variable:

import os
import requests

os.environ['NO_PROXY'] = 'stackoverflow.com'

response = requests.get('http://www.stackoverflow.com')

这篇关于请求:如何禁用/绕过代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆