using "request" variable in python web program

S

sami

Hi

I am trying to write a facebook application in python - I have been
programming simple desktop applications till now and am not really
familiar with web apps

Pyfacebook is the wrapper for the REST based Facebook API - there is a
simple example for its usage as shown below:

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

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?

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
 
P

Paul Boddie

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.
 
S

sami

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).
Thanks a ton Paul for the information. I am using CGI and my host
(nearlyfreespeech.net) does not have django hosting - and being mainly
a C/dekstop apps programmer I really don't want to learn the whole MVC
concept and its implementation (and quirks) in django - I want to use
abstractions only when I feel the need for them

Since a django user made pyfacebook, hopefully someone on the forum
will help out

Thanks again

Sami
 
P

Paul Boddie

Thanks a ton Paul for the information. I am using CGI and my host
(nearlyfreespeech.net) does not have django hosting - and being mainly
a C/dekstop apps programmer I really don't want to learn the whole MVC
concept and its implementation (and quirks) in django - I want to use
abstractions only when I feel the need for them

See the library reference for information on how to get the value of
form/request parameters from the CGI environment:

http://docs.python.org/lib/node561.html

Paul
 
S

sami

See the library reference for information on how to get the value of
form/request parameters from the CGI environment:

http://docs.python.org/lib/node561.html

Paul

Thanks again Paul - now I can see that the "request" object is a
string of name/value pairs that occurs after a "?" in a url e.g.
url.com/index.cgi?name=sami&lang=python - is that correct? Googling
for this information is useless since the word "request" is so common
on the www
 
P

Paul Boddie

Thanks again Paul - now I can see that the "request" object is a
string of name/value pairs that occurs after a "?" in a url e.g.
url.com/index.cgi?name=sami&lang=python - is that correct? Googling
for this information is useless since the word "request" is so common
on the www

The string after "?" in a URL is actually the "query string" and is
typically exposed as the QUERY_STRING environment variable in CGI. See
here for more specific details:

http://www.w3.org/CGI/
http://cgi-spec.golux.com/

What the cgi module provides is some conveniences for automatically
decoding the query string - perhaps not *so* difficult, you might
think, even taking the decoding of encoded values into account (such
as things like "%20" that you find in URLs) - but the cgi module also
provides facilities to decode request parameters that are supplied in
the body of a request (such as those provided in POST requests), and
that can be a more difficult exercise.

Paul
 
G

Gabriel Genellina

Thanks again Paul - now I can see that the "request" object is a
string of name/value pairs that occurs after a "?" in a url e.g.
url.com/index.cgi?name=sami&lang=python - is that correct? Googling
for this information is useless since the word "request" is so common
on the www

Not exactly - this is the "query string" part of the URI.
Request and Response are the two messages defined by the HTTP protocol.
When you type a URL or click on a link or press a button in a page, your
browser builds the appropiate Request message and sends it to the server.
After processing, the server emits the Response message, and the browser
displays it or otherwise processes the response.
The request and response objects that most web frameworks expose are
abstractions of these two HTTP messages.
The bloody details are specified in RFC 2616 HTTP/1.1
<http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html>, but you can find
plenty of less technical descriptions. At least a basic understanding of
how the HTTP protocol works is required to build a web application.
 
S

sami

Not exactly - this is the "query string" part of the URI.
Request and Response are the two messages defined by the HTTP protocol.
When you type a URL or click on a link or press a button in a page, your
browser builds the appropiate Request message and sends it to the server.
After processing, the server emits the Response message, and the browser
displays it or otherwise processes the response.

Thanks Paul and Gabriel - I am confused I guess - I do know about the
request/response mechanism, I wrote this app a while ago:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/522983 - I
wrote this app before in C and decoded request/response params using a
sniffer - I ported it to Python since I wanted to learn Python
The string after "?" in a URL is actually the "query string" and is
typically exposed as the QUERY_STRING environment variable in CGI. See
here for more specific details:

This is what is mixing me up - an example given in the source for
Pyfacebook - http://pyfacebook.googlecode.com/svn/trunk/examples/examples.py

def simple_web_app(request, api_key, secret_key):
fb = Facebook(api_key, secret_key, request.GET['auth_token'])
fb.auth.getSession()

It seemed to me "request" was a key in a key/value pair string/
dictionary

Anyway, I have the "auth_token" now and I can pass these 3 string
values to as Facebook(<api_key>, <secret_key>, <auth_token>) - and
it's moving along - but I am persevering :) no PHP for me - I hope I
can put up a tut for this afterwards

Thanks again for the help, guys
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top