python - 在 python 中对 CURL 使用请求
问题描述
当我使用 CURL 时,我得到以下输出,它表示代理能够通过代理连接到端点。CURL 输出是 200 连接已建立,后来显示 401 Unauthorized。只要代理能够连接(已建立 200 个连接),我就可以了,但是当我执行 python 代码时,python 输出只显示 301。我只关心是否可以通过代理建立连接。请问如何使用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
蟒蛇代码
import requests
proxies = { 'https':'proxy.config.pcp.local:3128'}
res = requests.get('https://www.sap.com', proxies=proxies)
print (res.status_code)
输出:
301
解决方案
连接也不是通过代理建立的。它200 Connection established
首先显示是因为已建立套接字连接,但随后它会遇到重定向并抛出301 Moved Permanently
. 当您使用 python 的请求模块时,它只显示最终状态代码(即 301)而不是中间套接字连接。