CGIHTTPRequestHandler runs php or python script in python

I am writing a simple python web server on windows.

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

here is my code:

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()

if I put the php code in the www folder, I get the page, but the code is not interpreted

what should I do? thank

+3
source share
3 answers

I think you're 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()
+4
source

Have you made the php executable? chmod + x spam.php (for Linux, I have no idea how to make files executable on windows)

You will also need a PHP interpreter installed on your PC.

source of the answer HERE

.

+1

The problem is the CGIHTTPServer class. It does not set CGI env variables. This has been fixed here:
https://github.com/gabrielgrant/tonto/blob/master/tonto.py

0
source

All Articles