我在 python 子行程模塊中遇到了 } 括號和雙引號的問題。這是標準終端命令的示例:
curl -X POST localhost:8080/employees -H 'Content-type:application/json' -d '{"name": "Luke Skywalker", "role": "Jedi"}'
當我嘗試像這樣使用 subprocess 模塊時: 1.
test = subprocess.Popen(
[
"curl",
"-X",
"POST",
"localhost:8080/employees",
"-H",
"'Content-type:application/json'",
"-d",
"'{",
"name:",
'"Luke Skywalker"',
'"role:"',
'"Jedi"',
"}'",
],
stdout=subprocess.PIPE,
)
output = test.communicate()[0]
我得到:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 126 0 124 100 2 4225 68 --:--:-- --:--:-- --:--:-- 5727
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
curl: (6) Could not resolve host: "Jedi"
curl: (3) unmatched close brace/bracket in URL position 1:
}'
^
還有這個:
test = subprocess.Popen(
[
"curl",
"-X",
"POST",
"localhost:8080/employees",
"-H",
"'Content-type:application/json'",
"-d",
'\'{"name":',
'"Luke Skywalker"',
'"role:"',
' "Jedi"}\' ',
],
stdout=subprocess.PIPE,
)
output = test.communicate()[0]
我得到:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 133 0 124 100 9 2699 195 --:--:-- --:--:-- --:--:-- 3325
curl: (3) URL using bad/illegal format or missing URL
curl: (3) URL using bad/illegal format or missing URL
curl: (3) unmatched close brace/bracket in URL position 8:
"Jedi"}'
^
任何幫助將不勝感激。提前致謝。
uj5u.com熱心網友回復:
您需要了解 shell 如何決議命令。執行時:
curl -X POST localhost:8080/employees -H 'Content-type:application/json' -d '{"name": "Luke Skywalker", "role": "Jedi"}'
的引數curl是:-X, POST, localhost:8080/employees, -H, Content-type:application/json, -d, 和{"name": "Luke Skywalker", "role": "Jedi"}
shell 呼叫中單引號的目的是將整個字串保留為文字引數,并防止它在空白處被拆分。此外,它是一種在引數中傳遞雙引號并防止它們被 shell 視為引號字符的機制。周圍的單引號Content-type:application/json是不必要的,因為該字串不包含任何特殊的 shell 字符,但通常的做法是參考這樣的長字串。為了文體的一致性,-Hand POSTand -Xeven 也curl應該被參考,但大多數“如果你不理解 shell 就參考所有內容”風格的擁護者很少采用這種級別的一致性。curl永遠不會看到單引號,并且試圖在 Python 的引數串列中傳遞它們是一個錯誤。初始curl命令也可以在 shell 中呼叫為:
'curl' "-X" PO""ST lo'cal'"host:8080/emp"loyees -"H" Content-type:application/json -d \{\"name\":\ \"Luke\ Skywalker\"\,\ \"role\":\ \"Jedi\"\}
在 shell 中參考是(也許)值得研究一下以理解的東西。需要理解的重要一點是,shell 洗掉了(未轉義的)引號(雙引號和單引號,但每個引號的規則略有不同),因此被呼叫的應用程式永遠不會看到它們。
請注意,我在上面參考時有點輕率。這是完全合理的參考字串Content-type:application/json,因為它是不值得的時間決定是否:和/特殊的外殼。它們不是,但在 shell 實作中有足夠多的歧義,因此非常值得參考它們。例如,符合 posix 的 shell 將cmd {a,b}通過cmd使用string呼叫來執行該字串{a,b},但是bash(即使使用 --posix )將cmd使用兩個引數a和 進行呼叫b。這在這里是相關的,因為 json 包含 a,并且如果沒有參考該引數,則不同的 shell 將對其進行不同的決議。如有疑問,請使用引號。并且始終參考您的變數,除非有特定原因不參考。
uj5u.com熱心網友回復:
串列中的每一項都是被呼叫行程的一個引數。您可以看到,您用作'Content-type:application/json'單個引數。同樣,您也可以使用 json。
見下文:
payload = '{"name":"Luke Skywalker","role:"Jedi"}'
test = subprocess.Popen(
[
"curl",
"-X",
"POST",
"localhost:8080/employees",
"-H",
"'Content-type:application/json'",
"-d",
f"'{payload}'"
],
stdout=subprocess.PIPE,
)
output = test.communicate()[0]
我把資料做成了一個變數,所以你可以很容易地轉義它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/387056.html
