简体   繁体   English

curl --noproxy“ *”在Python的请求模块中等效

[英]curl --noproxy “*” equivalent in Python's request module

I'm trying to access an API on a server within my network using Python's request module, but am getting a 502 Bad Gateway error. 我正在尝试使用Python的request模块访问网络中服务器上的API,但遇到502 Bad Gateway错误。 When I try to do something equivalent using curl , I get the same error and it's because the request is trying to run through the proxy server. 当我尝试使用curl进行等效操作时,出现相同的错误,这是因为请求尝试通过代理服务器运行。 Here is the output from curl : 这是curl的输出:

$ curl -v -k -u username:password https://hostname:port/some/api/endpoint              * Uses proxy env variable no_proxy == 'hostname'
* Uses proxy env variable https_proxy == 'http://10.3.0.40:10256'
*   Trying 10.3.0.40...
* TCP_NODELAY set
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* Connected to 10.3.0.40 (10.3.0.40) port 10256 (#0)
* allocate connect buffer!
* Establish HTTP proxy tunnel to hostname:port
* Server auth using Basic with user 'user'
> CONNECT hostname:port HTTP/1.1
> Host: hostname:port
> User-Agent: curl/7.61.1
> Proxy-Connection: Keep-Alive
>
< HTTP/1.0 502 Bad Gateway
< Server: Zscaler/5.6
< Content-Type: text/html
< Connection: close
<
* Received HTTP code 502 from proxy after CONNECT
* CONNECT phase completed!
* Closing connection 0
curl: (56) Received HTTP code 502 from proxy after CONNECT

As the output states at the top, curl is automatically implementing the HTTP_PROXY variable I have in my environment variables, which is needed for other Python tasks. 当输出位于顶部时, curl会自动实现我在环境变量中拥有的HTTP_PROXY变量,这是其他Python任务所需的。 In order to avoid this, I can pass --noproxy "*" to curl and the request no longer tries to run through the proxy. 为了避免这种情况,我可以传递--noproxy "*"进行curl并且该请求不再尝试通过代理运行。

I'd like to try to get the equivalent working with Python's request module. 我想尝试与Python的request模块一起使用。 I know that requests will automatically read from the environment variables as well, so I tried to bypass that specifying proxies=None in the code: 我知道requests也会自动从环境变量中读取,因此我尝试绕过在代码中指定proxies=None requests

response = requests.get('https://hostname:port/some/api/endpoint', proxies=None, auth=(USER, PASS))

But I still get the 502 Bad Gateway. 但是我仍然得到502 Bad Gateway。 In looking at some questions on SO, it looks as if you can specify the NO_PROXY environment variable and provide URLs that should not use the proxy. 在查看有关SO的一些问题时,似乎可以指定NO_PROXY环境变量并提供不应使用代理的URL。 I tried populating that variable with https://hostname , https://hostname:port , https://hostname/* , https://hostname:port/* , and hostname . 我尝试使用https://hostnamehttps://hostname:porthttps://hostname/*https://hostname:port/*hostname填充该变量。 None of which seemed to work. 这些似乎都不起作用。 (Are those even the right formats?) (这些格式是否正确?)

How do I get requests to use the equivalent of curl's --noproxy ? 我如何requests使用卷曲的相当于--noproxy

Setting proxies to None is like omitting it. proxies设置为“ None就像忽略它。 You want to specify that the http and https proxies are None by passing the value: 您想通过传递值来指定http和https代理为None

proxies={'http': None, 'https': None}

to the requests.get function. requests.get函数。

Regarding NO_PROXY (or no_proxy ), its value is a domain name , so you shouldn't add http:// or https:// in front of its value, just use the domain name hostname in your case. 关于NO_PROXY (或no_proxy ),其值是一个域名 ,因此您不应在其值前添加http://https:// ,而应使用域名hostname

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM