Quick start (to use webapp2 outside of App Engine)

webapp2 can also be used outside of App Engine as a general purpose web framework, as it has these features:

  • It is independent of the App Engine SDK. If the SDK is not found, it sets fallbacks to be used outside of App Engine.
  • It supports threaded environments through the module Local.
  • All webapp2_extras modules are designed to be thread-safe.
  • It is compatible with WebOb 1.0 and superior, which fixes several bugs found in the version bundled with the SDK (which is of course supported as well).

It won’t support App Engine services, but if you like webapp, why not use it in other servers as well? Here we’ll describe how to do this.

Note

If you want to use webapp2 on App Engine, read the Quick start tutorial instead.

Prerequisites

If you don’t have a package installer in your system yet (like pip or easy_install), install one. See Installing packages.

If you don’t have virtualenv installed in your system yet, install it. See Installing virtualenv.

Create a directory for your app

Create a directory hellowebapp2 for your new app. It is where you will setup the environment and create your application.

Install WebOb, Paste and webapp2

We need three libraries to use webapp2: WebOb, for Request and Response objects, Paste, for the development server, and webapp2 itself.

Type this to install them using the active virtual environment (see Installing virtualenv):

$ pip install WebOb
$ pip install Paste
$ pip install webapp2

Or, using easy_install:

$ easy_install WebOb
$ easy_install Paste
$ easy_install webapp2

Now the environment is ready for your first app.

Hello, webapp2!

Create a file main.py inside your hellowebapp2 directory and define a handler to display a ‘Hello, webapp2!’ message. This will be our bootstrap file:

import webapp2

class HelloWebapp2(webapp2.RequestHandler):
    def get(self):
        self.response.write('Hello, webapp2!')

app = webapp2.WSGIApplication([
    ('/', HelloWebapp2),
], debug=True)

def main():
    from paste import httpserver
    httpserver.serve(app, host='127.0.0.1', port='8080')

if __name__ == '__main__':
    main()

Test your app

Now start the development server using the Python executable provided by virtualenv:

$ python main.py

The web server is now running, listening for requests on port 8080. You can test the application by visiting the following URL in your web browser: