websocket

websocket,第1张

概述      WebSocket协议是基于TCP的一种新的协议。WebSocket最初在HTML5规范中被引用为TCP连接,作为基于TCP的套接字API的占位符。它实现了浏览器与服务器全双工(full-duplex)通信。其本质是保持TCP连接,在浏览器和服务端通过Socket进行通信。  本文将使用Python编写Socket服务端,一步一步分析请求过程!!! 1. 启动服务端 ? 1 2 3 4  

    WebSocket协议是基于TCP的一种新的协议。WebSocket最初在HTML5规范中被引用为TCP连接,作为基于TCP的套接字API的占位符。它实现了浏览器与服务器全双工(full-duplex)通信。其本质是保持TCP连接,在浏览器和服务端通过Socket进行通信。

 本文将使用Python编写Socket服务端,一步一步分析请求过程!!!

1. 启动服务端 ?
1 2 3 4 5 6 7 8 9 10 import 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‘ , 8002 )) sock.Listen( 5 ) # 等待用户连接 conn,address = sock.accept() ... ... ...

启动Socket服务器后,等待用户【连接】,然后进行收发数据。

2. 客户端连接 ?
1 2 3 4 <script type = "text/JavaScript" >      var socket = new WebSocket( "ws://127.0.0.1:8002/xxoo" );      ... < / script>

当客户端向服务端发送连接请求时,不仅连接还会发送【握手】信息,并等待服务端响应,至此连接才创建成功!

3. 建立连接【握手】 ?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import socket   sock = socket.socket(socket.AF_INET,socket.soCK_STREAM) sock.setsockopt(socket.soL_SOCKET, 1 ) sock.bind(( ‘127.0.0.1‘ , 8002 )) sock.Listen( 5 ) # 获取客户端socket对象 conn,address = sock.accept() # 获取客户端的【握手】信息 data = conn.recv( 1024 ) ... ... ... conn.send( ‘响应【握手】信息‘ )

请求和响应的【握手】信息需要遵循规则:

从请求【握手】信息中提取 Sec-WebSocket-Key 利用magic_string 和 Sec-WebSocket-Key 进行hmac1加密,再进行base64加密 将加密结果响应给客户端

注:magic string为:258EAFA5-E914-47DA-95CA-C5AB0DC85B11

请求【握手】信息为:

GET /chatsocket http/1.1Host: 127.0.0.1:8002Connection: UpgradePragma: no-cacheCache-Control: no-cacheUpgrade: websocketOrigin: http://localhost:63342Sec-WebSocket-Version: 13Sec-WebSocket-Key: mnwFxiolctXFN/DeMt1Amg==Sec-WebSocket-Extensions: permessage-deflate; clIEnt_max_window_bits
VIEw Code

提取Sec-WebSocket-Key值并加密:

@H_301_439@

import socketimport base64import hashlib def get_headers(data):    """    将请求头格式化成字典    :param data:    :return:    """    header_dict = {}    data = str(data,enCoding=utf-8)     for i in data.split(\r\n):        print(i)    header,body = data.split(\r\n\r\n,1)    header_List = header.split(\r\n)    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  sock = socket.socket(socket.AF_INET,socket.soCK_STREAM)sock.setsockopt(socket.soL_SOCKET,1)sock.bind((127.0.0.1,8002))sock.Listen(5) conn,address = sock.accept()data = conn.recv(1024)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%s\r\n\r\n"magic_string = 258EAFA5-E914-47DA-95CA-C5AB0DC85B11value = headers[Sec-WebSocket-Key] + magic_stringac = base64.b64encode(hashlib.sha1(value.encode(utf-8)).digest())response_str = response_tpl % (ac.decode(utf-8),headers[Host],headers[url])# 响应【握手】信息conn.send(bytes(response_str,enCoding=utf-8))
VIEw Code 4.客户端和服务端收发数据

客户端和服务端传输数据时,需要对数据进行【封包】和【解包】。客户端的JavaScript类库已经封装【封包】和【解包】过程,但Socket服务端需要手动实现。

第一步:获取客户端发送的数据【解包】

   info = conn.recv(8096)    payload_len = info[1] & 127    if payload_len == 126:        extend_payload_len = info[2:4]        mask = info[4:8]        decoded = info[8:]    elif payload_len == 127:        extend_payload_len = info[2:10]        mask = info[10:14]        decoded = info[14:]    else:        extend_payload_len = None        mask = info[2:6]        decoded = info[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)    print(body)
基于Python实现解包过程(未实现长内容)

解包详细过程: 

 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 3 4 5 6 7 8 9 0 1+-+-+-+-+-------+-+-------------+-------------------------------+|F|R|R|R| opcode|M| Payload len |    Extended payload length    ||I|S|S|S|  (4)  |A|     (7)     |             (16/64)           ||N|V|V|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 ...                |+---------------------------------------------------------------+

 

The MASK bit simply tells whether the message is encoded. Messages from the clIEnt must be masked,so your server should expect this to be 1. (In fact, section 5.1 of the spec says that your server must disconnect from a clIEnt if that clIEnt sends an unmasked message.) When sending a frame back to the clIEnt,do not mask it and do not set the mask bit. We‘ll explain masking later. Note: You have to mask messages even when using a secure socket.RSV1-3 can be ignored,they are for extensions.

The opcode fIEld defines how to interpret the payload data: 0x0 for continuation, 0x1 for text (which is always encoded in UTF-8), 0x2 for binary,and other so-called "control codes" that will be discussed later. In this version of WebSockets, 0x3 to 0x7 and 0xB to 0xF have no meaning.

The FIN bit tells whether this is the last message in a serIEs. If it‘s 0,then the server will keep Listening for more parts of the message; otherwise,the server should consIDer the message delivered. More on this later.

DeCoding Payload Length

To read the payload data,you must kNow when to stop reading. That‘s why the payload length is important to kNow. Unfortunately,this is somewhat complicated. To read it,follow these steps:

Read bits 9-15 (inclusive) and interpret that as an unsigned integer. If it‘s 125 or less,then that‘s the length; you‘re done. If it‘s 126,go to step 2. If it‘s 127,go to step 3. Read the next 16 bits and interpret those as an unsigned integer. You‘re done. Read the next 64 bits and interpret those as an unsigned integer (The most significant bit MUST be 0). You‘re done.

Reading and Unmasking the Data

If the MASK bit was set (and it should be,for clIEnt-to-server messages),read the next 4 octets (32 bits); this is the masking key. Once the payload length and masking key is decoded,you can go ahead and read that number of bytes from the socket. Let‘s call the data ENCODED,and the key MASK. To get DECODED,loop through the octets (bytes a.k.a. characters for text data) of ENCODED and XOR the octet with the (i modulo 4)th octet of MASK. In pseudo-code (that happens to be valID JavaScript):

 

var DECODED = "";
for (var i = 0; i < ENCODED.length; i++) {
    DECODED[i] = ENCODED[i] ^ MASK[i % 4];
}

 

Now you can figure out what DECODED means depending on your application.

 第二步:向客户端发送数据【封包】

def send_msg(conn,msg_bytes):    """    WebSocket服务端向客户端发送消息    :param conn: 客户端连接到服务器端的socket对象,即: conn,address = socket.accept()    :param msg_bytes: 向客户端发送的字节    :return:     """    import struct    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.send(msg)    return True
VIEw Code 5. 基于Python实现简单示例

a. 基于Python socket实现的WebSocket服务端:

#!/usr/bin/env python# -*- Coding:utf-8 -*-import socketimport base64import hashlib  def get_headers(data):    """    将请求头格式化成字典    :param data:    :return:    """    header_dict = {}    data = str(data,enCoding=utf-8)     header,1)    header_List = header.split(\r\n)    for i in range(0,len(header_List)):        if i == 0:            if len(header_List[i].split( )) == 3:                header_dict[method],1)            header_dict[k] = v.strip()    return header_dict  def send_msg(conn,address = socket.accept()    :param msg_bytes: 向客户端发送的字节    :return:    """    import struct     token = b"\x81"    length = len(msg_bytes)    if length < 126:        token += struct.pack("B",length)     msg = token + msg_bytes    conn.send(msg)    return True  def run():    sock = socket.socket(socket.AF_INET,socket.soCK_STREAM)    sock.setsockopt(socket.soL_SOCKET,1)    sock.bind((127.0.0.1,8003))    sock.Listen(5)     conn,address = sock.accept()    data = conn.recv(1024)    headers = get_headers(data)    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%s\r\n\r\n"     value = headers[Sec-WebSocket-Key] + 258EAFA5-E914-47DA-95CA-C5AB0DC85B11    ac = base64.b64encode(hashlib.sha1(value.encode(utf-8)).digest())    response_str = response_tpl % (ac.decode(utf-8),headers[url])    conn.send(bytes(response_str,enCoding=utf-8))     while True:        try:            info = conn.recv(8096)        except Exception as e:            info = None        if not info:            break        payload_len = info[1] & 127        if payload_len == 126:            extend_payload_len = info[2:4]            mask = info[4:8]            decoded = info[8:]        elif payload_len == 127:            extend_payload_len = info[2:10]            mask = info[10:14]            decoded = info[14:]        else:            extend_payload_len = None            mask = info[2:6]            decoded = info[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)        send_msg(conn,body.encode(utf-8))     sock.close() if __name__ == __main__:    run()
VIEw Code

b. 利用JavaScript类库实现客户端

<!DOCTYPE HTML><HTML><head lang="en">    <Meta charset="UTF-8">    <Title></Title></head><body>    <div>        <input type="text" ID="txt"/>        <input type="button" ID="btn" value="提交" onclick="sendMsg();"/>        <input type="button" ID="close" value="关闭连接" onclick="closeConn();"/>    </div>    <div ID="content"></div> <script type="text/JavaScript">    var socket = new WebSocket("ws://127.0.0.1:8003/chatsocket");     socket.onopen = function () {        /* 与服务器端连接成功后,自动执行 */         var newTag = document.createElement(div);        newTag.INNERHTML = "【连接成功】";        document.getElementByID(content).appendChild(newTag);    };     socket.onmessage = function (event) {        /* 服务器端向客户端发送数据时,自动执行 */        var response = event.data;        var newTag = document.createElement(div);        newTag.INNERHTML = response;        document.getElementByID(content).appendChild(newTag);    };     socket.onclose = function (event) {        /* 服务器端主动断开连接时,自动执行 */        var newTag = document.createElement(div);        newTag.INNERHTML = "【关闭连接】";        document.getElementByID(content).appendChild(newTag);    };     function sendMsg() {        var txt = document.getElementByID(txt);        socket.send(txt.value);        txt.value = "";    }    function closeConn() {        socket.close();        var newTag = document.createElement(div);        newTag.INNERHTML = "【关闭连接】";        document.getElementByID(content).appendChild(newTag);    } </script></body></HTML>
VIEw Code 6. 基于Tornado框架实现Web聊天室

Tornado是一个支持WebSocket的优秀框架,其内部原理正如1~5步骤描述,当然Tornado内部封装功能更加完整。

以下是基于Tornado实现的聊天室示例:

#!/usr/bin/env python# -*- Coding:utf-8 -*-import uuIDimport Jsonimport tornado.ioloopimport tornado.webimport tornado.websocketclass IndexHandler(tornado.web.RequestHandler):    def get(self):        self.render(index.HTML)class ChatHandler(tornado.websocket.WebSocketHandler):    # 用户存储当前聊天室用户    waiters = set()    # 用于存储历时消息    messages = []    def open(self):        """        客户端连接成功时,自动执行        :return:         """        ChatHandler.waiters.add(self)        uID = str(uuID.uuID4())        self.write_message(uID)        for msg in ChatHandler.messages:            content = self.render_string(message.HTML,**msg)            self.write_message(content)    def on_message(self,message):        """        客户端连发送消息时,自动执行        :param message:         :return:         """        msg = Json.loads(message)        ChatHandler.messages.append(message)        for clIEnt in ChatHandler.waiters:            content = clIEnt.render_string(message.HTML,**msg)            clIEnt.write_message(content)    def on_close(self):        """        客户端关闭连接时,,自动执行        :return:         """        ChatHandler.waiters.remove(self)def run():    settings = {        template_path: templates,static_path: static,}    application = tornado.web.Application([        (r"/",IndexHandler),(r"/chat",ChatHandler),],**settings)    application.Listen(8888)    tornado.ioloop.Ioloop.instance().start()if __name__ == "__main__":    run()
app.py

<!DOCTYPE HTML><HTML lang="en"><head>    <Meta charset="UTF-8">    <Title>Python聊天室</Title></head><body>    <div>        <input type="text" ID="txt"/>        <input type="button" ID="btn" value="提交" onclick="sendMsg();"/>        <input type="button" ID="close" value="关闭连接" onclick="closeConn();"/>    </div>    <div ID="container" style="border: 1px solID #dddddd;margin: 20px;min-height: 500px;">    </div>    <script src="/static/jquery-2.1.4.min.Js"></script>    <script type="text/JavaScript">        $(function () {            wsUpdater.start();        });        var wsUpdater = {            socket: null,uID: null,start: function() {                var url = "ws://127.0.0.1:8888/chat";                wsUpdater.socket = new WebSocket(url);                wsUpdater.socket.onmessage = function(event) {                    console.log(event);                    if(wsUpdater.uID){                        wsUpdater.showMessage(event.data);                    }else{                        wsUpdater.uID = event.data;                    }                }            },showMessage: function(content) {                $(#container).append(content);            }        };        function sendMsg() {            var msg = {                uID: wsUpdater.uID,message: $("#txt").val()            };            wsUpdater.socket.send(JsON.stringify(msg));        }</script></body></HTML>
index.HTML

示例源码下载

 

参考文献:https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers 

时间:2019-09-08 13:39:26 阅读(8) 最新文章 更多 1 获取请求url中的参数 2019-09-13 2 Django项目: 项目环境搭建 ---- 三、在码云平台创建项目&推送到码云上 2019-09-13 3 Cookie自动管理 2019-09-13 4 【靶场训练_DVWA】Command Execution 2019-09-13 5 python读取excel文件 2019-09-13 6 Jvm Stack栈(五) 2019-09-13 7 201709-3 JSON查询 2019-09-13 8 es6 面向对象选项卡(自动轮播功能) 2019-09-13 9 CSS 导入-选择器-权重 2019-09-13 10 JavaScript base64 加密解密 2019-09-13 最新资讯 更多 1 美国男子失踪22年,在谷歌地球上被找到了 2019-09-13 2 银行传统核心数据库首次国产化:采用腾讯云TDSQL承载核心业务数据 2019-09-13 3 未来几天内两颗小行星将掠过地球 但不会对我们的星球构成威胁 2019-09-13 4 苹果针对儿童类应用更新App Store应用商店审查指南 2019-09-13 5 运行深度Linux的华为MateBook笔记本电脑现身 2019-09-13 6 汇顶科技第一期员工持股计划完成股票购买 成交均价179.4元 2019-09-13 7 舒马赫已恢复意识!干细胞治疗创造再生奇迹 2019-09-13 8 古代“摩德纳情侣”遗骸被证明属于两位男性 2019-09-13 9 月球内部可能蕴含“白金宝藏” 2019-09-13 10 ESB 走上灭绝的道路:编程语言 Ballerina 重塑云原生中间件 2019-09-13 联系我们 - 留言反馈 © 2017 版权所有 鲁ICP备17052893号 总结

以上是内存溢出为你收集整理的websocket全部内容,希望文章能够帮你解决websocket所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1035713.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存