📄 web.py
字号:
#
# $Workfile: Web.py $ $Revision: 15 $
# $Date: 10/04/01 5:10p $ $Author: Sholden $
#
import os, base64
import cgi
import mx.DateTime
import Cookie
import MapSite
from dbsource import dbsource
conn = dbsource().conn
cursor = conn.cursor()
class Page:
"""Implement common page behavior, with only the most basic look-and-feel.
This is the ultimate ancestor class for all dynamic pages."""
def __init__(self, op, path, query, session, headers,
oHeaders=None, Realm=None, Initpath=""):
"""Build the page to prepare for content generation."""
self.op = op
self.path = path
self.query = query
self.session = session
self.headers = headers
if oHeaders:
self.oheaders = oHeaders
else:
self.oheaders = []
self.realm = Realm
self.initpath = Initpath
def Generate(self):
auth = self.Authenticate()
if auth:
return auth
else:
return self.PageContent()
def Authenticate(self):
realm = self.Realm()
if not realm: # no authentication required
return
else: # verify authentication
try:
passwd = self.session._auth[realm]["password"]
# Already authenticated for the realm
return
except KeyError:
# Check authentication credentials in HTTP headers
auth = self.headers.getheader("authorization")
if auth:
auth = auth.split()
if auth[0].lower() == "basic": # username:password
auth = base64.decodestring(auth[1]).split(":")
self.session._auth[realm] = {"email": auth[0].lower()}
# Retrieve authentication data from realm table
a = MapSite.RealmData(realm)
if a: # Always fail if no data for the realm
stmt ="SELECT %s FROM %s WHERE %s = ?" % (a[2], a[0], a[1])
cursor.execute(stmt, (auth[0].lower(), ))
pw = cursor.fetchall()
if pw and pw[0][0].lower() == auth[1].lower():
self.session._auth[realm]["password"] = pw[0][0]
# This realm is authenticated for the first time
return
# No credential is present for this realm, so we require
# authentication. If the browser stops trying, the content
# we return instead of the page offers the password via
# email. Email is only sent to users in the database!
return Error401Page(self.op, self.path, self.query, self.session, self.headers,
oHeaders=["WWW-Authenticate: Basic realm=%s" % realm],
Realm=None).Generate()
def PageContent(self):
"""Return content when authentication not required or known good."""
content = \
"""<HTML>
<HEAD>
<TITLE>%s [Session %s]</TITLE>
</HEAD>
<BODY BGCOLOR="#ffffff">
<TABLE>
<tr>
<td VALIGN="TOP"><IMG SRC="/Image/py.gif"></td>
<td VALIGN="TOP"><H3><font color="red">
Web/Database Technology Demonstrator</font></H3></td>
</tr>
<tr><td colspan="2"><hr></td></tr>
<tr>
<td valign="TOP" width="80">%s</td>
<td valign="TOP" width="250">%s</td>
</tr>
</TABLE>
</BODY>
""" % (self.Title(), self.session._id, self.NavBar(), self.Body())
return \
"""HTTP/1.0 %d OK Content Follows
Content-Type: text/html
Content-Length: %d
Cache-Control: no-cache
%s
%s
%s""" % (self.ReturnCode(), len(content), self.Cookie(), self.Headers(), content)
def Body(self):
return "Error: Page subclass %s requires Body() method" % str(self.__class__)
def Cookie(self):
cookie = Cookie.SimpleCookie()
cookie["session"] = self.session._id
cookie["session"]["path"] = "/"
#cookie["session"]["expires"] = "31 Dec 2002" # Seems to make it permanent
return cookie
def NavBar(self):
return "Error: Page subclass %s requires NavBar() method" % str(self.__class__)
def Headers(self):
return "\r\n".join(self.oheaders)
def Realm(self):
"""Return the authentication realm, if any."""
return self.realm
def ReturnCode(self):
return 200
def Title(self):
return "Error: Page subclass %s requires Title() method" % str(self.__class__)
class Error401Page(Page):
"""Error 401 is directly declared here to avoid circular imports."""
def Body(self):
return \
"""<P><FONT COLOR="RED" SIZE="+1"><B>Authentication Required</B></FONT></P>
<P>If you would like us to remind you of your password by
email please <A HREF="/MailPass/%s">click here</A>
""" % self.Realm()
def Message(self):
return "Authentication Required"
def NavBar(self):
return """<A HREF="/">HOME</A>"""
def ReturnCode(self):
return 401
def Title(self):
return "Authentication Required"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -