deploying a WSGI application on mod_python

杀马特。学长 韩版系。学妹 提交于 2019-12-05 11:12:46

You can use mod_python and mod_wsgi together so long as same Python version and mod_python not linked against a static Python library.

Run the 'ldd' command on the mod_python.so file:

ldd mod_python.so

to find out if it links to libpythonX.Y.so. Build mod_wsgi to use same Python version, ensuring it is similarly linked against same libpythonX.Y.so.


UPDATE

Version 4.X of mod_wsgi now explicitly refuses to start if mod_python is also loaded. In order for mod_python and mod_wsgi to be used together, certain features of mod_wsgi had to be crippled. As mod_python is now very old, not meaningfully updated, has various problems with it and should not be used for anything new, no longer trying to support them being used together.

Here's an idea (that needs fleshing out, and that maybe won't work):

Here's what I would start with:

from mod_python import apache
from wsgiref.handlers import BaseHandler

class MyWSGIHandler(BaseHandler):
    def __init__(self, apachereq):
        BaseHandler.__init__(self)
        self.apachereq = apachereq

    def _write(self, data):
        self.apachereq.write(data)

    # override the other required methods of BaseHandler, see
    # http://docs.python.org/library/wsgiref.html#wsgiref.handlers.BaseHandler

wsgi_app = create_your_wsgi_app()

def handler(req):
    wsgi_handler = MyWSGIHandler(req)
    wsgi_handler.run(wsgi_app)
    return apache.OK

IDEA 2 (pretty hackish):

You could in your handler code also use the werkzeug wsgi testing module to pass a request to the WSGI app, get a werkzeug response back and then write that response to apache.

Something like:

from mod_python import apache
from werkzeug.test import Client
from werkzeug.wrappers import BaseResponse

wsgi_app = create_your_wsgi_app()

def handler(req):
    c = Client(wsgi_app, BaseResponse)
    resp = c.get(somehow_get_the_url_from(req)) # or c.post if it's a POST request
    req.write(resp.data) # ... and find a way to write the headers as well
    return apache.OK

The best solution might be to use mod_proxy and run the Python web app in a different webserver.

After some googling, I found this:

How to set up mod_python for use as a WSGI server

The following now works using Apache2, with mpm_winnt and prefork! Other versions and MPM's may give radically different results. If you've got a little time and experience, please try this latest version with prefork on Linux, and any other Multi-Processing Modules you favor.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!