Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
pyramid.pdf
Скачиваний:
10
Добавлен:
24.03.2015
Размер:
3.82 Mб
Скачать

4.2. REFERENCES

After configuring views and ending configuration, the script creates a WSGI application via the pyramid.config.Configurator.make_wsgi_app() method. 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. We don’t discuss WSGI in any depth within this book, however, you can learn more about it by visiting wsgi.org.

The Pyramid application object, in particular, is an instance of a class representing a Pyramid router. It has a reference to the application registry which resulted from method calls to the configurator used to configure it. The router consults the registry to obey the policy choices made by a single application. These policy choices were informed by method calls to the Configurator made earlier; in our case, the only policy choices made were implied by calls to its add_view and add_route methods.

4.1.7 WSGI Application Serving

1server = make_server(’0.0.0.0’, 8080, app)

Finally, we actually serve the application to requestors by starting up a WSGI server. We happen to use the wsgiref make_server server maker for this purpose. We pass in as the first argument ’0.0.0.0’, which means “listen on all TCP interfaces.” By default, the HTTP server listens only on the 127.0.0.1 interface, which is problematic if you’re running the server on a remote system and you wish to access it with a web browser from a local system. We also specify a TCP port number to listen on, which is 8080, passing it as the second argument. The final argument is the app object (a router), which is the the application we wish to serve. Finally, we call the server’s serve_forever method, which starts the main loop in which it will wait for requests from the outside world.

When this line is invoked, it causes the server to start listening on TCP port 8080. The server will serve requests forever, or at least until we stop it by killing the process which runs it (usually by pressing Ctrl-C or Ctrl-Break in the terminal we used to start it).

4.1.8 Conclusion

Our hello world application is one of the simplest possible Pyramid applications, configured “imperatively”. We can see that it’s configured imperatively because the full power of Python is available to us as we perform configuration tasks.

4.2 References

For more information about the API of a Configurator object, see Configurator .

For more information about view configuration, see View Configuration.

35

4. CREATING YOUR FIRST PYRAMID APPLICATION

36

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]