import cgi,os,sys,time import types from snakeserver.snakelet import Snakelet class OldMemory(Snakelet): """previous memory snakelet, now returning a 410 gone""" def serve(self, request, response): response.sendError(410) class Memory(Snakelet): """Show memory usage""" sessionType=Snakelet.SESSION_WANTED def serve(self, request, response): import gc prev = int(request.getParameter("previous",0)) now= len(gc.get_objects()) delta = now-prev out = response.getOutput() print >>out, '' print >>out,'Memory usage' print >>out, "

'Memory' Snakelet

" print >>out, "

measured at "+time.strftime("%c")+"

" print >>out, "

Python memory usage: %d objects (delta = %d)

" % (now, delta) print >>out, "

Measure again

" % now print >>out, "

Measure again (with ypage)

" % now print >>out, "" class Included(Snakelet): """outputs stuff for the include test""" sessionType=Snakelet.SESSION_NOT_NEEDED def serve(self, request, response): response.encoding="UTF-8" out = response.getOutput() # print >>out,"This is the Included Snakelet! Euro sign: "+u"\u20ac" out.write("This is the Included Snakelet! Euro sign: "+u"\u20ac") class Encoding(Snakelet): """Test for char encodings""" sessionType=Snakelet.SESSION_NOT_NEEDED def serve(self, request, response): encoding=request.getParameter("encoding") or "UTF-8" response.encoding=encoding response.content_type="text/html" out = response.getOutput() out.write('') out.write("%s\n" % encoding) out.write("

A page with %s character-encoding

\n" % encoding) out.write("Text below generated by snakelet\n") out.write("

Showing different kind of symbols ") out.write(u"(\N{COPYRIGHT SIGN}, \N{GREEK SMALL LETTER BETA}, \N{GREEK SMALL LETTER PI}, \N{EURO SIGN})\n") out.write("
Accented letters ") out.write(u"(like \N{LATIN CAPITAL LETTER C WITH CEDILLA} and \N{LATIN CAPITAL LETTER U WITH CIRCUMFLEX}), ") out.write("Greek letters ") out.write(u"(\N{GREEK SMALL LETTER ALPHA} \N{GREEK SMALL LETTER BETA} \N{GREEK SMALL LETTER GAMMA}), ") out.write("Arabic letters ") out.write(u"(\N{ARABIC LETTER SHEEN} \N{ARABIC LETTER GHAIN} \N{ARABIC LETTER YEH}), ") out.write("Cyrillic letters ") out.write(u"(\N{CYRILLIC CAPITAL LETTER BE} \N{CYRILLIC CAPITAL LETTER EF} \N{CYRILLIC CAPITAL LETTER YA}), ") out.write("Hebrew ") out.write(u"(\N{HEBREW LETTER ALEF} \N{HEBREW LETTER BET} \N{HEBREW LETTER GIMEL})\n") out.write("
And much more...") out.write("
This should be an Euro: "+unichr(0x20ac)) out.write("


The page you're looking at is a snakelet.") out.write("
You can also try out an Ypage.\n") out.write('

View this page using: ') out.write(' UTF-7 ') out.write(' UTF-8 ') out.write(' UTF-16 ') out.write(' UTF-16BE ') out.write(' UTF-16LE ') out.write(' escaped unicode ') out.write("") class Redirecter(Snakelet): sessionType=Snakelet.SESSION_NOT_NEEDED def serve(self, request, response): response.encoding="UTF-8" form = request.getForm() arg = request.arg if arg=='redir': url=form['url'] url+="?password=foobar" request.context.message="This is the message put into the request by the snakelet" self.redirect(url, request, response) elif arg=='httpredir': url=form['url'] url+="?password=foobar" response.HTTPredirect(url) elif arg=='include': url=form['url'] out=response.getOutput() print >>out, '' print >>out,'Inclusion

Include test

The text in the yellow box below is included from elsewhere:' print >>out,'Please note that any links or images in the included page are not translated, and will likely be wrong.
' print >>out,'
' request.context.message="This is the message put into the request by the snakelet" self.include(url, request, response) print >>out,'
Here it is a second time: ' print >>out,'
' self.include(url, request, response) print >>out,'
' print >>out,'
This ends the include example.' else: out=response.getOutput() import urllib snoopUrl=urllib.quote("/test/snoop.sn?newarg=newvalue&morearg=morevalue") print >>out, '' print >>out,'Redirection

Redirect snakelet

' print >>out,'

Internal redirect to Ypage. Fancy redirect url stays in location bar' print >>out,'

Internal redirect to html. Fancy redirect url stays in location bar' print >>out,'

Internal redirect to Snoop page.' print >>out,'

Internal redirect to external link. Fancy url stays in location bar.' print >>out,'

HTTP (browser) redirect to Ypage. Destination url will be in location bar' print >>out,'

HTTP (browser) redirect to html. Destination url will be in location bar' print >>out,'

Include Ypage' print >>out,'

Include html' print >>out,'

Include directory listing' print >>out,'

Include external link' print >>out,'' class Error(Snakelet): def init(self): pass sessionType=Snakelet.SESSION_NOT_NEEDED def serve(self, request, response): if request.getParameter("custom"): self.errorPage="errorpage.y" # we will simply use the custom error Ypage for output # you may not call getOutput(), otherwise # the redirection to the error page fails! else: self.errorPage=None out=response.getOutput() out.write("This snakelet will generate an error. ") out.write("And it uses the default error page.") a=1 b=0 c=a/b # BOOM !!! ;-) response.getOutput().write("never reached!") class FakeIndex(Snakelet): def init(self): pass sessionType=Snakelet.SESSION_NOT_NEEDED def serve(self, request, response): out=response.getOutput() out.write("""

Fake index page

This page is the index page of a 'fake' directory.

The directory you requested in the url does not actually exist, instead the index page is served by a special index-snakelet. """) class HTTPAuthenticator_management(Snakelet): sessionType=Snakelet.SESSION_LOGIN_REQUIRED # make sure a user is logged in authMethod=("httpbasic","Management") # ... using http basic authentication. # the ypage equivalent is <%@authmethod=httpbasic%> def serve(self, request, response): out = response.getOutput() user=request.session.user print >>out, '' print >>out,'Made it!

You made it!

' if user: print >>out,"

Authorized user: "+user.userid+"" print >>out,"

privileges: "+self.escape(str(user.privileges))+"" else: print >>out,"

Strange, there is no logged in user??" print >>out,"" class HTTPAuthenticator_backoffice(HTTPAuthenticator_management): authMethod=("httpbasic","Backoffice") # ... realm = Backoffice