簡體   English   中英

使用 requests / http.client 在 python 中運行 curl 調用

[英]run curl call in python using requests / http.client

我正在嘗試在 python 中執行 curl 調用。

curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" --header "Authorization:xxxx" -d "{
\"ProductNames\": [
\"PReport\"
],
\"SearchType\": \"FullAddress\",
\"FullAddress\": \"123 Main Street, New York, 10001\",
\"ReferenceId\": \"\"
}" "https://api.aws.com:443/api/rep/grep"

我努力了:

 import http.client 
 import urllib.parse 

 addr = '123 Main Street, New York, 10001'
 prod = 'PReport'
 stype = 'FullAddress'
 

 conn = http.client.HTTPSConnection("https://api.aws.com:443/api/rep/grep") 

    headers = { 
        'Accept': "application/json", 
        'Authorization': "xxxx", 
        } 

    address = urllib.parse.quote(addr)

    url = "https://api.aws.com:443/api/rep/grep?ProductName={}&SearchType={}&FullAddress={}".format(prod, stype, address)

    conn.request("GET", url, headers=headers)

    res = conn.getresponse() 

在招搖,我只看到: https://api.aws.com:443/api/rep/grep ,而不是完整的請求網址。

如何使用上面的信息在 python 中巧妙地進行 curl 調用?

當你說你想執行那個 curl 調用時,我假設它可以工作並且你想用 Python 發出同樣的請求。

您的 Python 代碼可能存在更多問題,但我立即發現一個我認為不正確的問題是您使用 URL 查詢參數,而 curl 命令使用-d ,這是--data的縮寫形式。 以下是相關文檔:

- 數據

(HTTP MQTT) 將 POST 請求中的指定數據發送到 HTTP 服務器,就像瀏覽器在用戶填寫 HTML 表單並按下提交按鈕時所做的一樣。 這將導致 curl 使用內容類型 application/x-www-form-urlencoded 將數據傳遞給服務器。 與 -F、--form 進行比較。

[...]

https://curl.se/docs/manpage.html

這意味着它進入請求正文。 但是,在您的 Python 代碼中,您將它們作為查詢字符串參數放置,這似乎並不等效。 但是您還說您“看不到完整的請求網址”,這讓我有點困惑,因為您不應該看到除“https://api.aws.com:443/api/rep/grep”之外的其他網址" 卷曲,或者。 一種快速查看使用 curl 得到什么的方法是使用工具“netcat”,當您將它安裝在系統上時,通常由“nc”調用。

例如,要監聽 1234 端口,你可以這樣調用 netcat:

nc -l 1234

有關 netcat 的更多信息,請參閱https://linux.die.net/man/1/nc 當然,您還可以使用其他工具。 這只是一個建議。

當使用上面的 curl 命令行對 localhost 端口 1234 發出相同的請求時,它會打印以下內容:

POST /api/rep/grep HTTP/1.1
Host: localhost:1234
User-Agent: curl/7.79.1
Content-Type: application/json
Accept: application/json
Authorization:xxxx
Content-Length: 132

{
"ProductNames": [
"PReport"
],
"SearchType": "FullAddress",
"FullAddress": "123 Main Street, New York, 10001",
"ReferenceId": ""
}

因此,請求參數並不是您認為的 URL 的一部分,並且您可能對您應該在 API 端點的實現代碼中看到的內容有錯誤的期望。

但是,如果 curl 調用仍然有效,那么與您的 curl 調用更等效的 Python 代碼可能是這樣的:

import requests

url = "https://api.aws.com:443/api/rep/grep"
headers = {
    "Content-Type": "application/json",
    "Accept": "application/json",
    "Authorization": "xxxx"
}
data = """{
    "ProductNames": [ "PReport" ],
    "SearchType": "FullAddress",
    "FullAddress": "123 Main Street, New York, 10001",
    "ReferenceId": ""
}"""

resp = requests.post(url, headers=headers, data=data)
print(resp.status_code)

此代碼在以相同方式針對 netcat 偵聽的端口運行時,會顯示一個與上述 curl 調用相匹配的請求。

在最新版本中還有一個json參數,可以通過使用字典來發送數據,從而使代碼更清晰:

data = {
    "ProductNames": [ "PReport" ],
    "SearchType": "FullAddress",
    "FullAddress": "123 Main Street, New York, 10001",
    "ReferenceId": ""
}
requests.post(url, headers=headers, json=data)

可以在https://requests.readthedocs.io/en/latest/下找到最新版本的 requests Python 模塊的文檔。

此外,在您的問題中轉換各種執行 HTTP 請求的方式的任務非常普遍,以至於有很多在線工具可以自動執行此操作。 一個例子是https://curlconverter.com/ 該網站還包含其他相關信息,但我沒有仔細檢查您的查詢結果。 它看起來確實產生了相同的代碼。 不過,在過度依賴自動化工具之前,最好花點時間了解常見 HTTP 請求的各個部分是如何傳輸的。

暫無
暫無

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

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