using "request" variable in python web program

Paul Boddie paul at boddie.org.uk
Sat Oct 20 20:01:16 EDT 2007


On 21 Okt, 00:21, sami <sami.... at gmail.com> wrote:
>
> def simple_web_app(request, api_key, secret_key):
>     fb = Facebook(api_key, secret_key, request.GET['auth_token'])
>     fb.auth_getSession()
>
> A Django-based tutorial and source is available here:http://wiki.developers.facebook.com/index.php/PythonPyFacebookTutorial
>
> I really don't want to use a framework for the time being and just
> want an app up and running

Something like CGI, mod_python or Python's standard library
BaseHTTPServer might be good enough. Or you could try WebStack if you
can't decide. See these links for details:

http://wiki.python.org/moin/CgiScripts
http://www.modpython.org/
http://wiki.python.org/moin/BaseHttpServer
http://www.python.org/pypi/WebStack

> My question is - how do you create the "request" object which will be
> passed into the simple_web_app(request, api_key, secret_key) function
> here - is there somewhere I can read into it?

The request object in this case is specifically a Django request
object; this can be deduced from usage of the request.GET attribute
which is specific to Django. If you decided to use another framework,
you'd need to rewrite this part; for example in WebStack...

class FacebookResource:
    def respond(self, trans):
        try:
            auth_token = trans.get_fields_from_path()['auth_token'][0]
            fb = Facebook(api_key, secret_key, auth_token)
            fb.auth_getSession()
            ...
        except KeyError: # no auth token
            ...

I'm sure others can suggest their own preferred solutions.

> I have been googling for hours and there seems to be no simple,
> straightforward example for this - PHP rules when it comes to simple
> web programming apps - either I am not looking at the right places but
> it's very hard to get up and running quickly for web apps in Python -
> maybe DiveIntoPython needs a section devoted for web programming

There's only consensus around WSGI as some kind of standard in Python
Web programming, but it isn't likely to get you very far without
forcing you to choose extra components to make the work bearable (eg.
to read query/form values), so it isn't the one way of doing things at
the level that PHP presumably offers (which is why it's presumably
easier to find advice on such matters all over the Web for PHP).

Most people advocate particular frameworks instead of WSGI, and as
you've noticed people do seem to like Django quite a lot. The problem
with this state of affairs is that you need to make "up front" choices
when testing out frameworks, and after the legwork of setting stuff
up, if you don't like what you've seen you've got to back out and do
similar (but different) stuff for your next choice of framework.

However, it shouldn't be too bad to set Django up, really, especially
if people have taken some time to explain something very close to what
you want to do using Django. Moreover, there's a Google group (django-
users) where you could ask general questions, and I imagine that
people will be quite happy to help you out. But I can totally
understand that creating databases and installing various packages
seems somewhat peripheral to a task which could arguably be done using
a CGI script (as far as I can see).

Paul

P.S. I'm not too impressed by the lack of a common high-level Web API
for Python, either. However, I've mostly ignored the discussions and
stuck with my own API (WebStack) in the knowledge that if it doesn't
work for me, at least I know how I might fix it.




More information about the Python-list mailing list