Download Current Version: swi.py (2005-08-06)

Simple Web Interface is a fast way to create Python programs that serve dynamic web pages, without all that tedious mucking about with CGI scripts. You simply define a class, and the methods in that class will be called to create the various web pages. The program itself runs as a web server. The tool is flexible enough to be used as a simple user interface system, too.

An HTTP request for http://server/page?a=val1&b=val2 will result in a call to

  def swi_page(self,a,b):

and a will get the value val1 and b will be val2. You can also define defaults for these arguments, and it will use those if they are unspecified. This method will also be called for a request like this: http://server/page/val1/val2

There is no limitation on the number of arguments. The system supports both GET and POST (and multipart POST, so file uploading will work).

The system also supports a cookie-based login system. You can define users with swi.addUser(name,password). You can then check for logins by adding this to the beginning of your methods:

    if self.user==None:
return 'Please log in

'+self.createLoginForm()

You can log out by causing a call to self.logOut().

Here is an extended example (which is included in the source code):

class Demo(swi.SimpleWebInterface):
# http://localhost:8080/
def swi(self):
return 'Hello world!'

# http://localhost:8080/test1?a=data
# http://localhost:8080/test1/data
def swi_test1(self,a='value'):
return 'The value of a is "%s"'%a

# http://localhost:8080/test2?a=data&b=data2
# http://localhost:8080/test2/data/data2
# http://localhost:8080/test2?b=data2
def swi_test2(self,a='value',b='value'):
return 'The value of a is "%s" and b is "%s"'%(a,b)

# http://localhost:8080/page
def swi_page(self):
if self.user==None:
return self.createLoginForm()
return '''
Hello, %s.

Click here to go to another page.

Click here to log out.
'''%self.user

# http://localhost:8080/otherPage
def swi_otherPage(self):
if self.user==None:
return self.createLoginForm()
return '''
You are now at the other page, %s.

Click here to go back to the first page.

Click here to log out.
'''%self.user

# http://localhost:8080/logout
def swi_logout(self):
self.logOut()
return '''
You are now logged out.

Click here to the first page.

Click here to go to another page.
'''
swi.addUser('terry','password')
swi.start(Demo,8080)

With this example, the pages /page and /otherPage are both protected so that the user must be logged in.

History