What is websocket? websocket is a new protocol for communications between browsers and servers. Generally, http is the most common protocol for communications between browsers and servers, but http is stateless, each time the browser requests information, the channel between the browser and the server is closed after the server returns the information, which makes it impossible for the server to actively send information to the browser, server pushing technology in the http era, one solution is to use client polling or comet technology, while websocket, like General socket, enables a browser and a server to establish a duplex channel.
The specific websocket protocol is included in rfc6455. Here is a brief description. Websocket communication requires a handshake process to change the Protocol from http to webscoket, and then the browser and server can use this socket to communicate.
First, the browser sends the handshake information, requiring the protocol to be changed to websocket
GET, HTTP, 1.1
Host: example.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ =
Origin: http://example.com
After receiving the information, the server obtains the Sec-WebSocket-Key and concatenates it with a fixed string 258eafa5-e914-4710995ca-c5ab0dc85b11. Then, the obtained string is converted using sha1 and base64, the response string is obtained, so the message sent back by the server is as follows:
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK + xOo =
In this way, the handshake is complete, and the process of using python to implement this handshake is as follows.
Defhandshake (conn ):
Key = None
Data = conn. recv (8192)
Ifnotlen (data ):
ReturnFalse
Forlineindata. split ('\ r \ n \ r \ n') [0]. split (' \ r \ n') [1:]:
K, v = line. split (':')
Ifk = 'sec-WebSocket-key ':
Key = base64.b64encode (hashlib. sha1 (v + '258eafa5-E914-47DA-95CA-C5AB0DC85B11 '). digest ())
Ifnotkey:
Conn. close ()
ReturnFalse
Response = 'HTTP/1.1 101 Switching Protocols \ r \ n '\
'Upgrade: websocket \ r \ n '\
'Connection: Upgrade \ r \ n '\
'Sec-WebSocket-Accept: '+ key +' \ r \ n \ r \ N'
Conn. send (response)
ReturnTrue
After the handshake is complete, information is transmitted. The websocket data format is as follows.
+-+ ------- +-+ ------------- + ----------------------------- +
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 4 5 6 7 8 9 0 1
+-+ ------- +-+ ------------- + ----------------------------- +
| F | R | opcode | M | Payload len | Extended payload length |
| I | S | (4) | A | (7) | (16/64) |
| N | V | S | (if payload len = 126/127) |
| 1 | 2 | 3 | K |
+-+ ------- +-+ ------------- +--+
| Extended payload length continued, if payload len = 127 |
+---+ ----------------------------- +
| Masking-key, if MASK set to 1 |
+ ------------------------------- +
| Masking-key (continued) | Payload Data |
+ ----------------------------------+
: Payload Data continued ...:
+--+
| Payload Data continued... |
+ --------------------------------------------------------------- +
It is worth noting that payload len indicates the Data Length. If it is less than 126, payload len indicates the Data Length. If it is 126, the next two bytes are the data length, if it is 127, it indicates that the next eight bytes are the data length, and then there will be four bytes of masks. The actual data can be obtained only when payload data and mask are different or different, in this way, you can get the data. The format of the sent data is similar to that of the received data. For details, refer to rfc6455.
Then, on my github, I implemented the websocket server code, which is sufficient for simple use, however, for an application, you still need to implement the specific details of all protocols.