• python实现websocket


    # websocket实现原理
    '''
    1.服务端开启socket,监听ip和端口
    2.客户端发送连接请求(带上ip和端口)
    3.服务端允许连接
    4.客户端生成一个随机字符串,和magic string组合进行一个sha1加密,加密。并将随机字符串发送给服务端
    5.然后服务端也要用相同的方式进行加密。
    6.然后服务端将加密之后的密串返回给客户端
    7.客户端将服务端返回的密串和自己加密之后的密串进行比对,如果一样,说明遵循同样的协议。如果不一样,就没法玩了·····
    '''
    
    import socket
    import base64
    import hashlib
    from pprint import pprint
    
    
    def get_headers(data):
        """
        将请求头格式化成字典
        :param data:
        :return:
        """
        header_dict = {}
        data = str(data, encoding='utf-8')
    
        header, body = data.split('
    
    ', 1)
        header_list = header.split('
    ')
        for i in range(0, len(header_list)):
            if i == 0:
                if len(header_list[i].split(' ')) == 3:
                    header_dict['method'], header_dict['url'], header_dict['protocol'] = header_list[i].split(' ')
            else:
                k, v = header_list[i].split(':', 1)
                header_dict[k] = v.strip()
        return header_dict
    
    
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind(('localhost', 8080))
    server.listen(5)
    
    # 等待用户连接
    conn, addr = server.accept()
    
    # 握手消息
    content = conn.recv(8096)
    '''
    >>> print(content)
    
    b'GET / HTTP/1.1
    Host: localhost:8080
    Connection: Upgrade
    Pragma: no-cache
    Cache-Control: no-cache
    Upgrade: websocket
    Origin: http://localhost:63342
    Sec-WebSocket-Version: 13
    User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36
    Accept-Encoding: gzip, deflate, br
    Accept-Language: zh-CN,zh;q=0.9
    Cookie: uuid=81a68694c772e0c62d4a5a3c256fe3e0; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%2216453a8bf2bbe-09a40e8e58a866-5e442e19-1fa400-16453a8bf2c745%22%7D; Hm_lvt_2af69bc2b378fb58ae04ed2a04257ed1=1530411925; Pycharm-bdfc5fce=a920e49d-da4e-4d2f-a76e-17acfacc6462
    Sec-WebSocket-Key: 1y6WpsSgfF80wqi3HpmrqQ==
    Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
    
    '
    '''
    
    # 获取请求头
    headers = get_headers(content)
    '''
    >>>pprint(headers)
    {'Cache-Control': 'no-cache',
     'Connection': 'Upgrade',
     'Cookie': 'Pycharm-bdfc5fce=a920e49d-da4e-4d2f-a76e-17acfacc6462',
     'Host': 'localhost:8080',
     'Origin': 'http://localhost:63342',
     'Sec-WebSocket-Key': 'RRGDeYeYSGEP9eHy85u8oQ==',
     'Sec-WebSocket-Version': '13',
     'Upgrade': 'websocket',
     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) '
                   'like Gecko',
     'method': 'GET',
     'protocol': 'HTTP/1.1',
     'url': '/'}
    '''
    
    # 规定:魔法字符串就叫这个
    magic_string = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
    
    # 获取随机密串,并将其与魔法字符串组合
    value = headers["Sec-WebSocket-Key"] + magic_string
    
    # 进行加密,规定,只能按照此加密方式
    hash_str = base64.b64encode(hashlib.sha1(bytes(value, encoding='utf-8')).digest())
    
    response_tpl = "HTTP/1.1 101 Switching Protocols
    " 
                   "Upgrade:websocket
    " 
                   "Connection: Upgrade
    " 
                   "Sec-WebSocket-Accept: %s
    " 
                   "WebSocket-Location: ws://%s%s
    
    "
    
    # 获取握手消息,组合魔法字符串,进行sha1加密
    # 发送给客户端
    response_str = response_tpl % (str(hash_str, encoding='utf-8'), headers['Host'], headers['url'])
    conn.send(bytes(response_str, encoding='utf-8'))
    

      

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
    </head>
    <body>
        <script>
            ws = new WebSocket("ws://localhost:8080/");
    
            //如果连接成功,会打印下面这句话,否则不会打印
            ws.onopen = function () {
                console.log('连接成功')
            }
        </script>
    </body>
    </html>
    

      

  • 相关阅读:
    SpringBoot(八):SpringBoot中配置字符编码 Springboot中文乱码处理
    SpringBoot(二): SpringBoot属性配置文件 SpringBoot多环境配置文件 SpringBoot自定义配置文件
    使用Maven新建SpringBoot工程
    SpringBoot(四): SpringBoot web开发 SpringBoot使用jsp
    SpringBoot(三):SpringBoot热部署插件
    SpringBoot(六):SpringBoot中如何使用Servlet?
    ARQ自动重传请求与UDP提供可靠交付
    多线程程序中操作的原子性转帖
    多目录的Makefile
    VC++小小绘图程序
  • 原文地址:https://www.cnblogs.com/traditional/p/9260758.html
Copyright © 2020-2023  润新知