HTTPRequestHandler在python中运行php或

HTTPRequestHandler在python中运行php或

本文介绍了CGIHTTPRequestHandler在python中运行php或python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Windows上编写一个简单的python网络服务器。

I'm writing a simple python web-server on windows..

它可以工作,但现在我想运行动态脚本(php或py),而不仅仅是html页面。

it works but now I want to run dynamic scripts (php or py) and not only html pages..

这是我的代码:

from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler

class RequestsHandler(CGIHTTPRequestHandler):
    cgi_directories = ["/www"] #to run all scripts in '/www' folder
    def do_GET(self):
        try:
            f = open(curdir + sep + '/www' + self.path)
            self.send_response(200)
            self.send_header('Content-type', 'text/html')
            self.end_headers()
            self.wfile.write(f.read())
            f.close()
        except IOError:
            self.send_error(404, "Page '%s' not found" % self.path)

def main():
    try:
        server = HTTPServer(('', 80), RequestsHandler)
        server.serve_forever()
    except KeyboardInterrupt:
        server.socket.close()

if __name__ == '__main__':
    main()

如果我将php代码放在www文件夹中,我会得到页面,但代码没有被解释

if I put php code in www folder I get the page but the code isn't interpreted

我该怎么办?谢谢

推荐答案

我认为您正在研究工程。

I think you are over engineering.

#!/usr/bin/env python
import CGIHTTPServer

def main():

    server_address = ('', 8000)
    handler = CGIHTTPServer.CGIHTTPRequestHandler
    handler.cgi_directories = ['/cgi']
    server = CGIHTTPServer.BaseHTTPServer.HTTPServer(server_address, handler)
    try:
        server.serve_forever()
    except KeyboardInterrupt:
        server.socket.close()

if __name__ == '__main__':
    main()

这篇关于CGIHTTPRequestHandler在python中运行php或python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:06