AEWeb
Home Home Contact us Contact
home News Projects Tests Files Follow manatlan on Twitter Sign in
Last edition : 2009/03/28 14:29

AEWeb

AEWeb is not another python web framework ! It's just a wrapper around webapp, providing some facilities. It was designed to work on Google App Engine, but it works outside, if you provide webapp and webob. In the past, I used the marvelous webpy on GAE accounts. But webpy is going to be too big for my needs (bloatware ? it's not what I said). In fact, webapp is already pretty good, and it's based on the fabulous webob for its request and response objects. So everything I needed was here, and I had just make a lite wrapper (for syntaxic sugar).

Here is the simplest "hello world" :

from aeweb import Web

class MyApp(Web):
  def GET(self):
    yield "Hello World"

if __name__ == '__main__':
  Web.run([('/', MyApp)])

Note that restful methods are uppercase (like webpy), and they can act as generators too. In this case, return "Hello World" could be possible too. In fact, all what is returned or yielded is sent in the response body (like webpy).

Another thing I've stollen from webpy is the autodelegate concept :

from aeweb import Web

class MyApp(Web):
  def GET(self,path):
    return self.delegate("GET_",path)

  def GET_index(self):
    return "this is index page <a href='/hello'>go to hello</a>"

  def GET_hello(self):
    yield "this is hello page <a href='/'>go to index</a>"

if __name__ == '__main__':
  Web.run([('/(.*)', MyApp),])

Now, this app will reply on path '/' and '/hello'. Others will receive a 404 Not Found. Note that '/' go to GET_index (index is used when path is empty).

But the delegate method can do more : it can use input parameters (query and post parameters) as method arguments like this :

  def GET(self,path):
    return self.delegate("GET_",path,True)

  def GET_index(self,name="world"):
    return "hello %s !" % name

So, this app will display "hello world" for url "/", and "hello marc" for url "/?name=marc". Note that it will send back a 404 Not Found for "/?nom=marc" !

In fact, when it doesn't found the right "get method", it will call self.notfound(), which will send back the 404 http status, and the content of Web.page_notfound().

So, you should call self.notfound() if you want to send back a 404 Not Found on your own.

If you want to redefine this content, simply do that, before calling the run process:

Web.page_notfound = lambda w: "not found your %s" % w.url

Note that w is your Web instance. (Web.page_notfound() can receive one parameter)

The run process (aka Web.run() ), is in fact, a shortcut to run only one app.

This call :

Web.run( [('/(.*)', MyApp),] )

is the same as :

Web.app( [('/(.*)', MyApp),] ).run()

In fact : Web.app() return a WSGI app, which contains 2 methods :

... in progress ...

Web staticmethod api :

Web.app( routes, middlewares=None, debug=False ) : return a "wsgi app" with 2 new methods

Web.run( routes, middlewares=None, debug=False ) : shortcut method to Web.app().run()

... in progress ...

Web Instance api :

self.delegate( prefix, page, inputAsArgs=False )

self.is_dev

self.set_cookie()

self.del_cookie()

self.cookies

self.environ

self.url

self.path

self.header( name, value )

self.has_key( name )

self[ name ]

... in progress ...

Get aeweb.py

Download latest version

RSS Python Powered Get Ubuntu
©opyleft 2008-2019 - manatlan