web socket code example

Example 1: socket

import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            if not data:
                break
            conn.sendall(data)

Example 2: what are websockets

persistent bi-directional communication channel between 
a client (e.g. a browser) and a backend service. 
In contrast with HTTP request/response connections, websockets 
can transport any number of protocols
provide  message delivery without polling
without racy, high-latency, and bandwidth-intensive implementations
establish TCP-style connections in a browser-compatible 
fashion using HTTP during initial setup.
Messages over websockets can be provided in any protocol, 
remove unnecessary overhead of HTTP requests and responses 
(including headers, cookies, and other artifacts).