Bas
Bas

Reputation: 1076

mod wsgi using apache and python

I have used mod_wsgi to create a web server that can be called locally. Now I just found out I need to change it so it runs through the Apache server. I'm hoping to do this without rewriting my whole script.

from wsgiref.simple_server import make_server



class FileUploadApp(object):
    firstcult = ""

    def __init__(self, root):
        self.root = root

    def __call__(self, environ, start_response):

        if environ['REQUEST_METHOD'] == 'POST':
            post = cgi.FieldStorage(
                fp=environ['wsgi.input'],

                environ=environ,
                keep_blank_values=True
            )

        body = u"""
            <html><body>

            <head><title>title</title></head>
            <h3>text</h3>
            <form enctype="multipart/form-data" action="http://localhost:8088" method="post">
            </body></html>
            """


        return self.__bodyreturn(environ, start_response,body)

    def __bodyreturn(self, environ, start_response,body):
        start_response(
                    '200 OK',
                    [
                        ('Content-type', 'text/html; charset=utf8'),
                        ('Content-Length', str(len(body))),
                    ]
                )        
        return [body.encode('utf8')]

def main():
    PORT = 8080
    print "port:", PORT
    ROOT = "/home/user/"
    httpd = make_server('', PORT, FileUploadApp(ROOT))
    print "Serving HTTP on port %s..."%(PORT)
    httpd.serve_forever() # Respond to requests until process is killed

if __name__ == "__main__":
    main()

I am hoping to find a way to make it possible to avoid making the server and making it possible to run multiple instances of my script.

Upvotes: 0

Views: 139

Answers (1)

Graham Dumpleton
Graham Dumpleton

Reputation: 58523

The documentation at:

explains what mod_wsgi is expecting to be given.

If you also read:

you will learn about the various ways that WSGI application entry points can be constructed.

From that you should identify that FileUploadApp fits one of the described ways of defining a WSGI application and thus you only need satisfy the requirement that mod_wsgi has of the WSGI application object being accessible as 'application'.

Upvotes: 2

Related Questions