user authentication interface in python

Gerhard Häring gh at ghaering.de
Tue Jun 17 13:35:33 EDT 2003


scn wrote:
> Gerhard Häring <gh at ghaering.de> 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 'configure.inc' 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 hello.py ----
#!/usr/bin/env python
# A very simple CGI script in Python
import motd

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

--- 8< ----- file motd.py -----
# 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 hello.py, 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