Understand the logical ecology of Python using WebSocket

  • Foreword

    The difference and meaning of Restful and WebSocket are not difficult to understand logically, but how to use WebSocket is always confused, restfule is very intuitive to understand:

    df = restful_get_data
    result = deal_data(df)
    

    But websocket is pushed data, the data is not in blocks, how to use it?

  • websocket concept

  • Several common concepts
    1. Handshake : The handshake is the process of ensuring that the server and client are synchronized;
    2. websocket (Web socket): Web socket is defined as two-way communication between server and client, realizing real-time communication between client and server;
    3. URL : HTTP has its own mode, WebSocket also has its own mode:

    Insert picture description here

    1. Full duplex : The communication from both ends has a fairly fast speed.

    Before websocket , all communication between the web client and the server only relied on HTTP.

    websocket is a TCP- based protocol; a transport layer.

    WebSocket is a stateful protocol, and HTTP is a stateless protocol.

    1. The steps to establish a Web Socket connection are as follows :
      • The client establishes a connection through a process called Web Socket handshake;
      • The process starts with the client sending a regular HTTP request to the server ;
      • Request to upgrade the header . In this request, it informs the server that the request is for a Web Socket connection;
      • Web Socket URL uses ws scheme;

    A simple example of the initial request header is as follows:

    GET ws://websocket.example.com/ HTTP/1.1
    Origin: http://example.com
    Connection: Upgrade
    Host: websocket.example.com
    Upgrade: websocket
    
  • websocket event

    The four main Web Socket API events, each event by implementing respectively onopen, onmessage, onclose, onerrorand other functions to handle:

    1. turn on

      When a connection is established between the client and the server, an openevent is triggered from the Web Socket instance . It is called the initial handshake between the client and the server. The event that is triggered when the connection is established is called onopen.

      onopenRefers to the initial handshake between the client and server.

    2. News

      A message event usually occurs when the server sends some data. The messages sent by the server to the client can include plain text messages, binary data or images. Whenever data is sent, the onmessagefunction is triggered .

      This event acts as the client's ear to the server, and the onmessageevent is triggered whenever the server sends data .

    3. shut down

      The shutdown event marks the end of communication between the server and the client. With onclosethe help of the event, the connection can be closed. After the onclosecommunication is marked with the help of the event, the server and the client cannot transmit messages further. The shutdown event may also occur due to poor connection.

    4. error

      Some erroneous error flags occur during communication. It was onerrormarked with the help of an incident. After an error, the connection is always terminated.

      Event implementation effect
      onLoad Create an object to initialize the connection
      onOpen Establish connection with server and send status
      onMessage Display information
      onSend Send a message to the server
  • websocket operation

    Two main operations are supported:

    1. send()

      Messages can only be sent when the connection is open.

    2. close()

      Shake goodbye and terminate the connection.

  • websocket life cycle

    1. Create connection

      Creating a websocket connection is very simple: call the websocket constructor and pass in the URL of the server .

  • websocket与restful

    Websocket and restful exist side by side, but carefully distinguished, restful is an API based on http protocol . In fact, websocket directly corresponds to http . Both websocket and http are protocols, so there will be a conceptual correspondence deviation.

    In restful , all HTTP communication is guided by the client.

  • websocket与websockets

    You can see these two packages in the code:

    import websocket  # 是一个websocket服务的client端模块,websocket-client
    import websockets # 同时包含server端和client端
    

    As far as subscription data is concerned, first use websocket .

  • websocket-client(websocket)

    There is a basic usage introduction on github .

  • websockets ( official website EBEY tutorial )

    See the eBay tutorial.

  • References

  1. The difference between websocket-client (websocket) and websockets
  2. Python3 + WebSockets realize WebSocket communication
  3. WebSocket-Summary of mainstream implementation based on Python
  4. WebSocket Detailed Tutorial
Published 880 original articles · praised 1331 · 980,000 views

Guess you like

Origin blog.csdn.net/The_Time_Runner/article/details/105646308