Thursday, 6 September 2012

Help Documentation - Python Application Engine

WSGI specification

You are using Python Web application development environment, you need to follow the WSGI specification. The following is a Python script to follow WSGI specification Example:
  from bae. core. wsgi import WSGIApplication   def app (environ, start_response):        status = '200 OK '        headers = [('Content-type', 'text / html')]        body = ['hello guys \ n \ n']        start_response (status, headers)        return body   application = WSGIApplication (app) 
In simple terms, WSGI specification can be understood as a function call specification, which includes the following components:
  • 1. Callable Definition
Applications to provide a Python script file in the file, you must provide a name for the application of the callable object, callable objects include:
• Functions
• Implementation of the __call__ () method object
Examples of the application is a realization __call__ () method callable object. It accepts two input parameters.
  • 2 Enter the parameters environ
In the above example, the first parameter is the environ dict type that contains an HTTP request environmental information. 
Python environment, WSGI environment variables are defined as follows:
Environment Variables Description
wsgi.version Always (1, 1)
wsgi.url_scheme http or https
wsgi.input POST request body input stream
wsgi.errors Error output stream; BAE does not currently support
wsgi.multithread False
wsgi.multiprocess True
wsgi.run_once False
  • 3 Enter the parameters start_response
The second parameter start_response example is a function type, application calls this function to return the appropriate HTTP response code and the HTTP response header.
  • 4 Return Value
  application () return value is an Iterable object.  Iterable object refers to the object traversal operation can be performed, including:  
• list
• iterator
• string
• tuple
Iterable object returned by each element must be of type string or unicode. Recommend that you list or iterator to return data, such as ['hello', 'world']. Although the string type is also iterable, but not recommended for direct return string, this will lead to a string traversal operation, performance will be relatively low.

Profiles

When you use an application developed in Python environment, you must provide a configuration file that specifies the front-end Web Server URL request will be mapped to the application script. The profile name is app.conf. 

Constant

When you develop applications that can be accessed from bae.core.const to the following environment variables:
Environment Variables Description
APP_NAME Name of the application
APP_DIR Application root directory
APP_TMPDIR Application of the temporary directory

Static file support

In addition to python script to generate dynamic pages, it also needs a lot of support ". Html", ". Css", ". Js", ". Jpg" format, such as static files.
For static files, can be achieved through the configuration file; specific implementation steps, please refer to the following example:
1 In the application's root directory, create a static directory, all types of static files into this directory.
For example: Create the following directory structure:
 index.py   app.conf   faivcon.ico   static /       css /       image /       js /       html /  
(2) Put a picture in the image directory winter.jpg.
3 modify the configuration file, add a mapping for static files:
 handlers:     - Url: /       script: index.py       - Url: / (. *). Py       script: $ 1.py       - Url: / static / (. *)       script: / static / $ 1       - Expire:. Jpg modify 10 years     - Expire:. Swf modify 10 years     - Expire:. Png modify 10 years     - Expire:. Gif modify 10 years     - Expire:. JPG modify 10 years     - Expire:. Ico modify 10 years  
4 Visit winter.jpg you can see just added pictures.

Module search path

The default module search path is:
Apply the current directory> Python execution environment system directory.
Application to add custom module search path methods are:
  import sys       sys. path. insert (0, your_custom_module_path) 

Temporary directory

In order to support distributed applications, extensions, BAE prohibit the application on the local file system writes. Some applications in the execution of the request process, the need to create a temporary file for each application we provide a separate temporary directory, applications can create temporary files in this directory. You can bae.core.const.APP_TMPDIR get a temporary directory. Meanwhile, we tempfile Python standard library has been modified, you are in the process of developing this module can be used directly to create a temporary file.

Error handling, logging, standard output

When you use the BAE Python application development, information about error handling, logging, and standard output, the following issues should be noted:
  • Application through the return value of the output results back to the WEB server;
  • stdout and stderr are redirected to / dev / null. This means you can not use in the development process stdout and stderr for debugging;
  • wsgi.stderr be closed;
  • You develop the application process should be used to provide a log module bae.api.logging BAE to output the log. Using the module output logs will be sent to a dedicated log server via WEB interface to view the log;
  • Not recommended to use the Python standard library logging module, using the module output log information can not be viewed.

Module cache

To shorten the response time of the request, BAE Python application execution environment will import all the modules for caching. But the module cache might cause this to happen: If the application module uses global variables or class variables, then the value of this variable between requests may hold. For example:
  from bae. core. wsgi import WSGIApplication     counter = 0   def app (environ, start_response):        global counter       counter + = 1       status = '200 OK '        headers = [('Content-type', 'text / html')]        body = ['counter: \ n \ n'% counter]        start_response (status, headers)        return body     application = WSGIApplication (app) 
As users continue to access the application, counter value is incremented. However, due to BAE Python execution environment is based on a distributed architecture, the request will fall on different machines. So that the user run this application, the access to the counter value is not necessarily a result of the increment of. Another BAE Python execution environment in certain circumstances may restart the service process, resulting in counter back to 0. For these reasons, you develop an application, you need to use according to their needs cache module characteristics.

Preloaded list of modules

In BAE Python environment, in addition to the standard Python modules, it also comes with the following modules. You should try to use the process of developing pre-installed modules. If the module is not dependent on the list of pre-installed modules, then you need to include it in the application directory.
Module Name The default version
BeautifulSoup 3.2.0
Imaging 1.1.7
PyYAML 3.10
MySQL-python 1.2.3
numpy 1.6.1
South 0.7.3
pycrypto 2.4.1
MarkupSafe 0.15
Jinja2 2.6
Pygments 1.4
WebOb 1.1.1
pytz 2011n
werkzeug
SQLAlchemy 0.7.5
oauth2
Markdown 2.1.0

Built-in service

  • Cache (Cache) provides high-performance, distributed memory object caching service.
  • Counter (counter) provide distributed counting services used to access statistics, voting application scenarios.
  • Cron (Cron Job) provides a distributed environment timed task functions.
  • FetchURL (web crawling) crawling web content via the URL provided services.
  • Image (image processing) provided in accordance with user-defined image capture and image processing operations on the service.
  • Log (Log) provides logging services, can set the log level.
  • MySQL (database) provides a distributed relational database services, back-end using the industry one of the most widely used database: MySQL.
  • MongoDB provides a distributed non-relational database services, back-end using the industry one of the most widely used database: MongoDB.
  • Redis provides key-value-type non-relational database services, back-end using the industry one of the most widely used database: Redis.
  • Rank (ranking) ranking statistics provide distributed services, real-time information for all types of ranking (such as games, user visits, etc.).
  • Socket (network proxy) to access the external network through a proxy, no changes to the interface, support for TCP, UDP, and monitor does not support.
  • TaskQueue (task queue) provides a distributed task queue service can be asynchronous and concurrent manner efficient implementation of user-submitted FetchURL, offline download tasks.

Environmental restrictions

Since the execution environment will also provide services for multiple applications, in order to prevent mutual interference between multiple applications, we Python execution environment for a certain functional limitations, these limitations include:
  • File System Access restrictions:
• Applications directory, temporary directory allows read, read other prohibited;
• temporary directory to allow write, create directory, delete directory, create files, delete files, other directories prohibited actions;
  • Temporary files support:
• Modified tempfile.py, retain all the classes, functions, but make sure to return the temporary file or directory in the user's own temporary file directory;
  • Network Access Restrictions:
• Only allow the creation of the TCP protocol socket;
• Only allow access to back-end services specified environment variable, the other is prohibited;
  • Prohibits create multiple processes:
• Prohibit os.fork;
• Prohibit subprocess, popen2, pipes, _multiprocessing module;
  • Multithreading restrictions:
• Allows multiple threads, but the thread of the life cycle is limited to a request within. After each request, if the application does not end the process of creating, by the execution environment mandatory recycling;
• Allow users to create up to 10 threads;
  • Signal restrictions:
• signal module All functions are limited;
  • C extension module limitations:
• Allow loading system path of the C module, such as math.so; another directory of the C extension module from loading;
  • Module is loaded:
• Allow the path from the system, application directory, load modules; another path blocked from loading;
  • Zip module supports:
• Allow the application directory from the zip file into pure Python modules, but not import C extension module;
  • Banned modules:
• removed from the standard library 'ctypes', '_ctypes_test', 'dl', 'nis', 'resource', 'sqlite', 'fcntl', 'spwd', 'pwd', 'grp', 'syslog', '_multiprocessing' other modules;
  • Banned function:
• Posix module allows the following system calls, other prohibited;
• Allow functions: Access, chmod, chown, getcwd, listdir, mkdir, readlink, rename, rmdir, stat, unlink, remove, utime, getpid, getuid, open, fdopen, close, lseek, read, write, strerror, chdir;
  • Limiting function:
• For posix module rmdir, unlink, rename, link, chmod, chown, listdir, mkdir, open, fdopen, function parameters such as the file path for the restriction;
• For builtin module, execfile () can only perform their own directory under the application module; compile () can only apply its own settings file called a module corresponding directory under the file name;
Application code should avoid a conflict with the above restrictions, otherwise it will not work as expected.

Development Framework

BAE preloaded with some popular WEB framework, including: django, lask, tornado, etc., which is the focus of our support django WEB framework; Other WEB framework, and did not do a complete compatibility testing, some of the functions with BAE environmental conflicts can not be be used.

Django

BAE preloaded with Django 1.4 version. If you need to Django-based applications to the BAE environment, you need to pay attention to the following matters:
CACHE
• Cache backend only supports memcache;
• Cache obtained through bae.core.const.CACHE_ADDR address;
Configuration Example
  CACHES = {       'Default': {            'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',           'LOCATION': const. CACHE_ADDR,           'TIMEOUT': 60,       }   } 
DATABASE
• DATABASE backend currently only supports MySQL;
• From bae.core.const get host, port, user, password;
• Please BAE management interface, use PHPMyAdmin to manage the maintenance database, table;
Configuration Example
   from bae. core import const DATABASES = {'default': {'ENGINE': 'django.db.backends.mysql', 'NAME': 'cluster11db3', 'USER': const. MYSQL_USER, 'PASSWORD': const. MYSQL_PASS, 'HOST': const. MYSQL_HOST, 'PORT': const. MYSQL_PORT,}} 
MAIL
• MAIL backend please use BAE new BCMS backend;
• You must configure EMAIL_BCMS_QNAME. BAE management interface can be obtained from bcms_qname;
Configuration Example
 EMAIL_BACKEND = 'django.core.mail.backends.bcms.EmailBackend'   EMAIL_BCMS_QNAME = '13fe1db4a291e6a57574071e8ff3a52c ' 
SESSION
• SESSION backend support Cache backend and DB backend;
• Cache backend is based Cache implementation, and therefore must be configured and Cache options related to the foregoing;
• DB backend is MySQL-based implementation, and therefore must be configured with the DATABASE options related to the foregoing;
Configuration Example
 SESSION_ENGINE = 'django.contrib.sessions.backends.cache'   # # # Or   SESSION_ENGINE = 'django.contrib.sessions.backends.db' 
ADMIN
• In the project directory, create a static directory;
• From Django1.4 source code, you will django / contrib / admin / static / admin directory copied to the newly created static directory;
• Modify app.conf, adding support for static files;
LOGGING
• Django uses Python standard library logging module for logging output; logging module by default the log output to stderr;
• Set WSGIApplication (app, stderr = "log"), the standard error will be output to the log, the log server redirected to the BAE.
Development Samples
Below Django official website of the sample program, for example, describes how to create an environment in BAE Django application. This example uses the MySQL service.
Step one: create a Django project in the machine and debugging.
• You need to install on your development machine Python2.7.2, Django1.4, MySQLdb;
• Create a working directory: mkdir django_root && cd django_root;
• Follow the guidelines described in the Django development at the local establishment Django Demo projects and local debugging;
• Complete turorial 1-4, the directory structure is as follows:
    django_root /       djangodemo /           djangodemo /           polls /           templates /               admin /               polls /           manager. py 
Which templates directory is used to save the template related files.
Step two: Migrate the project to BAE.
• at BAE to create a Python project Django Demo;
• Create a version and then update to the project via SVN own development machine;
• The first step to create a Django project copied the following steps:
  cd djangodemo   ls-l # # # directory should have index.py, app.conf, favicon.ico three documents;   cp-R XXX / django_root / djangodemo / * 
• From Django1.4 installation directory, or source code directory, Admin related static files copied:
    mkdir-p static   cp-R DJANGO_SOURCE / django / contrib / admin / static / admin static / 
• Modify settings.py, BAE environments using MySQL database, and through the management interface provided by BAE PHPMyAdmin to create the database and table.
• Modify index.py, modified index.py as follows:
 import os import sys os. environ ['DJANGO_SETTINGS_MODULE'] = 'djangodemo.settings' path = os. path. dirname (os. path. abspath (__ file__)) + '/ djangodemo' if path not in sys. path: sys. path. insert (1, path) from django. core. handlers. wsgi import WSGIHandler from bae. core. wsgi import WSGIApplication application = WSGIApplication (WSGIHandler ()) 
• Modify app.conf, to increase support for static files. Modified app.conf as follows:
  handlers:      - Url: / static / (. *)        script: / static / $ 1      - Url: /. *        script: index. py        - Expire:. Jpg modify 10 years      - Expire:. Swf modify 10 years      - Expire:. Png modify 10 years      - Expire:. Gif modify 10 years      - Expire:. JPG modify 10 years      - Expire:. Ico modify 10 years 
After completing the above steps, you can access your Django application it.

Flask

 from flask import Flask, g, request app = Flask (__ name__) app. debug = True @ app. route ('/') def hello (): return "Hello, world! - Flask \ n" from bae. core. wsgi import WSGIApplication application = WSGIApplication (app) 

Tornado

  tornado web site     import tornado. wsgi     class MainHandler (tornado. web. RequestHandler):       def get (self):           self. write ("Hello, world! - Tornado \ n")     app = tornado. wsgi. WSGIApplication ([       (R "/", MainHandler),   ])     from bae. core. wsgi import WSGIApplication   application = WSGIApplication (app) 

Webapp2

    import webapp2      class HelloWebapp2 (webapp2. RequestHandler):       def get (self):           self. response. write ('Hello, webapp2! \ n')     app = webapp2. WSGIApplication ([       ('/', HelloWebapp2),   ], Debug = True)     from bae. core. wsgi import WSGIApplication   application = WSGIApplication (app) 

Web.py

Thank Aaron H. Swartz (1986 - 2013) !
 import web urls = ('/', 'Hello') app_root = os. path. dirname (__ file__) templates_root = os. path. join (app_root, 'templates') render = web. template. render (templates_root) class Hello: def GET (self): return render. hello () app = web. application (urls, globals ()). wsgifunc () from bae. core. wsgi import WSGIApplication application = WSGIApplication (app) 

Bottle

BAE in yet preinstalled bottle framework, developers need to bottle.py put together under the application root directory uploads.
  from bottle import Bottle   app = Bottle ()     @ App. Route ('/')   def hello ():       return "Hello, world!"     from bae. core. wsgi import WSGIApplication   application = WSGIApplication (app) 


0 comments:

Post a Comment