欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

[Python] How to use Pyramid?

程序员文章站 2022-03-30 12:02:02
in this blog, i will tell you how to use pyramid by codes. ''' the start point is the conf...

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 https://localhost:8080')
    server.serve_forever()