ウェブアプリケーション

強力なスクリプト言語は早くプロトタイプを作成することと大きなプロジェクトの両方に対応していて、 Pythonはウェブアプリケーションの開発で広く使われています。

コンテキスト

WSGI

Web Server Gateway Interface(省略して”WSGI”)は、 ウェブサーバーとPythonウェブアプリケーションフレームワーク間の標準的なインターフェースです。 ウェブサーバーとPythonウェブフレームワークとの間の振る舞いややり取りを共通化することで、 持ち運び可能なPythonのコードを書くことが可能になり、 WSGIに対応した任意のウェブサーバー にデプロイすることができます。 PEP-3333 でドキュメント化されています。

フレームワーク

大まかに言うと、ウェブフレームワークはウェブアプリケーションを実装するための処理を独自のコードを書くためのライブラリから成ります。 ほとんどのウェブフレームワークはパターンがあって、少なくとも以下のことができるようなツールがあります。 :

URLルーティング
HTTPリクエストされたものと呼び出されるべきPythonの特定のコードとのマッチングを行う。
リクエストとレスポンスオブジェクト
ユーザーのブラウザから送られてきた情報を受け取ってカプセル化する。
Template Engine
Allows for separating Python code implementing an application’s logic from the HTML (or other) output that it produces
Development Web Server
Runs an HTTP server on development machines to enable rapid development; often automatically reloads server-side code when files are updated

Django

Django is a “batteries included” web application framework. By providing many utilities and patterns out of the box, Django aims to make it possible to build complex, database-backed web applications quickly, while encouraging best practices in code written using it.

Django has a large and active community, and many pre-built re-usable modules that can be incorporated into a new project as-is, or customized to fit your needs.

There are annual Django conferences in the United States and in Europe.

Flask

Flask is a “microframework” for Python. Rather than aiming to provide everything you could possibly need, Flask implements the most commonly-used core components of a web application framework, like URL routing, request and response objects, and templates. As a user of Flask, it is therefore up to you to choose and integrate other components you may need, such as database access or form generation and validation. For many popular modules, Extensions may already exist to suit your needs.

Support for flask can best be found in its mailing list. Just shoot an email to flask@librelist.com and reply to the confirmation email.

Todo

Explain Pyramid

Web Servers

Nginx

Nginx (pronounced “engine-x”) is a web server and reverse-proxy for HTTP, SMTP and other protocols. It is known for its high performance, relative simplicity, and compatibility with many application servers (like WSGI servers). It also includes handy features like load-balancing, basic authentication, streaming, and others. Designed to serve high-load websites, Nginx is gradually becoming quite popular.

WSGI Servers

Stand-alone WSGI servers typically use less resources than traditional web servers and provide top performance [3].

Gunicorn

Gunicorn (Green Unicorn) is a WSGI server used to serve Python applications. It is a Python interpretation of the Ruby Unicorn server. Unicorn is designed to be lightweight, easy to use, and uses many UNIX idioms. Gunicorn is not designed to face the internet, in fact it was designed to run behind Nginx which buffers slow requests, and takes care of other important considerations. A sample setup for Nginx + gUnicorn can be found in the Gunicorn help.

Server Best Practices

The majority of self hosted Python applications today are hosted with a WSGI server such as gUnicorn, either directly or behind a lightweight web server such as nginx.

The WSGI servers serve the Python applications while the web server handles tasks better suited for it such as static file serving, request routing, DDoS protection, and basic authentication.

ホスティング

Platform-as-a-Service

Platform-as-a-Service (PaaS) is a type of cloud computing infrastructure which abstracts and manages infrastructure, routing, and scaling of web applications. When using PaaS, application developers can focus on writing application code rather than needing to be concerned with deployment details.

Most PaaS services offer a command-line interface that developers can use to set up and interrogate configuration, and to deploy new releases of an application to the service.

PaaS services and their partners offer add-on functionality which is well integrated into the platform, such as database hosting, email services, logging, scheduled and background tasks, billing and payment, etc.

Heroku

Heroku‘s Cedar stack offers first class support for Python 2.7 applications.

Heroku allows you to run as many Python web applications as you like, 24/7 and free of charge. Heroku is best described as a horizontal scaling platform. They start to charge you once you “scale” you application to run on more than one Dyno (abstracted servers) at a time.

Heroku publishes step-by-step instructions on how to set up your first application for use in Heroku, and maintains a list of example applications.

DotCloud

DotCloud は、WSGIアプリケーションとプラットフォーム上でバックグランドとワーカーのタスクを最初からサポートしています。 ウェブアプリケーションはPython 2.6、 nginxuWSGI で実行され、設定を変更することができます。

DotCloudは、gitレポジトリや他の任意のバージョン管理システムで管理されているアプリケーションを作業するための独自のコマンドラインAPIを使うことができます。

DotCloudはデータベース容量の制限や他のサービス(キャッシュ等)がない無料プランがあります。

使うためのヘルプやより詳しい情報を見る場合は、 DotCloudのPythonドキュメント で確認して下さい。

Gondor

Gondor はDjangoやPinaxのアプリケーションをデプロイするのに特化したPaaSです。 Gondorは、Pythonのバージョン2.7でDjangoのバージョン1.2と1.3をサポートしています。そして、 サイトの仕様が定義されている local_settings.py を使ってDjangoのサイトを自動で設定することができます。

Gondorは、Gondorプラットフォームに DjangoプロジェクトPinaxプロジェクト をデプロイするためのガイドを公開しています。

Templating

Most WSGI applications are responding to HTTP requests to serve content in HTML or other markup languages. Instead of generating directly textual content from Python, the concept of separation of concerns advises us to use templates. A template engine manage a suite of template files, with a system of hierarchy and inclusion to avoid unnecessary repetition, and is in charge of rendering (generating) the actual content, filling the static content of the templates with the dynamic content generated by the application.

As template files are sometimes written by designers or front-end developers, it can be difficult to handle increasing complexity.

Some general good practices apply to the part of the application passing dynamic content to the template engine, and to the templates themselves.

  • Template files should be passed only the dynamic content that is needed for rendering the template. Avoid to be tempted to pass additional content “just in case”: it is easier to add some missing variable when needed than to remove a likely unused variable later.
  • Many template engines allow for complex statements or assignments in the template itself, and many allow some Python code to be evaluated in the templates. This convenience can lead to uncontrolled increase in complexity, and often harder to find bugs.
  • It is often possible or necessary to mix javascript templates with HTML templates. A sane approach to this design is to isolate the parts where the HTML template passes some variable content to the javascript code.

References

[1]The mod_python project is now officially dead
[2]mod_wsgi vs mod_python
[3]Benchmark of Python WSGI Servers