4

I want to know how to show requests ($_GET) on a python script ?

Note that i am using mod_python as basic web server running with apache2 on UBUNTU server.

If you have any idea your help will be very appreciated :)

Other python web server like Django etc ... note needed in my case because i am running only one python script for a specific task :)

9
  • 2
    Are you using any web frameworks or are you drinking from the WSGI hosepipe directly? Commented Sep 11, 2014 at 9:18
  • No i don't use any framework :) basic python web server :) Commented Sep 11, 2014 at 9:18
  • 3
    Then start using one now. There is little reason to stay at that low level. Pick one from wiki.python.org/moin/WebFrameworks, although Flask, Pyramid and Django are most popular right now depending on your needs. Commented Sep 11, 2014 at 9:20
  • 1
    For a specific, simple task without a database backend use Flask. Commented Sep 11, 2014 at 9:21
  • 1
    Sure there is, but why go through that pain at all? If you insist, at least read a WSGI tutorial. Commented Sep 11, 2014 at 9:22

1 Answer 1

9

For a simple script to respond to a web request, I'd not use mod_python; you probably want to switch to mod_wsgi instead here.

You'll have to parse the QUERY_STRING WSGI variable yourself; you can do so with the urlparse.parse_qs() function:

from urlparse import parse_qs

def application(environ, start_response):
    params = parse_qs(environ['QUERY_STRING'])
    foo = params.get('foo', [None])[0]

parse_qs parses out the query string and sets the value in the resulting dictionary to a list per key, because keys can occur more than once in a query string.

The above will pick out the first value in the list, or None if the key isn't present at all.

If you are going to stick to plain-vanilla WSGI, read a decent tutorial to understand how it works. I strongly recommend you use a framework however; even just Werkzeug can make your life as a developer vastly simpler, even for a simple script.

If you are going to stick with a mod_python request handler, replace environ['QUERY_STRING'] with request.args():

from urlparse import parse_qs

def requesthandler(req):
    params = parse_qs(req.args)
    foo = params.get('foo', [None])[0]
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you, and how to call def application 'foo' from an external string ?
@user3818090: ick, you are using mod_python, not mod_wsgi. That's rather ancient and outmoded these days..
I just installed mod_wsgi like you suggested me !
@user3818090: ah, then I'll roll back to the other revision. mod_wsgi is certainly the better choice unless you want to write a Apache filter or connection handler.
@user3818090: my mistake, I meant to use req (the argument to the handler).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.