user authentication interface in python

Gerhard Häring gh at
Tue Jun 17 19:35:33 CEST 2003

scn wrote:
> Gerhard Häring <gh at> wrote:
>>scn wrote:
>>>hello.  does anyone have a high-level example of how to implement
>>>'secure' user authentication interface for web users in python.
>>>i am in the process of designing a simple web application that
>>>requires user authentication and i remembered that php typically held
>>>system authentication information in a separate file and included this
>>>file in the main interface script.
>>What's the advantage of this approach? [...]
>>Sure, you can
>>a) import a module from elsewhere [...]
> thanks for responding. what i wanted to achieve was keeping the
> database access information in a separate file, similar to the
> convention i've seen used in php.  the db connection, username and
> password information was typically stored in an '' file
> and included in the main script using the 'include' statement. 

Ah! Now I get it. Well, I don't know PHP but it seems it always 
interprets a *single* file. So to access any PHP code from outside this 
file, you include it. But it's still a single file that's processed by 
the PHP interpreter, it's just that another file was inserted at a 
placeholder position.

Well, Python is different :)

Python has the concept of 'modules', which are independent of each 
other. To access functions, classes or data from a different module, you 
  'import' the module.

Here's a simple example:

--- 8< ----- file ----
#!/usr/bin/env python
# A very simple CGI script in Python
import motd

# HTTP headers:
print "Content-type: text/plain"
print "The message of the day is:"
print motd.get_motd()
--- >8 -------------------------

--- 8< ----- file -----
# The motd (Message Of The Day) module

# It contains one function:
def get_motd():
     return "Just for testing ..."
--- >8 -------------------------

You see: two files - two Python modules. Module, the main 
module, which will also be the script that's started from the web 
server, imports the motd module and calls the function get_motd() with it.

I hope this makes the Python way clearer.

> [...]

-- Gerhard

More information about the Python-list mailing list