How to connect a webpage with python to mod_wsgi?
Question: I am a total newbie when it comes to Python and incorporating it within html.
I have spent many hours researching with no luck, so all I ask is a little
help or an area where I can find the information required.

I'm using AMPPS to develop a website as this has mod_wsgi pre-installed. I am
creating a function where the user can input a Python script within a text
area E.G.

    
    
    > print 'Hello World'
    

The user pushes an execute button and then the results is displayed in a page.
This I assume is sent using a html and a Python script and passed server side
so mod_wsgi can decode python and pass it back.

    
    
    Hello World
    

The question is what commands or python commands are required to send a script
that includes 'print hello world' and then connects with mod_wsgi server side?

And once connected how is the resonse displayed?

Does AMPPS need any configuration to allow mod_wsgi to work correctly (read a
thread about configuring the httpd.conf file)?

I'm sorry for all the questions, Im just totally stumped atm and my Python
book is going way over my head.

Kind regards Andy


Answer: The easiest wsgi work I've ever done was using [web.py](http://webpy.org/).

[Here](http://webpy.org/cookbook/mod_wsgi-apache) are the apache mod_wsgi
setup docs.

You'll need a class with a GET method that renders html with an input form
which will take the script then POST it back to a POST method (on the same
class, if you wish). The response from your post method will be whatever
result you get from calling eval on the python script. Note: this is a very
dangerous application as the user can now run unvalidated python scripts on
your server as whatever user the server process runs as.

Example code:

    
    
    import web
    
    class Echo:
        def GET(self):
            return """<html><form name="script-input-form" action="/" method="post">
    Script: <input type="text" name="script-body">
    <input type="submit" value="Submit">
    </form><html>"""
    
        def POST(self):
            data = web.input()
            obj = compile(data['script-body'], '', 'exec')
            result = eval(obj, globals(), locals())
            web.seeother('/')
    
    urls = (
      '/.*', Echo,
    )
    if __name__ == "__main__": 
        app = web.application(urls, globals())
        app.run()
    else:
        app = web.application(urls, globals()).wsgifunc()
    

I must also comment, if you are just learning programming, just learning
python, and have not done any apache configuration, you're biting off a lot at
the same time. You may want to learn these items in more digestible portions.

