[Python] How to use Pyramid?


In this blog, i will tell you how to use Pyramid by codes.

'''
The start point is the Configurator class.Use it to configure the routes and views, 
give application back to wsgi server finally.
The class Configurator inherits many classes.It is a big mix and has great power.
'''
from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response

def hello(request):
    return Response("This is my small application")
def page(request):
    return Response("This is another page")
def main():
    config = Configurator()
    config.add_route('hello', '/')
    config.add_route('test','/test')
    config.add_route(name="page",pattern="/page",view=page)
    config.scan('view')
    config.add_view(view=hello,route_name="hello")
    
    app = config.make_wsgi_app()
    return app

if __name__ == '__main__':
    app = main()
    server = make_server('127.0.0.1', 8080, app)
    print ('Starting up server on http://localhost:8080')
    server.serve_forever()



评论关闭