简体   繁体   English

在 python 中对 CURL 使用请求

[英]Using request in python for CURL

When I use CURL, I am getting the below output, which says that proxy was able to connect to the endpoint via proxy.当我使用 CURL 时,我得到以下输出,它表示该代理能够通过代理连接到端点。 CURL output was 200 connection established and later showing the 401 Unauthorized. CURL 输出是 200 连接建立,后来显示 401 未经授权。 I am OK as long as proxy was able to connect (200 connection established) but when I execute the python code, the python output is showing only 301. I only care about if the connection can be made over the proxy or not.只要代理能够连接(建立了 200 个连接),我就可以了,但是当我执行 python 代码时,python 输出只显示 301。我只关心是否可以通过代理建立连接。 Can I please how can I check the (200 connection established) like in the CURL output using the python?请问如何使用python检查CURL输出中的(已建立200个连接)?

export HTTPS_PROXY=<proxy_details> && curl -vvv https://www.sap.com


* About to connect() to proxy proxy.abc.local.port 1256 (#0)
*   Trying 169.31.123.234...
* Connected to proxy.abc.local.port () port 1256 (#0)
* Establish HTTP proxy tunnel to www.sap.com:443
> CONNECT www.sap.com:443 HTTP/1.1
> Host: www.sap.com:443
> User-Agent: curl/7.29.0
> Proxy-Connection: Keep-Alive
> 
< HTTP/1.1 200 Connection established
< 
* Proxy replied OK to CONNECT request
* Initializing NSS with certpath: sql:/etc/pki/nssdb
*   CAfile: /etc/pki/tls/certs/ca-bundle.crt
  CApath: none
* SSL connection using TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384
* Server certificate:
*   subject: CN=www.sap.com,O=SAP SE,L=Walldorf,ST=Baden-Württemberg,C=DE
*   start date: May 26 00:00:00 2021 GMT
*   expire date: May 31 23:59:59 2022 GMT
*   common name: www.sap.com
*   issuer: CN=GeoTrust RSA CA 2018,OU=www.digicert.com,O=DigiCert Inc,C=US
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.sap.com
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Content-Type: text/html; charset=iso-8859-1
< Content-Length: 238
< Strict-Transport-Security: max-age=15724800; includeSubDomains
< Location: https://www.sap.com/index.html
< Date: Sat, 24 Jul 2021 03:44:35 GMT
< Connection: keep-alive
< 
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="https://www.sap.com/index.html">here</a>.</p>
</body></html>
* Connection #0 to host   proxy.abc.local.port left intact

python code蟒蛇代码

import requests 
proxies = { 'https':'proxy.config.pcp.local:3128'}
res = requests.get('https://www.sap.com', proxies=proxies)
print (res.status_code)

output:输出:

301

The connection is not made over proxy too.连接也不是通过代理建立的。 It shows 200 Connection established at first because the socket connection is established but then it hits the redirect and it throws 301 Moved Permanently .它首先显示200 Connection established ,因为套接字连接已建立,但随后它命中重定向并抛出301 Moved Permanently When you are using python's request module it only shows the final status code (which is 301) and not the intermediate socket connections.当您使用 python 的请求模块时,它只显示最终状态代码(即 301)而不是中间套接字连接。

You should probably try to directly send only the CONNECT request then, to have a chance at looking at thats response code.您可能应该尝试仅直接发送 CONNECT 请求,以便有机会查看该响应代码。

But I don't think Python requests allows that.但我认为 Python requests不允许这样做。 Lower-level tools like http.client do though:但是像http.client这样的低级工具可以:

import http.client
conn = http.client.HTTPConnection("proxy.config.pcp.local", 1256)
conn.request("CONNECT", "www.sap.com:443",'',{"Host":"www.sap.com:443"})
response = conn.getresponse()
print(response.status)

(use http.client.HTTPSConnection in case of an HTTPS proxy) (在 HTTPS 代理的情况下使用http.client.HTTPSConnection

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

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