美文网首页
python版websocket

python版websocket

作者: hugoren | 来源:发表于2019-05-27 11:11 被阅读0次

背景

目前看到实现websocket的框架的
- django的channels
- flask
- sanic
好奇,用python怎样写一个websokcet服务器,以及socket如何与应用框一起启动

后记

  1. accept 与recv是阻塞模式,调用时候,同时新开一个threading解决同步阻塞的问题
  2. while True 要写在 sock.accept() 之前,因为在要等不同的socket连接,如果写在这之后,只能有一个socket连接
  3. 首次连接通过http建立,之后直接互相通信 while True 同样要写在conn.recv(8094) 之前, 因为要不停的通信
  4. 整个socket通信要写两次 while True
  5. werkzeug与websocket同时启动, 在主进程内,各开一个子进程即可
    6.在此基础上可以按需扩展,如改成python3 的async写法 , 引用更高级别的库websocket或python3的protocol等。万变不离其宗,从python层面,这是比较底层的写法,其它的写法大多是在此基础上封装的。

第一版后端

import socket
import struct
import hashlib
import base64


def get_headers(data):
    headers = {}
    data = str(data, encoding="utf-8")

    header, body = data.split("\r\n\r\n", 1)

    header_list = header.split("\r\n")

    for i in header_list:
        i_list = i.split(":", 1)
        if len(i_list) >= 2:
            headers[i_list[0]] = "".join(i_list[1::]).strip()
        else:
            i_list = i.split(" ", 1)
            if i_list and len(i_list) == 2:
                headers["method"] = i_list[0]
                headers["protocol"] = i_list[1]
    return headers


def parse_payload(payload):
    payload_len = payload[1] & 127
    if payload_len == 126:
        extend_payload_len = payload[2:4]
        mask = payload[4:8]
        decoded = payload[8:]

    elif payload_len == 127:
        extend_payload_len = payload[2:10]
        mask = payload[10:14]
        decoded = payload[14:]
    else:
        extend_payload_len = None
        mask = payload[2:6]
        decoded = payload[6:]

    # 这里我们使用字节将数据全部收集,再去字符串编码,这样不会导致中文乱码
    bytes_list = bytearray()

    for i in range(len(decoded)):
        # 解码方式
        chunk = decoded[i] ^ mask[i % 4]
        bytes_list.append(chunk)
    body = str(bytes_list, encoding='utf-8')
    return body


def send_msg(conn, msg_bytes):
    token = b"\x81"
    length = len(msg_bytes)
    if length < 126:
        token += struct.pack("B", length)
    elif length <= 0xFFFF:
        token += struct.pack("!BH", 126, length)
    else:
        token += struct.pack("!BQ", 127, length)

    msg = token + msg_bytes
    conn.sendall(msg)
    return True


def server_socket():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind(("127.0.0.1", 10083))
    sock.listen(5)
    conn, addr = sock.accept()
    print(conn)
    data = conn.recv(8096)
    headers = get_headers(data)
    # 对请求头中的sec-websocket-key进行加密
    response_tpl = "HTTP/1.1 101 Switching Protocols\r\n" \
                   "Upgrade:websocket\r\n" \
                   "Connection: Upgrade\r\n" \
                   "Sec-WebSocket-Accept: %s\r\n" \
                   "WebSocket-Location: ws://%s\r\n\r\n"

    magic_string = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'

    if headers.get('Sec-WebSocket-Key'):
        value = headers['Sec-WebSocket-Key'] + magic_string

    ac = base64.b64encode(hashlib.sha1(value.encode('utf-8')).digest())
    response_str = response_tpl % (ac.decode('utf-8'), headers.get("Host"))
    conn.sendall(bytes(response_str, encoding="utf-8"))

    while True:
        data_1 = conn.recv(8096)
        data_2 = parse_payload(data_1)
        print(data_2)
        send_msg(conn, b"test")


if __name__ == "__main__":
    server_socket()

web 端

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .message{
            width: 60%;
            margin: 0 10px;
            display: inline-block;
            text-align: center;
            height: 40px;
            line-height: 40px;
            font-size: 20px;
            border-radius: 5px;
            border: 1px solid #B3D33F;
        }
        .form{
            width:100%;
            position: fixed;
            bottom: 300px;
            left: 0;
        }
        .connect{
            height: 40px;
            vertical-align: top;
            /* padding: 0; */
            width: 80px;
            font-size: 20px;
            border-radius: 5px;
            border: none;
            background: #B3D33F;
            color: #fff;
        }
    </style>

    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
</head>
<body>
<ul id="content"></ul>
<form class="form">
<input type="text" placeholder="请输入发送的消息" class="message" id="message"/>
<input type="button" value="发送" id="send" class="connect"/>
<input type="button" value="连接" id="connect" class="connect"/>
</form>
<script>

    var oUl=document.getElementById('content');
    var oConnect=document.getElementById('connect');
    var oSend=document.getElementById('send');
    var oInput=document.getElementById('message');
    var ws=null;
    oConnect.onclick=function(){
        ws=new WebSocket('ws://127.0.0.1:10083');
         ws.onopen=function(){
             oUl.innerHTML+="<li>客户端已连接</li>";
         }

        ws.onmessage=function(evt){
            console.log("fdsa")
            oUl.innerHTML+="<li>"+evt.data+"</li>";
        }
        ws.onclose=function(){
            oUl.innerHTML+="<li>客户端已断开连接</li>";
        };
        ws.onerror=function(evt){
            oUl.innerHTML+="<li>"+evt.data+"</li>";

        };
    };
    oSend.onclick=function(){
        if(ws){
             ws.send($("#message").val())
        }
    }

</script>
</body>
</html>

效果

image.png image.png

这版有些问题

  • 客户端断开,服务端就挂了
  • 单线程.....

改进版

import socket
import struct
import hashlib
import base64
import threading


def get_headers(data):
    headers = {}
    data = str(data, encoding="utf-8")

    header, body = data.split("\r\n\r\n", 1)

    header_list = header.split("\r\n")

    for i in header_list:
        i_list = i.split(":", 1)
        if len(i_list) >= 2:
            headers[i_list[0]] = "".join(i_list[1::]).strip()
        else:
            i_list = i.split(" ", 1)
            if i_list and len(i_list) == 2:
                headers["method"] = i_list[0]
                headers["protocol"] = i_list[1]
    return headers


def parse_payload(payload):
    payload_len = payload[1] & 127
    if payload_len == 126:
        extend_payload_len = payload[2:4]
        mask = payload[4:8]
        decoded = payload[8:]

    elif payload_len == 127:
        extend_payload_len = payload[2:10]
        mask = payload[10:14]
        decoded = payload[14:]
    else:
        extend_payload_len = None
        mask = payload[2:6]
        decoded = payload[6:]

    # 这里我们使用字节将数据全部收集,再去字符串编码,这样不会导致中文乱码
    bytes_list = bytearray()

    for i in range(len(decoded)):
        # 解码方式
        chunk = decoded[i] ^ mask[i % 4]
        bytes_list.append(chunk)
    body = str(bytes_list, encoding='utf-8')
    return body


def send_msg(conn, msg_bytes):
    # 接收的第一字节,一般都是x81不变
    first_byte = b"\x81"
    length = len(msg_bytes)
    if length < 126:
        first_byte += struct.pack("B", length)
    elif length <= 0xFFFF:
        first_byte += struct.pack("!BH", 126, length)
    else:
        first_byte += struct.pack("!BQ", 127, length)

    msg = first_byte + msg_bytes
    conn.sendall(msg)
    return True

sock_pool = []


def handler_accept(sock):

    while True:
        conn, addr = sock.accept()

        data = conn.recv(8096)
        headers = get_headers(data)
        # 对请求头中的sec-websocket-key进行加密
        response_tpl = "HTTP/1.1 101 Switching Protocols\r\n" \
                       "Upgrade:websocket\r\n" \
                       "Connection: Upgrade\r\n" \
                       "Sec-WebSocket-Accept: %s\r\n" \
                       "WebSocket-Location: ws://%s\r\n\r\n"

        # 第一次连接发回报文
        magic_string = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
        if headers.get('Sec-WebSocket-Key'):
            value = headers['Sec-WebSocket-Key'] + magic_string

        ac = base64.b64encode(hashlib.sha1(value.encode('utf-8')).digest())
        response_str = response_tpl % (ac.decode('utf-8'), headers.get("Host"))
        conn.sendall(bytes(response_str, encoding="utf-8"))
        t = threading.Thread(target=handler_msg, args=(conn, ))
        t.start()


def handler_msg(conn):
        with conn as c:
            while True:
                data_recv = c.recv(8096)

                if data_recv[0:1] == b"\x81":
                    data_parse = parse_payload(data_recv)
                    print(data_parse)
                send_msg(c, bytes("recv: {}".format(data_parse), encoding="utf-8"))


def server_socket():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind(("127.0.0.1", 10083))
    sock.listen(5)
    t = threading.Thread(target=handler_accept(sock))
    t.start()
        # data = conn.recv(8096)
        # headers = get_headers(data)
        #
        # # 对请求头中的sec-websocket-key进行加密
        # response_tpl = "HTTP/1.1 101 Switching Protocols\r\n" \
        #                "Upgrade:websocket\r\n" \
        #                "Connection: Upgrade\r\n" \
        #                "Sec-WebSocket-Accept: %s\r\n" \
        #                "WebSocket-Location: ws://%s\r\n\r\n"
        #
        # # 第一次连接发回报文
        # magic_string = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
        # if headers.get('Sec-WebSocket-Key'):
        #     value = headers['Sec-WebSocket-Key'] + magic_string
        #
        # ac = base64.b64encode(hashlib.sha1(value.encode('utf-8')).digest())
        # response_str = response_tpl % (ac.decode('utf-8'), headers.get("Host"))
        # conn.sendall(bytes(response_str, encoding="utf-8"))


if __name__ == "__main__":
    server_socket()

参考

websocket例子
https://www.cnblogs.com/ssyfj/p/9245150.html

websocket 理论理解的例子
https://www.cnblogs.com/IT-Scavenger/p/9435405.html

一个不错的例子
https://blog.csdn.net/qq_39687901/article/details/81531101

tcp理解
https://mp.weixin.qq.com/s?__biz=MzAwNjY3MjgzOA==&mid=2477610578&idx=1&sn=edf68d8c87215fe30549d432ce14a6e9&chksm=8d502efbba27a7ed31bb916acf27d7e107a97f2d019b19cd7e797fa6dba0a1ac3a0ceb200f9b&token=1027437335&lang=zh_CN#rd

相关文章

  • python版websocket

    背景 目前看到实现websocket的框架的- django的channels- flask- sanic好奇,用...

  • Python WebSocket Client

    当WebSocket服务搭好之后,如何用Python调试呢? Python的WebSocket客户端有很多,这里推...

  • 2019-01-17

    python websocket 应用总结 首先要了解websocket原理,知乎上有很多大神的答复,多看看就了解...

  • 2019-03-18

    python stomp订阅websocket 这两天被这个搞的头大,一般的订阅websocket都是没问题的,这...

  • Java WebSocket Security 加密通信实践

    最近在项目中用到了WebSocket通信的内容 第一版的实现方案:spring-websocket + nv-we...

  • websocket实现实时直播(滑鸡版)

    websocket实现实时直播 作为一名web开发者,我使用websocket实现实时直播(滑鸡版)。为什么是滑鸡...

  • little problem

    1. Python 2.0版本 : print"Hello, Python!" Python 3.0版本 : ...

  • javascript 与python 删除重复数组对象

    javascript 版 python 版

  • Python websocket 实践

    正常http就够了,但是若是需要双向通信的话,websocket就会更加简单些。下面介绍python下的一些实践。...

  • python之websocket

    一、websocket WebSocket协议是基于TCP的一种新的协议。WebSocket最初在HTML5规范...

网友评论

      本文标题:python版websocket

      本文链接:https://www.haomeiwen.com/subject/cmuuzqtx.html