Tornado, a high-performance web Server framework, implements restful interfaces and Development instances.
A friend asked me to engage in the tornado framework. To be honest, I don't use much of this framework...
I will share some of my O & M R & D examples with you.
I want to know how to install tornado.
pip install tornado
Let's talk about some of his modules on the official website. Here I will try again on the rereading machine, which is mixed with my understanding.
Main modules
The basic web framework used by Web-FriendFeed includes most of Tornado's important functions. You can do it right.
Escape-XHTML, JSON, and URL encoding/Decoding Methods
Database-simple encapsulation of MySQLdb makes it easier to use. It is an orm.
Template-Python-based web template system, similar to jinja2
Httpclient-non-blocking HTTP client, which is designed to work with web and httpserver. This is similar to adding urllib2
Auth-third-party authentication implementation (including Google OpenID/OAuth, Facebook Platform, Yahoo BBAuth, FriendFeed OpenID/OAuth, Twitter OAuth)
Locale-Support for localization and Translation
Options-the command line and Configuration File Parsing tools are optimized for the server environment and the parameters are accepted
Underlying Module
Httpserver-a simple HTTP server implementation for the web module
Iostream-simple encapsulation of non-blocking socket to facilitate common read/write operations
Ioloop-core I/O Loops
Let's talk about how tornado accepts the request:
Get Method
class MainHandler(tornado.web.RequestHandler): def get(self): self.write("You requested the main page") class niubi(tornado.web.RequestHandler): def get(self, story_id): self.write("xiaorui.cc niubi'id is " + story_id) application = tornado.web.Application([ (r"/", MainHandler), (r"/niubi/([0-9]+)", niubi), ])
In this way, the access to/niubi/123123123 will go through the niubi class, which contains the get parameter.
Post method
class MainHandler(tornado.web.RequestHandler): def get(self): self.write('
In tornado, get and post are generally included in an access route, but they are differentiated by different methods.
Let's test get and post.
import tornado.ioloop import tornado.web import json class hello(tornado.web.RequestHandler): def get(self): self.write('Hello,xiaorui.cc') class add(tornado.web.RequestHandler): def post(self): res = Add(json.loads(self.request.body)) self.write(json.dumps(res)) def Add(input): sum = input['num1'] + input['num2'] result = {} result['sum'] = sum return result application = tornado.web.Application([ (r"/", hello), (r"/add", add), ]) if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
# You can write a form test or use curl-d for testing.
Why use Tornado as the Web development framework?
This is also the reason for developing Tornado for FriendFeed-Because FriendFeed needs to update Timeline in real time, and Comet is currently the best and most popular method. Because zhihu also has a large number of long polling connections to be maintained, it is reasonable to choose Tornado. However, we also need to see that Tornado is not a panacea. Since Tornado's WEB Server is a single thread, if a Request is blocked by I/O, the process will remain suspended, neither the new Request nor the other requests that are being blocked cannot be accepted. Although Spawn can have multiple Tornado processes, too many Spawn will consume a large amount of memory resources. This is like the FastCGI process of PHP. Therefore, requests that block I/O are generally sent to other dynamic backend servers using the asynchronous HTTP Client built in Tornado. Therefore, Tornado usually requires a layer of nginx for reverse proxy before production, and nginx is used for static files and other large data volume I/O operations. Tornado's I/O time is too expensive to afford. As for the few Tornado documents you mentioned, I think you can take the time to read Tornado code. After all, it is a lightweight framework with few codes, but the comments are very detailed and easy to understand. Remember, code is always the best document!
When Jersey developed RESTfulWebService, it encountered an error in the resource interface for Tomcat instantiation and release. I hope you can give me some advice,
The error message indicates that the interface class cannot be instantiated. Now you write the Annotation on the interface class, and the interface class itself does not have a specific function implementation method.
Try to write the annotation and so on in its inheritance class