Tornado installation, Environment preparation:
1. Python installation package and installation
2. Tornado installation Package
Python package installs Linux under installation
If you are using a Linux system or Mac OS X, the system may already have Python preinstalled. Enter Python in the Terminal Command Line window (or in the Program/tool/terminal of OS X). If you see this information, Python is already installed.
Python 2.7.10 (#2, Mar 31 2005, 00:05:10)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] on Darwin
Type "Help", "copyright", "credits" or "license" for more information.
>>>
Install under Windows
First, download the latest version of 2.7.9 from the official website of Python, www.python.org the following address: Http://www.python.org/ftp/python/2.7.9/python-2.7.9.msi
Then, run the downloaded MSI installation package, and tick all the components when you select the step of the installation component:
Pay attention to the selection pip
and Add python.exe to Path
(with this you do not have to manually configure the environment variable), and then all the way to "Next" to complete the installation. Python will be installed by default in the C:\Python27
directory, and then open a command prompt (cmd) window, after entering Python, there are two scenarios:
Case one: As shown, Python is installed properly, can be used, type any Python code, enter to execute, and get the results. You can enter exit()
and return to exit the Python interactive environment (of course, the direct k out of the command line window more convenient).
Situation two: Get an error:显示
This is because Windows will look for the Path
path set by an environment variable, and if it does python.exe
解释器
not, it will report the error shown. If you omit the tick when you install Add python.exe to Path
it, you will need to manually python.exe
add the path you are C:\Python27
adding to your path. If you do not know how to modify the environment variable, you can reinstall the Python installer again, remember to check it Add python.exe to Path
就可以了
.
Tornado installation 1. Environment preparation
Os:ubuntu 13.04 64bits/centos
python:2.7.4 is just the version number, and you can install the Python version yourself as needed
tornado:3.1 is just the version number, and you can install the Python version yourself as needed
2. Download and install Tornado
You can download Tornado's compressed package (very small, about 400K or so) on Tornado's official website and then install it offline. As follows, select the item as shown:
http://www.tornadoweb.org/en/stable/
There is little difference between installing Tornado on Linux and Windows. Install the Linux installation first:
1. Unzip the package
Tar xvzf tornado-3.1.tar.gz
2. Go to the Uncompressed Tornado folder
CD tornado-3.1
3. Build a Python extension
Python setup.py Build
4, Tornado installation (need to install with the super User, or will be reported insufficient authority)
sudo python setup.py install
The installation of Tornado under Linux is complete. Installation under Windows is similar to Linux.
1. Unzip the package
2. Locate the Unpacked folder under "Command Prompt"
3. Build a Python extension
Python setup.pybuild
4, Tornado installation
Python setup.py Install
Of course, you can also use PIP for installation (assuming, of course, that PIP is already installed):
Pip Install tornado
3. Verify
Hello World
Use the example in the first chapter of Introduction to Tornado as Hello World, as it introduces more than the examples in the official website
1. Use of command-line arguments; 2.get parameters.
#!/usr/bin/python
#-*-coding:utf-8-*-
Importtornado.httpserver
Importtornado.ioloop
Importtornado.options
Importtornado.web
Fromtornado.options import define, Options
Define ("Port", default=8000, help= "run on the given port", Type=int)
Classindexhandler (Tornado.web.RequestHandler):
def get (self):
Greeting =self.get_argument (' greeting ', ' Hello ')
Self.write (greeting + ', tornado! ')
if__name__ = = "__main__":
Tornado.options.parse_command_line ()
App =tornado.web.application (handlers=[(r "/", Indexhandler)])
Http_server = Tornado.httpserver.HTTPServer (APP)
Http_server.listen (Options.port)
Tornado.ioloop.IOLoop.instance (). Start ()
Executes the python hello.py
boot server.
Open Http://localhost:8000/in your local browser to see:
Hello, tornado!.
Open Http://localhost:8000/?greeting=feng to see:
Feng, tornado!
The following is a simple explanation of the code:
4-7 rows: Import the four modules of the tornado that must be in the demo use case code.
9-10 line: From the Tornado.options module to read the configuration and parse commands from the command line, define a configuration option port, port. Default represents the defaults, Help represents the information that is displayed in assistance , type represents the data type of the configuration parameter, and an error occurs if the type match errors.
12-15 Line: This is a page request for the handler class, inheriting the Web's RequestHandler class. There is only one get method, which represents a GET request for HTTP. The RequestHandler class has some useful built-in methods for get_argument
getting HTTP page request parameters, which are displayed as default values (the second parameter of the Get_argument method) if no parameters are passed in. Another common built-in method write
is to write the content of the response to the page.
18 rows: Read and parse configuration parameters from the command line.
19 Rows: Create a tornado app. Handlers is a tuple list, where the first element of each tuple is a regular expression that represents the URL route, and if it contains a capture packet, the matched content is sent to RequestHandler, and the second element is the RequestHandler class used to respond to the operation.
20 Line: Use this Tornado app to create an HTTP server.
21 Line: Set the listening port of the HTTP server, the command line has an incoming port to listen to the incoming port, not the default port to listen.
22 Line: Start the server.
Python----Tornado Installation