子进程Python中的curl问题 [英] Curl issue in subprocess Python
问题描述
当我尝试在命令行上运行以下命令时,我获得了存储大小。
curl -G - dkey = val'http://172.16.26.2:9005/as/system/storage'
{
userQuotaMax:675048,
userQuotaUsed:439191
}
如果我尝试在我的python脚本中运行,数据。
arg_list = curl -G -dkey = val'http://172.16.26.2:9005/as / system / storage'
p = Popen(arg_list,shell = True,stdin = PIPE,stdout = PIPE,stderr = STDOUT,executable ='/ bin / bash')
$ b b output = p.stdout.read()
打印输出
而不是使用popen()调用curl,你可能更适合使用请求。 / p>
http:/ /docs.python-requests.org/en/master/user/quickstart/
这样的东西):
导入请求
payload = {key:val}
response = requests.get(http://172.16.26.2:9005/as/system/storage,data = payload)
print(response.text)
所以像(POST):
导入请求
payload = {key:val}
response = requests.post (http://172.16.26.2:9005/as/system/storage,data = payload)
print(response.text)
应该返回(假设api返回html / text,如果它返回JSON,看看response.json在以上链接):
{
userQuotaMax:675048,
userQuotaUsed:439191
}
希望这可以帮助你
When I tried to run the following command on the command line, I get the storage size.
curl -G -d "key=val" 'http://172.16.26.2:9005/as/system/storage'
{
"userQuotaMax" : 675048,
"userQuotaUsed" : 439191
}
If I try to run in my python script, Then, I can'not get the same data.
arg_list = curl -G -d "key=val" 'http://172.16.26.2:9005/as/system/storage'
p = Popen(arg_list, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, executable='/bin/bash')
output = p.stdout.read()
print output
Any helps would be really appreciated.
Instead of calling curl using popen(), you might be better served by using requests.
http://docs.python-requests.org/en/master/user/quickstart/
so something like (for GET):
import requests
payload = {"key" :"val"}
response = requests.get("http://172.16.26.2:9005/as/system/storage", data=payload)
print(response.text)
so something like (for POST):
import requests
payload = {"key" :"val"}
response = requests.post("http://172.16.26.2:9005/as/system/storage", data=payload)
print(response.text)
Should return (assuming the api returns html/text, if it returns JSON, have a look at response.json() explained at the link above):
{
"userQuotaMax" : 675048,
"userQuotaUsed" : 439191
}
Hope this helps you
这篇关于子进程Python中的curl问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!