#!/usr/bin/env python # Copyright (c) 2001-2004 Twisted Matrix Laboratories. # See LICENSE for details. """I am a simple test resource. """ import os.path import cgi as pycgi from twisted.web2 import log, iweb from twisted.web2 import static, wsgi, resource, responsecode, twcgi from twisted.web2 import resource, stream, http, http_headers from twisted.internet import reactor ### A demo WSGI application. def simple_wsgi_app(environ, start_response): status = '200 OK' response_headers = [('Content-type','text/html; charset=ISO-8859-1')] start_response(status, response_headers) data = environ['wsgi.input'].read() environ['wsgi.errors'].write("This is an example wsgi error message\n") s = '
'
items=environ.items()
items.sort()
for k,v in items:
s += repr(k)+': '+repr(v)+'\n'
return [s, '', data, '']
### Demonstrate a simple resource which renders slowly.
class Sleepy(resource.Resource):
def render(self, req):
# Create a stream object which can be written in pieces.
s=stream.ProducerStream()
# Write a string, and then, later, write another string, and
# call it done. (Also write spaces so browsers don't wait
# before displaying anything at all)
s.write("Hello\n")
s.write(' '*10000+'\n')
reactor.callLater(1, s.write, "World!\n")
reactor.callLater(2, s.finish)
# Return a response. Use the default response code of OK, and
# the default headers
return http.Response(stream=s)
### Form posting
class FormPost(resource.PostableResource):
def render(self, req):
return http.Response(responsecode.OK,
{'content-type': http_headers.MimeType('text', 'html')},
"""
Form1, x-www-form-urlencoded:
Form2, multipart/form-data:
Arg dict: %r, Files: %r""" % (req.args, req.files)) ### Toplevel resource. This is a more normal resource. class Toplevel(resource.Resource): # addSlash=True to make sure it's treated as a directory-like resource addSlash=True # Render the resource. Here the stream is a string, which will get # adapted to a MemoryStream object. def render(self, req): contents = """