Package web :: Package web :: Module application' :: Class application
[hide private]
[frames] | no frames]

Class application

source code

Known Subclasses:

Application to delegate requests based on path.

>>> urls = ("/hello", "hello")
>>> app = application(urls, globals())
>>> class hello:
...     def GET(self): return "hello"
>>>
>>> app.request("/hello").data
'hello'
Instance Methods [hide private]
 
__init__(self, mapping=(), fvars={}, autoreload=None) source code
 
_cleanup(self) source code
 
_delegate(self, f, fvars, args=[]) source code
 
_delegate_sub_application(self, dir, app)
Deletes request to sub application `app` rooted at the directory `dir`.
source code
 
_load(self) source code
 
_match(self, mapping, value) source code
 
_unload(self) source code
 
add_mapping(self, pattern, classname) source code
 
add_processor(self, processor)
Adds a processor to the application.
source code
 
browser(self) source code
 
cgirun(self, *middleware)
Return a CGI handler.
source code
 
get_parent_app(self) source code
 
handle(self) source code
 
handle_with_processors(self) source code
 
internalerror(self)
Returns HTTPError with '500 internal error' message
source code
 
load(self, env)
Initializes ctx using env.
source code
 
notfound(self)
Returns HTTPError with '404 not found' message
source code
 
request(self, localpart='/', method='GET', data=None, host='0.0.0.0:8080', headers=None, https=False, **kw)
Makes request to this application for the specified path and method.
source code
 
run(self, *middleware)
Starts handling requests.
source code
 
wsgifunc(self, *middleware)
Returns a WSGI-compatible function for this application.
source code
Method Details [hide private]

_delegate_sub_application(self, dir, app)

source code 

Deletes request to sub application `app` rooted at the directory `dir`. The home, homepath, path and fullpath values in web.ctx are updated to mimic request to the subapp and are restored after it is handled.

@@Any issues with when used with yield?

add_processor(self, processor)

source code 

Adds a processor to the application.

>>> urls = ("/(.*)", "echo")
>>> app = application(urls, globals())
>>> class echo:
...     def GET(self, name): return name
...
>>>
>>> def hello(handler): return "hello, " +  handler()
...
>>> app.add_processor(hello)
>>> app.request("/web.py").data
'hello, web.py'

cgirun(self, *middleware)

source code 

Return a CGI handler. This is mostly useful with Google App Engine.
There you can just do:

    main = app.cgirun()

request(self, localpart='/', method='GET', data=None, host='0.0.0.0:8080', headers=None, https=False, **kw)

source code 

Makes request to this application for the specified path and method. Response will be a storage object with data, status and headers.

>>> urls = ("/hello", "hello")
>>> app = application(urls, globals())
>>> class hello:
...     def GET(self): 
...         web.header('Content-Type', 'text/plain')
...         return "hello"
...
>>> response = app.request("/hello")
>>> response.data
'hello'
>>> response.status
'200 OK'
>>> response.headers['Content-Type']
'text/plain'

To use https, use https=True.

>>> urls = ("/redirect", "redirect")
>>> app = application(urls, globals())
>>> class redirect:
...     def GET(self): raise web.seeother("/foo")
...
>>> response = app.request("/redirect")
>>> response.headers['Location']
'http://0.0.0.0:8080/foo'
>>> response = app.request("/redirect", https=True)
>>> response.headers['Location']
'https://0.0.0.0:8080/foo'

The headers argument specifies HTTP headers as a mapping object such as a dict.

>>> urls = ('/ua', 'uaprinter')
>>> class uaprinter:
...     def GET(self):
...         return 'your user-agent is ' + web.ctx.env['HTTP_USER_AGENT']
... 
>>> app = application(urls, globals())
>>> app.request('/ua', headers = {
...      'User-Agent': 'a small jumping bean/1.0 (compatible)'
... }).data
'your user-agent is a small jumping bean/1.0 (compatible)'

run(self, *middleware)

source code 

Starts handling requests. If called in a CGI or FastCGI context, it will follow that protocol. If called from the command line, it will start an HTTP server on the port named in the first command line argument, or, if there is no argument, on port 8080.

`middleware` is a list of WSGI middleware which is applied to the resulting WSGI function.