python调用子进程实例,来自cherrypy-cgiserver
2013-12-03 15:57
351 查看
python调用子进程实例,调用php-cgi.exe,生成网页内容,截取自cherrypy-cgiserver。
import subprocess import tempfile from cStringIO import StringIO import threading PHP_CGI = 'H:/web/php_server_simple/php/php-cgi.exe' def test2(): response = tempfile.SpooledTemporaryFile( max_size =1024*1024, mode = "w+b" ) handler_executable=PHP_CGI cmd_args=[handler_executable,"phpinfo.php"] dir="H:/web/django-php-master" proc = subprocess.Popen( cmd_args, executable = handler_executable, stdin = subprocess.PIPE, stdout = response, stderr = subprocess.STDOUT, cwd = dir ) proc.force_terminated = False body_file = StringIO() proc.stdin.write(body_file.read()) def terminate_cgi_process(): """ Terminiert nach einem Timeout den CGI-Prozess """ proc.terminate() proc.force_terminated = True # Timeout-Timer starten, der nach Ablauf den CGI-Prozess terminiert timer = threading.Timer(5, terminate_cgi_process) timer.start() # Los geht's (hier wird gewartet bis das CGI-Programm fertig ist) proc.communicate() # Timeout-Timer abbrechen, da er hier nicht mehr ben?tigt wird timer.cancel() # Falls der Timeout-Timer den CGI-Prozess abbrechen musste, wird der # GATEWAY_TIMEOUT-Fehler ausgel?st. if proc.force_terminated: raise cherrypy.HTTPError(httplib.GATEWAY_TIMEOUT) # Get header lines response.seek(0) print response.read() if __name__=="__main__" : test2()
相关文章推荐
- python使用mysqldb连接数据库操作方法示例详解
- 二种python发送邮件实例讲解(python发邮件附件可以使用email模块实现)
- python日志管理:logging
- Python读写文件 - 转
- python paramiko实现ssh远程访问的方法
- Python的控制结构(转)
- python 的import机制
- python's fnmatch&glob&os.listdir
- PYTHON LIB
- VS2008 编译 boost::python boost::serialization 方法
- python 高级学习网址
- 【转】python OS
- Python3 下 cx_freeze 打包 PyQt5
- 各种工具使得数据分析工作使用python变得越来越流行
- python 根据类名实例化对象
- python代码refine实例
- Python IDLE 运行错误:IDLE's subprocess didn't make connection 的解决方法
- Python面向对象编程
- 去除文件中的重复元素后写入文件 分类: python 小练习 open()文件读写 2013-12-03 11:21 356人阅读 评论(0) 收藏
- python 读取文件基本格式