The birth of a website 07--Tornado web Server

Source: Internet
Author: User

Use spiders to crawl data, then do all sorts of processing, and then put it on a Web page for everyone to use.
Then, you need a Web Server.


Almost every language has a lot of Web server development frameworks, and Python is no exception, like here http://www.zhihu.com/question/20706333. Liaoche also has a Python tutorial http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 0014023080708565bc89d6ab886481fb25a16cdc3b773f0000.


Choosing the right Web server architecture can alleviate a lot of effort.


What metrics should I use to select Web server? My view is that the first concurrency is good, the speed is fast, the number of support online is high; second, no need to match nginx, Apache, I am lazy; Thirdly, the project is live, someone maintains it; the framework components can be replaced by third parties; Five, there are well-known sites in use, experienced the industry test; The code is concise.


Overall look down, tornado with these indicators of the highest fit, fast enough, pure python, code about 10,000 lines, a single process can run very well, only in the open multi-process need nginx support. Quora and Tornado are used.


Tornado's official website is in http://www.tornadoweb.cn/.


Http://zuijiacanting.com's backend code is not much, and less than 400 lines will be done.


One of the simplest Tornado Web servers follows the official tutorial, creating a s.py with the following content:


Import Tornado.ioloop
Import Tornado.web


Class MainHandler (Tornado.web.RequestHandler):
def get (self):
Self.write ("Hello, World")


application = Tornado.web.Application ([
(r "/", MainHandler),
])


if __name__ = = "__main__":
Application.listen (8888)
Tornado.ioloop.IOLoop.instance (). Start ()
Execute "python s.py" on the command, then enter "localhost:8888" in the browser address bar and press ENTER to see "Hello, World".


The back-end code in http://zuijiacanting.com is responsible for rendering the page with the Get method, using post to interact with Ajax, such as this:


Class Shopxxxhandler (Tornado.web.RequestHandler):
def get (self):
Template = Os.path.join (Template_dir, "shxxxxx.html")
Self.render (template)


Def post (self):
Shopid = Xhtml_escape (self.request.arguments[' shopid '][0])
LNG = Xhtml_escape (self.request.arguments[' LNG '][0])
lat = xhtml_escape (self.request.arguments[' lat '][0])
SQLQuery = "INSERT into shopxxxxtable (Shopid, LNG, LAT) values (\ '%s\ ', \ '%s\ ', \ '%s\ ')"% (Shopid, LNG, LAT)
Mpdb.execute_rowcount (sqlquery);
Self.write ("OK")
Return
There are also some details, such as special characters in the page display ESCAP module, to avoid cross-site attack xsrf policy, with the Logging module processing logs, with cookies to save user login and access and so on.


If you are interested in writing Web server, try probation The source code of tornado, and then write something similar, and then use Apachebench to test the effect to see if it can be improved.


One of Tornado's developers, Brett Taylor, was the first product manager for Google Maps, and Frindfeed's Co-founder,frindfeed, who became Facebook's platform director after being acquired by Facebook, later became CTO. Then he resigned to do a business project, called quip, the effect is probably on any device can be run can be multi-person co-editor can also be on the side of typing to discuss the ability to go offline @ Someone can share word processing software.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.