Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 generates 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?

like image 297
weberc2 Avatar asked Dec 02 '25 06:12

weberc2


1 Answers

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

like image 123
Cameron Avatar answered Dec 04 '25 21:12

Cameron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!