繁体   English   中英

带参数的python curl execute命令

[英]python curl execute command with parameter

我需要执行以下命令

curl -v -H 'X-Auth-User: myaccount:me' -H 'X-Auth-Key: secretpassword' http://localhost:8080/auth/v1.0/

当我从终端运行它时它工作得很好。 并给我结果如下

* About to connect() to localhost port 8080 (#0)
*   Trying ::1...
* Connection refused
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> GET /auth/v1.0/ HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:8080
> Accept: */*
> X-Auth-User: myaccount:me
> X-Auth-Key: secretpassword
> 
< HTTP/1.1 200 OK
< X-Storage-Url: http://localhost:8080/v1/AUTH_myaccount
< X-Auth-Token: AUTH_tk8bf07349d36041339450f0b46a2adc39
< Content-Type: text/html; charset=UTF-8
< X-Storage-Token: AUTH_tk8bf07349d36041339450f0b46a2adc39
< Content-Length: 0
< X-Trans-Id: tx99a9e2a129f34ab487ace-00553cb059
< Date: Sun, 26 Apr 2015 09:31:05 GMT
< 
* Connection #0 to host localhost left intact

但我需要从python运行它。 我以下面的方式使用了subprocess.call和subprocess.popen

import subprocess
subprocess.call(["curl", "-v -H 'X-Auth-User: myaccount:me' -H 'X-Auth-Key: secretpassword' http://localhost:8080/auth/v1.0/"], shell=False)

但我得到了跟随错误

curl: option -v -H 'X-Auth-User: myaccount:me' -H 'X-Auth-Key: secretpassword' http://localhost:8080/auth/v1.0/: is unknown
curl: try 'curl --help' or 'curl --manual' for more information

使用popen

result = subprocess.Popen(["curl", "-v -H 'X-Auth-User: myaccount:me' -H 'X-Auth-Key: secretpassword' http://localhost:8080/auth/v1.0/"])
print(result)

并得到这个错误

curl: option -v -H 'X-Auth-User: myaccount:me' -H 'X-Auth-Key: secretpassword' http://localhost:8080/auth/v1.0/: is unknown
curl: try 'curl --help' or 'curl --manual' for more information
<subprocess.Popen object at 0x7fd003d82cd0>

怎么解决这个???

由于call需要传递一个命令行参数数组,您可以自己拆分命令行并调用如下:

subprocess.call([
    "curl", "-v", "-H", "X-Auth-User: myaccount:me", "-H", 
    "X-Auth-Key: secretpassword", "http://localhost:8080/auth/v1.0/"
], shell=False)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM