How can I route HTTP requests through a python script?
I've thoroughly searched for the answer for my question, but I don't think I know the correct terminology to make my searches effective. Anyway, I'm looking to set up my web server to route a request for a static page through a Python "PageBuilder" program which will dynamically implant content into templates.
In other words:
- user requests mysite.com/index.html
- server passes request to certain python script (my server is currently Apache2)
- python script g开发者_StackOverflow中文版enerates output, passes output to server
- server serves content to user
Could someone please help me or at least give me the "proper terminology" with which to search?
This isn't really static content any more. You want to dynamically generate content using a script.
In Python, this is generally done using the WSGI interface (in olden days, the de facto standard was CGI), which defines a standard way for your Python code to get input (information about the HTTP request), and send back a response (HTTP headers, and the content you want to serve -- typically HTML).
To this end, there's the excellent mod_wsgi
Apache module that sets up the plumbing between the server-stuff (receiving requests and sending back content) and your code, using this WSGI interface.
You may also want to check out some sort of templating engine to make generating dynamic HTML easier (my favourite is Jinja2).
This is called CGI. http://en.wikipedia.org/wiki/Common_Gateway_Interface
精彩评论