Python FastCGI Application and LiteSpeed

This is a generic FCGI how-to for Python applications. Although we use Jon's fcgi python library for this example, you can use any valid Python FCGI module for your own setup.

  1. LiteSpeed
  2. Python
  3. Jon's Python FCGI library: jonpy.sourceforge.net

1) Create a Virtual Host for this setup. From now on we will refer to this vhost as PythonVhost.

2) Save the following sample Python FCGI application to a path of your choice. We will call this app “demoapp.py” for tutorial purposes.

#!/usr/bin/python
import jon.cgi as cgi
import jon.fcgi as fcgi
from cgi import escape
import traceback

class Handler(cgi.Handler):
  def process(self, req):
    req.set_header("Content-Type", "text/html")
    req.write("Hello Python! Hello LiteSpeed!<hr size=1>")
    keys = req.environ.keys()
    keys.sort()
    for k in keys:
        req.write(escape(k) + " = " + escape(req.environ[k]) + "<br>")
    req.write(traceback.print_exc())

fcgi.Server({fcgi.FCGI_RESPONDER: Handler}).run()

3) Go “PythonVhost → External Apps” tab and create a new “External Application”.

Name: MyPythonFCGI	
Address: uds://tmp/lshttpd/mypythonfcgi.sock
Max Connections: 10
Initial Request Timeout (secs): 10
Retry Timeout (secs): 0
Persistent Connection	Yes
Connection Keepalive Timeout: 30
Response Bufferring: No	
Start By Server: Yes
Command: /absolutepathto/demoapp.py  <--- path to python fcgi app
Back Log: 50
Instances: 10

4) Go to “PythonVhost → Contexts” and create a new “FCGI Context”.

URI: /python/   <--- URI path to bind to python fcgi app we created	
Fast CGI App: [VHost Level] MyPythonFCGI  <--- select the python extapp we just created

5) Restart LiteSpeed, “lswsctrl restart”, and access your new Pythyon FCGI application with /python/ url to your vhost. Enjoy! =)

  • Admin
  • Last modified: 2018/09/13 20:44
  • by Michael Alegre