Search code examples
python-3.xfastcgilighttpdweb.py

lighttpd + webpy + fastCGI + python3 How it works?


Story: I wrote python3 application with web.py framework (some parser). I like it, but it couldn't process the load (some limit for 10 workers). I found the solution from web.py documentation - Webpy + LightTTPD with FastCGi.

When I try to use it and start lighttpd with python3 application I have an error:

     File "code.py", line 18, in <module>
if __name__ == '__main__':application.run()
File "/usr/local/lib/python3.6/site-packages/web/application.py", line 341, in run
return wsgi.runwsgi(self.wsgifunc(*middleware))
File "/usr/local/lib/python3.6/site-packages/web/wsgi.py", line 34, in runwsgi
or 'SERVER_SOFTWARE') in os.environ:
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/_collections_abc.py", line 666, in __contains__
self[key]
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/os.py", line 666, in __getitem__
value = self._data[self.encodekey(key)]
File "/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6/lib/python3.6/os.py", line 744, in encode
raise TypeError("str expected, not %s" % type(value).__name__)
TypeError: str expected, not bool

Next, I tried to up python2 application, this is lighttpd.conf:

server.modules              = (
        "mod_access",
        "mod_alias",
        "mod_accesslog",
        "mod_compress"
        )

server.port = 8081
server.document-root = "/python/metrics_interlayer/"
server.errorlog = "/python/metrics_interlayer/light_error.log"
accesslog.filename          =    "/python/metrics_interlayer/light_access.log"

server.modules += ("mod_fastcgi", "mod_rewrite")

fastcgi.server = ( "/code.py" =>
(
"python-fcgi" =>
(
  "socket" => "/tmp/fastcgi.python.socket",
  "bin-path" => "/python/metrics_interlayer/code.py",
  "max-procs" => 1
))
)

url.rewrite-once = (
 "^/(.*)$" => "/code.py/$1"
)

And my "app"(actually, it works well if I start it like: python code.py):

    #!/usr/bin/env python

    import web

    urls = ('/(.*)', 'Index')

    application = web.application(urls, globals())
    web.config.debug = True

    class Index:

        def GET(self, name=''):
            return 'Hello World'

        def POST(self, name=''):
            return 'Hello World'

    if __name__ == '__main__':application.run()

When I try to open any link, I don't get anything - just loading page and then get 500 error. Error logs doesn't show anything.

Should I start fastCGI previously or lighttpd must do it itself? I think this section (fastcgi.server) doesn't work (flup already installed)


Solution

  • Here's lines 33 and 34 out of web/wsgi.py which I just downloaded from http://webpy.org/:

    if (os.environ.has_key('PHP_FCGI_CHILDREN') #lighttpd fastcgi
      or os.environ.has_key('SERVER_SOFTWARE')):
    

    It differs from what your TypeError from above shows you. Maybe this was just a bug in an older version of webpy, which is now fixed? Try updating it.