[Tutor] CGI script: get the client user

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Jan 10 11:22:32 CET 2007



On Wed, 10 Jan 2007, paulino1 at sapo.pt wrote:

> I have some python CGI scripts running in an intranet.
>
> I would like to get the user of the client browser to use it as an input 
> in the script, so it can show the the data related to that user.
>
> There is the function getpass.getuser() that would return the user 
> logged in the server.


But people are supposed to be anonymous on the Internet, right?  *wink* I 
suspect that's less true on an Intranet.  Still, from the perspective of 
the server, you only know as much as the client is willing to share with 
you.  Login information isn't usually a part of that communication, so 
you'll need to make it explicitely so.


One thing you can do is ask the user explicitely for their authentication 
information, and save that as a "cookie" or some other session mechanism.

Think of a web-email application, such as Google Mail.  If it's the first 
time you visit a site like gmail, it begins a new session and asks for 
your login information.  I'd suspect that they implement sessions with 
cookies.  If so, then a cookie is saved on your web browser, and on 
subsequent interactions with the system, your web browser will pass those 
cookies off for the server to look at, in order to maintain the illusion 
of the system remembering who you are.

For examples and documentation of cookies, see:

     http://hkn.eecs.berkeley.edu/~dyoo/python/cookiecounter.py
     http://docs.python.org/lib/module-Cookie.html


However, doing the work necessary to set up authentication can be tedious 
code: people have done this stuff already.  Have you looked into a web 
development framework such as Django or web.py?  Both provide session 
support that you should be able to reuse.


More information about the Tutor mailing list