Home  >  Article  >  Backend Development  >  Introduction to the concept and application of WebSocket in Python's Tornado

Introduction to the concept and application of WebSocket in Python's Tornado

不言
不言forward
2018-10-20 15:31:352302browse
This article brings you an introduction to the concept and application of WebSocket in Python's Tornado. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The asynchronous nature of Tornado makes it very suitable for high-concurrency processing of servers. The persistent connection application framework between the client and the server is a typical application of high concurrency. WebSocket is the HTML5 standard technology that establishes a persistent connection between the HTTP client and the server. This chapter will explain the application of WebSocket technology in the Tornado framework.
WebSocket protocol is a new standard protocol (RFC6455) defined by HTML5, which implements full-duplex communication between the browser and the server.

1. WebSocket application scenarios

Traditional HTTP and HTML technologies use the client to actively send requests to the server and obtain responses. However, as the demand for instant messaging increases, this communication model sometimes cannot meet the needs of applications.

WebSocket is similar to ordinary Socket communication. It breaks the original one-to-one communication model of HTTP's Request and Response, and also breaks the application scenario where the server can only passively accept client requests. Perhaps readers have heard of dynamic client technologies based on traditional HTTP such as Ajax and Long poll, but these technologies all use polling technology, which consumes a lot of network bandwidth and computing resources.

WebSocket is the HTML5 standard formulated to deal with such scenarios. Compared with ordinary Socket communication, WebSocket defines a basic interaction process at the application layer, making server frameworks such as Tornado and JavaScript clients Standard WebSocket modules can be built.

The characteristics of WebSocket are summarized as follows:

  • WebSocket is suitable for scenarios where the server actively pushes.

  • Compared with technologies such as Ajax and Long poll, the WebSocket communication model is more efficient.

  • WebSocket still completes Internet communication with HTTP.

  • Because it is a standard protocol of HTML5, it is not blocked by corporate firewalls.

2. Communication principle of WebSocket

The communication principle of WebSocket is to establish a TCP persistent link between the client and the server, so that the current When the server has messages that need to be pushed to the client, it can communicate instantly.

Although WebSocket is not HTTP, because HTML content is encapsulated and transmitted by HTTP on the Internet, WebSocket still needs to cooperate with HTTP. The IETF defines the standard process for establishing a WebSocket channel based on an HTTP link in RFC6455.

The client tells the server that it needs to establish a WebSocket long link channel by sending the following HTTP Request:

GET /stock_info/?encoding=text HTTP/1.1
Host:echo.websocket.org
Origin:http://websocket.org
Cookie:__token=ubcxx13
Connection:Upgrade
Sec-WebSocket-Key:uRovscZjNol/umbTt5uKmw==
Upgrade:websocket
Sec-WebSocket-Version:13

Readers can find that it is still an HTTP Request packet and are very familiar with the contents.

  • HTTP request method: GET

  • Request address:/stock_info

  • HTTP version number: 1.1

  • Server host domain name: echo.websocket.org

  • Cookie information: __token=ubcxx13

But there are 4 distinctive fields in the HTTP Header. They are:

Connection:Upgrade
Sec-WebSocket-Key:uRovscZjNol/umbTt5uKmw==
Upgrade:websocket
Sec-WebSocket-Version:13

This is the core of WebSocket link establishment. It tells the Web server: the client wants to establish a WebSocket link. The client uses The WebSocket version is 13, the key is uRovscZjNol/umbTt5uKmw==.

After receiving the Request, the server will return a Response similar to the following if it agrees to establish a WebSocket link:

HTTP/1.1 101 WebSocket Protocol Handshake
Date:Fri,10 Feb 2012 17:38:18 GMT
Connection:Upgrade
Server:Kaazing Gateway
Upgrade:WebSocket
Access-Control-Allow-Origin:http://websocket.org
Access-Contril-Allow-Credentials:true
Sec-WebSocket-Accept:rLHCKw/SKs09GAH/ZSFhBATDKrU=
Access-Control-Allow-Headers:content-type

This is still a standard HTTP Response, in which the Header information related to WebSocket is :

Connection:Upgrade
Upgrade:WebSocket
Sec-WebSocket-Accept:rLHCKw/SKs09GAH/ZSFhBATDKrU=

The first two pieces of data tell the client: The server has converted this connection into a WebSocket link. Sec-WebSocket-Accept is the data generated after encrypting the Sec-WebSocket-Key sent by the client, so that the client can confirm that the server can work normally.

At this point, a TCP persistent link has been established between the client and the server, and the two parties can send messages to each other at any time.

The above is the detailed content of Introduction to the concept and application of WebSocket in Python's Tornado. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete