简体   繁体   中英

python pexpect failing for curl output

I am capturing a curl output after spawning to a remote machine. The expect function keeps getting timed out , i tried different patterns still no luck. The curl request is of the form ,

hdl2.sendline("curl -v http://{0}/index.html -o /dev/null".format(host1))

The output received is

" > GET /index.html HTTP/1.1
> User-Agent: curl/7.35.0
> Host: 13.126.208.1
> Accept: */*

< HTTP/1.1 200 OK
< Date: Sun, 20 Aug 2017 12:32:54 GMT
* Server Apache/2.4.7 (Ubuntu) is not blacklisted
< Server: Apache/2.4.7 (Ubuntu)
< Last-Modified: Sun, 20 Aug 2017 09:56:44 GMT
< ETag: "2cf6-5572c61363668"
< Accept-Ranges: bytes
< Content-Length: 11510
< Vary: Accept-Encoding
< Content-Type: text/html
< 
{ [data not shown]
100 11510  100 11510    0     0  3055k      0 --:--:-- --:--:-- --:--:-- 3746k
* Connection #0 to host 13.126.208.1 left intact
ubuntu@ip-172-31-28-48:~$ "

This is the end output , i have given expect as

hdl2.expect("\$ ")

But every time i get pexpect timeout. Any suggestions is appreciated.

This may happen because of line buffering: ubuntu@ip-172-31-28-48:~$ is not terminated with \\n , so except may not consider this line. You can try this:

hdl2.sendline("curl -v http://{0}/index.html -o /dev/null; echo DONE".format(host1))
hdl2.expect("DONE")

(Use a string that will be unique for your data instead of DONE .)

Pexpect's default timeout is 30 seconds. If your curl command needs more time then you need to increase the timeout value. For example:

hdl2.expect("\$ ", timeout=600)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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