Custom Search

Wednesday, January 2, 2013

How to explain Your First Pyramid Application

from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response

def hello_world(request):
    return Response('Hello %(name)s!' % request.matchdict)

if __name__ == '__main__':
    config = Configurator()
    config.add_route('hello', '/hello/{name}')
    config.add_view(hello_world, route_name='hello')
    app = config.make_wsgi_app()
    server = make_server('0.0.0.0', 8080, app)
    server.serve_forever()



########################################


from wsgiref.simple_server import make_server
from pyramid.config import Configurator
from pyramid.response import Response

"""
* http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/firstapp.html
* The script imports the Configurator class from the pyramid.config module. An instance of the Configurator class is later used to configure your Pyramid application.
* Like many other Python web frameworks, Pyramid uses the WSGI protocol to connect an application and a web server together.

"""

def hello_world(request):
    """
    * The function accepts a single argument (request) and it returns an instance of the pyramid.response.Response class.
    * This function is known as a view callable. A view callable accepts a single argument, request.
      It is expected to return a response object.
      A view callable doesn’t need to be a function; it can be represented via another type of object, like a class or an instance.
        * A view callable is always called with a request object.
        * A view callable is required to return a response object because a response object has
      all the information necessary to formulate an actual HTTP response; this object is
          then converted to text by the WSGI server which called Pyramid and it is sent back to the requesting browser.
    """  
    print "======request.matchdict========", request.matchdict ,"\n"
    return Response('Hello %(name)s!' % request.matchdict)

if __name__ == '__main__':
    """
       * Application Configuration
       * Methods called on the Configurator will cause registrations to be made in an
     application registry associated with the application.An application registry maps resource types to views,
     as well as housing other application-specific component registrations.
         Every Pyramid application has one (and only one) application registry.
       * In browser, type http://127.0.0.1:8080/hello/saju
         Here string "saju" will map to "name" and
         get in view via dictionary request.matchdict.
         eg:{'name': u'saju'}.
       * A call to make_wsgi_app implies that all configuration is finished (meaning all method calls to
     the configurator which set up views, and various other configuration settings have been performed).
         The make_wsgi_app method returns a WSGI application object that can be used by any WSGI server to
         present an application to a requestor. WSGI is a protocol that allows servers to talk to Python applications.    
    """  
    ###Configurator Construction###
    config = Configurator()
    ###Adding Configuration###
    ##registers a route to match any URL path that begins with /hello/ followed by a string.
    config.add_route('hello', '/hello/{name}')
    ##registers the hello_world function as a view callable and makes sure that it will be called when the hello route is matched.
    config.add_view(hello_world, route_name='hello')
    ##WSGI Application Creation###
    app = config.make_wsgi_app()
    ###WSGI Application Serving###
    server = make_server('0.0.0.0', 8080, app)
    print "===wsgiref Server Running==="
    server.serve_forever()
    print "===wsgiref Server Stopped==="

No comments:

Post a Comment