Skip to content

shajunxing/tore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Tornado Enhancement Package Readme

Introduction

TorE is an enhancement package of Tornado web framework which provides rapid ways to develop template and ajax based web applications. It is developed under Python 3.2 and compatible with same version of Tornado. It has following new features:

  1. Template engine has been enhanced to support ASP.NET like Code Behind style.
  2. Built-in Json Messaging compatible message server with WebSocket support.
  3. Other enhancements such as authentication, authorization, Json handling and so on.

Changelog

2013.01.17

  • Support Tornado version 2.4.1 now.
  • Added a "callback" setting to start_server() method.
  • Added a thread lock to messaging exchange to prevent possible confiction.
  • Added "write_html_file()" method to tore.web.RequestHandler class.
  • In tore.web.JsonHandler class, rename "write_object" to "write_json_object", rename "write_text" to "write_json_text", added "write_plain_text" method.
  • Merged tore.web.JsonHandler into tore.web.RequestHandler

2012.10.28

  • Support Tornado version 2.4 now.

Example

  1. Install Python 3.2.
  2. Install the latest Tornado package.
  3. run server.py
  4. Open Chrome or firefox, visit http://localhost:8080, enter any strings as user name and password.

Usage

Enhanced Template Engine

Tormado's template engine is enhanced by tore.web.RequestHandler, tore.web.Loader and tore.web.Template. Now the new template engine will search the file named ".py" and merge into the code generated by template, just like ASP.NET Code Behind style.

For example, there is a template file "ex1.t":

<html>
<body>
    {{ hello }}
    {{ world() }}
</body>
</html>

And a code file named "ex1.t.py" at the same dir as "ex1.t":

hello = 'Hello'

def world():
    return 'World'

After template engine loads "ex1.t", it will find "ex1.t.py" and if exists it will be loaded and parsed automatically and all the global things (variables, functions, classes ... ) will be merged to template file. So the response will be:

<html>
<body>
    Hello
    World
</body>
</html>

Enhanced template engine also supports template inheritance. If parent template and child template all have corresponding code file and if a name (variable name, function name ...) exists on both code files, the child one will override the parent one.

See "web/ex3.t" for more information.

Message Engine

Fully compatible with Json Messaging. see tore.start_server() for more information.

Since it is based on WebSocket protocol, only latest version of Chrome and Firefox are supported.

Notice tore.messaging.exchange.push(message, destination) can be used inside application to directly publish messages without going through TCP or UDP.

tore.web.JsonHandler (Deprecated and has been merged into tore.web.RequestHandler)

A handler inherited from tornado.web.RequestHandler and optimized for Ajax and RESTful Web Service. Adds some new methods:

  1. write_json_object(self, obj): Output Python object to Json format. The "Content Type" will be set to "application/json; charset=UTF-8".
  2. write_json_text(self, txt): Directly output Json text. The "Content Type" will be set to "application/json; charset=UTF-8". Notice this method will not do encoding and gramma checking.
  3. get_params_as_dict(self): Get dictionary wrapped request params, including query strings followed by url(GET) and request body(POST).
  4. get_body_as_text(self): Get text formatted requese body. Default encoding is UTF-8.
  5. get_body_as_object(self): Convert Json string formatted request body to Python object. For example, Json string can be submitted by jQuery.ajax(), notice that "processData" must be set to "false".

tore.web.authenticated Decorator

Used to decorate get, post ... methods and do HTTP basic authentication. see tore.start_server() for more information.

TorE's template engine is decorated by default.

tore.web.authorized Decorator

Used to decorate get, post ... methods and do authorizations. see tore.start_server() for more information.

TorE's template engine is decorated by default.

These two decorators can be used at same time, for example:

@tore.web.authenticated
@tore.web.authorized
def get(self, *args, **kwargs):
    ...

tore.start_server(**settings)

Simplest way to start Tornado with just one statement. It's parameters are listed below, most of them are enhanced from Tornado and some are newly added.

root_dir

(New) The absolute base dir of all the other settings. Default is os.getcwd()

handlers

(Enhanced) URL mappings, same as the param of tornado.web.Application's constructor. Some default mappings are added:

  1. /web: Mapped to "root_dir/web" which contains static and template files. Accessing rule is: ".t" are template files; ".py" are template code files and direct access is forbidden; other files are static files and handled by tornado.web.StaticFileHandler.
  2. /messaging: Message service's WebSocket url, which shares the same port with web application.
  3. /: The web root url, which will be redirected to /web/index.t. So /web/index.t must be existed.

port

(Enhanced) HTTP and WebSocket port, default is 80.

gzip

A boolean value to to enable/disable HTTP compression. Default is disabled.

encryption

(New) A boolean value to enable/disable HTTP SSL. If enabled, "certfile" and "keyfile" params must be set. Default is disabled.

certfile

(New) SSL public key filename. Same as the param of tornado.httpserver.HTTPServer. File path may be absolute or relative to root_dir.

keyfile

(New) SSL private key filename. Same as the param of tornado.httpserver.HTTPServer. File path may be absolute or relative to root_dir.

debug

(Enhanced) A boolean value to enable/disable debug mode, same as param of tornado.web.Application but added some new features:

  1. If disabled, all the HTTP log output generated by Tornado will be shutdown in order to improve performance.
  2. If enabled, response of tore.web.JsonHandler will be "human readable", which means Json string will not be ascii escaped and will be well formatted with line break and indent.
  3. If enabled, each request will trigger reloading of template and code file, just as Tornado's default behavor.

authentication

(New) A function which format is "foo(username, password)". tore.web.authenticated decorator will get user name and password from HTTP basic authentication and try to call this function. If this function exists and the return value is False, An 401 error will be raised, or the user name will be set as "current_user". Default is None.

unauthenticated_response_file

(New) An html file which content will be sent back while 401 error occured. File path may be absolute or relative to root_dir. Defalut is None and the 401 response will be "Unauthenticated".

authorization

(New) A function which format is "foo(username, path)". tore.web.authorized decorator will call this function using current user name and request url. If this function exists and the return value is False, An 403 error will be raised. Default is None.

unauthorized_response_file

(New) An html file which content will be sent back while 403 error occured. File path may be absolute or relative to root_dir. Defalut is None and the 401 response will be "Unauthorized".

messaging_tcp_port

(New) TCP port of messaging service, Which will be disabled if set to None. Default is None.

messaging_udp_port

(New) UDP port of messaging service, Which will be disabled if set to None. Default is None.

callback

(New) A callback function which format is "foo(port, messaging_tcp_port, messaging_udp_port)" and will be invoked after tornado ioloop started.

About

Enhancement package of Tornado web framework which provides rapid ways to develop template and ajax based web applications.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors