簡體   English   中英

在 python 中對 CURL 使用請求

[英]Using request in python for CURL

當我使用 CURL 時,我得到以下輸出,它表示該代理能夠通過代理連接到端點。 CURL 輸出是 200 連接建立,后來顯示 401 未經授權。 只要代理能夠連接(建立了 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)而不是中間套接字連接。

您可能應該嘗試僅直接發送 CONNECT 請求,以便有機會查看該響應代碼。

但我認為 Python requests不允許這樣做。 但是像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)

(在 HTTPS 代理的情況下使用http.client.HTTPSConnection

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM