使用Python运行cURL';s Popen会导致不同的输出到stdout
我有以下一段python 2.7代码:使用Python运行cURL';s Popen会导致不同的输出到stdout,python,curl,Python,Curl,我有以下一段python 2.7代码: import sys from subprocess import Popen, PIPE cmd = "curl -v http://172.23.85.34 2>&1" p = Popen(cmd, shell=True, stderr=PIPE, stdout=PIPE) ret = p.wait() out, err = p.communicate() sys.stdout.write("Command is:\n" + cmd +
import sys
from subprocess import Popen, PIPE
cmd = "curl -v http://172.23.85.34 2>&1"
p = Popen(cmd, shell=True, stderr=PIPE, stdout=PIPE)
ret = p.wait()
out, err = p.communicate()
sys.stdout.write("Command is:\n" + cmd + "\nOutput:\n" + out)
出于某种原因,运行python脚本时得到的输出与运行curl-v时得到的输出不同http://172.23.85.34 2> &1
直接来自bash。这是什么原因
Python输出:
Command is:
curl -v http://172.23.85.34 2>&1
Output:
* Rebuilt URL to: http://172.23.85.34/
* Trying 172.23.85.34...
* TCP_NODELAY set
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to 172.23.85.34 (172.23.85.34) port 80 (#0)
> GET / HTTP/1.1
> Host: 172.23.85.34
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Thu, 23 Mar 2017 13:35:06 GMT
< Server: Apache/2.4.23 (Unix) OpenSSL/1.0.2g
< Upgrade: h2
< Connection: Upgrade
< Last-Modified: Tue, 20 Dec 2016 09:33:57 GMT
< ETag: "61-54413bc4fb12a"
< Accept-Ranges: bytes
< Content-Length: 97
< Content-Type: text/html
<
{ [97 bytes data]
* Curl_http_done: called premature == 0
100 97 100 97 0 0 5307 0 --:--:-- --:--:-- --:--:-- 5388
* Connection #0 to host 172.23.85.34 left intact
<html>
<body>
<h1>It works!</h1>
<img src="Helium.jpg" alt="Helium">
</body>
</html>
$ curl -v http://172.23.85.34 2>&1
* Rebuilt URL to: http://172.23.85.34/
* Trying 172.23.85.34...
* TCP_NODELAY set
* Connected to 172.23.85.34 (172.23.85.34) port 80 (#0)
> GET / HTTP/1.1
> Host: 172.23.85.34
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Thu, 23 Mar 2017 13:37:01 GMT
< Server: Apache/2.4.23 (Unix) OpenSSL/1.0.2g
< Upgrade: h2
< Connection: Upgrade
< Last-Modified: Tue, 20 Dec 2016 09:33:57 GMT
< ETag: "61-54413bc4fb12a"
< Accept-Ranges: bytes
< Content-Length: 97
< Content-Type: text/html
<
<html>
<body>
<h1>It works!</h1>
<img src="Helium.jpg" alt="Helium">
</body>
</html>
* Curl_http_done: called premature == 0
* Connection #0 to host 172.23.85.34 left intact
命令是:
旋度-vhttp://172.23.85.34 2>&1
输出:
*重新生成的URL到:http://172.23.85.34/
*正在尝试172.23.85.34。。。
*TCP_节点集
%总接收百分比%x平均速度时间电流
数据加载上载总左速度
0 0 0 0 0 0 0--:-:-:-:---:-:---0*已连接到172.23.85.34(172.23.85.34)端口80(#0)
>GET/HTTP/1.1
>主持人:172.23.85.34
>用户代理:curl/7.51.0
>接受:*/*
>
卷曲输出:
Command is:
curl -v http://172.23.85.34 2>&1
Output:
* Rebuilt URL to: http://172.23.85.34/
* Trying 172.23.85.34...
* TCP_NODELAY set
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Connected to 172.23.85.34 (172.23.85.34) port 80 (#0)
> GET / HTTP/1.1
> Host: 172.23.85.34
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Thu, 23 Mar 2017 13:35:06 GMT
< Server: Apache/2.4.23 (Unix) OpenSSL/1.0.2g
< Upgrade: h2
< Connection: Upgrade
< Last-Modified: Tue, 20 Dec 2016 09:33:57 GMT
< ETag: "61-54413bc4fb12a"
< Accept-Ranges: bytes
< Content-Length: 97
< Content-Type: text/html
<
{ [97 bytes data]
* Curl_http_done: called premature == 0
100 97 100 97 0 0 5307 0 --:--:-- --:--:-- --:--:-- 5388
* Connection #0 to host 172.23.85.34 left intact
<html>
<body>
<h1>It works!</h1>
<img src="Helium.jpg" alt="Helium">
</body>
</html>
$ curl -v http://172.23.85.34 2>&1
* Rebuilt URL to: http://172.23.85.34/
* Trying 172.23.85.34...
* TCP_NODELAY set
* Connected to 172.23.85.34 (172.23.85.34) port 80 (#0)
> GET / HTTP/1.1
> Host: 172.23.85.34
> User-Agent: curl/7.51.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Thu, 23 Mar 2017 13:37:01 GMT
< Server: Apache/2.4.23 (Unix) OpenSSL/1.0.2g
< Upgrade: h2
< Connection: Upgrade
< Last-Modified: Tue, 20 Dec 2016 09:33:57 GMT
< ETag: "61-54413bc4fb12a"
< Accept-Ranges: bytes
< Content-Length: 97
< Content-Type: text/html
<
<html>
<body>
<h1>It works!</h1>
<img src="Helium.jpg" alt="Helium">
</body>
</html>
* Curl_http_done: called premature == 0
* Connection #0 to host 172.23.85.34 left intact
$curl-vhttp://172.23.85.34 2>&1
*重新生成的URL到:http://172.23.85.34/
*正在尝试172.23.85.34。。。
*TCP_节点集
*连接到172.23.85.34(172.23.85.34)端口80(#0)
>GET/HTTP/1.1
>主持人:172.23.85.34
>用户代理:curl/7.51.0
>接受:*/*
>
在python脚本的输出中有进度表
信息
从男子卷发:
PROGRESS METER
curl normally displays a progress meter during operations, indicating the amount of transferred data, transfer speeds and estimated time left, etc.
curl displays this data to the terminal by default, so if you invoke curl to do an operation and it is about to write data to the terminal, it disables the progress
meter as otherwise it would mess up the output mixing progress meter and response data.
简单地说,默认情况下,curl将显示progress info
,但如果输出为terminal,则禁用它,因此当您运行python脚本时,curl
的输出不是terminal,而是通过管道传输到python进度,使进度信息
在输出中
您将使用选项-s
获得相同的输出以禁用它:
curl -v -s https://httpbin.org/get 2>&1