Contents
Tornado
Installation
-
Open Web Platform Installer and install "Python project":
- Follow steps in "Creating Tornado project":
- Create server.py file with the following content:
Output of sample Tornado application:import tornado.ioloop import tornado.web from tornado.options import define, options define("port", default=8888, help="run on the given port", type=int) class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world") application = tornado.web.Application([ (r".*", MainHandler), ]) if __name__ == "__main__": tornado.options.parse_command_line() application.listen(options.port) tornado.ioloop.IOLoop.instance().start()
Deployment
To deploy Python Tornado application you will need to install Python Hosting Package on a target server. Then just copy IIS web site from one machine to another.
web.config example
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<heliconZoo>
<application name="tornado.project" >
<environmentVariables>
<add name="PYTHONPATH" value="%APPL_PHYSICAL_PATH%;%APPL_PHYSICAL_PATH%\python_modules\Lib\site-packages;%PYTHONPATH%" />
<add name="APP_WORKER" value="%APPL_PHYSICAL_PATH%\server.py" />
<add name="DEPLOY_FILE" value="deploy.py" />
<add name="DEPLOY_LOG" value="log\deploy.log" />
</environmentVariables>
</application>
</heliconZoo>
<handlers>
<add name="tornado.project#x86" scriptProcessor="python.2.7.http"
path="*" verb="*" modules="HeliconZoo_x86"
preCondition="bitness32" resourceType="Unspecified" requireAccess="Script" />
<add name="tornado.project#x64" scriptProcessor="python.2.7.http"
path="*" verb="*" modules="HeliconZoo_x64"
preCondition="bitness64" resourceType="Unspecified" requireAccess="Script" />
</handlers>
</system.webServer>
</configuration>
Environment variables
-
PYTHONPATH
— path to Python modules. In the example it points to application root, the python_modules\Lib\site-packages subfolder and also includes value defined by user. -
APP_WORKER
— path to Tornado application’s worker script. -
WATCH_FILE_CHANGES_MASK
— optional files mask to watch for changes. Worker is restarted every time a file of specified type changes. In the example all.py
files are watched.
web.config in directory with static files
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<!--
This removes Helicon Zoo handler and makes IIS processing static files.
-->
<remove name="tornado.project#x64" />
<remove name="tornado.project#x86" />
</handlers>
</system.webServer>
</configuration>
deploy.py file example
Deploy file is used to execute commands, such as components installations or database migrations, on a server when the application initially starts or updated. The following code is only an example. We recommend you to use deploy.py file that comes with the Python project template.
# This file is example of deploy.py
# The file is executed once on the first request after every restart of IIS application.
# The file output is redirected to log file described in DEPLOG_LOG environment variable.
import sys
import os
import os.path
PROJECT_DIR = os.path.dirname(__file__)
os.chdir(PROJECT_DIR)
PIP_EXE = os.path.join(os.path.dirname(sys.executable), 'scripts\\pip.exe')
# update APPDATA env for pip
os.environ['APPDATA'] = os.path.join(PROJECT_DIR, 'python_modules')
# install requirements to local folder
os.system('{0} install --install-option="--prefix={1}" --requirement=requirements.txt'.format(PIP_EXE, os.path.join(PROJECT_DIR, 'python_modules')))