简体   繁体   English

Python中的子进程超时

[英]Subprocess Timeout in Python

I am trying to check the header of a website and the code works perfectly fine. 我正在尝试检查网站的标题,并且代码工作正常。 However when the website does not respond within a reasonable amount of time, I added a timeout and that works too. 但是,当网站在合理的时间内没有响应时,我添加了一个超时,它也可以正常工作。

Unfortunately the command is not taking parameters and am struck over there. 不幸的是,该命令未获取参数,并被击中。 Any suggestions would be highly appreciated 任何建议将不胜感激

import subprocess
from threading import Timer
kill = lambda process: process.kill()
c1='curl -H'
cmd = [c1, 'google.com']
p = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
my_timer = Timer(10, kill, [p])
try:
    my_timer.start()
    stdout, stderr = p.communicate()
    print stdout
finally:
    print stderr
    my_timer.cancel()

Error while running : 运行时出错:

OSError: [Errno 2] No such file or directory OSError:[Errno 2]没有这样的文件或目录

However if I change c1 as shown below, it works fine. 但是,如果我按如下所示更改c1,则效果很好。

c1='curl' C1 = '卷曲'

With

c1='curl' 

use 采用

cmd = [c1, '-H','google.com']

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

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