zoukankan      html  css  js  c++  java
  • [Python]通过websocket与jsclient通信

    站点大多使用HTTP协议通信。而HTTP是无连接的协议。仅仅有client请求时,server端才干发出对应的应答。HTTP请求的包也比較大,假设仅仅是非常小的数据通信。开销过大。于是,我们能够使用websocket这个协议,用最小的开销实现面向连接的通信。

    详细的websocket介绍可见http://zh.wikipedia.org/wiki/WebSocket 

    这里,介绍怎样使用Python与前端js进行通信。

    websocket使用HTTP协议完毕握手之后,不通过HTTP直接进行websocket通信。

    于是,使用websocket大致两个步骤:使用HTTP握手,通信。

    js处理websocket要使用ws模块;Python处理则使用socket模块建立TCP连接就可以,比一般的socket,仅仅多一个握手以及数据处理的步骤。

    握手


    过程

     

    包格式

    jsclient先向server端python发送握手包,格式例如以下:

    GET /chat HTTP/1.1
    Host: server.example.com
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
    Origin: http://example.com
    Sec-WebSocket-Protocol: chat, superchat
    Sec-WebSocket-Version: 13

    server回应包格式:

    HTTP/1.1 101 Switching Protocols
    Upgrade: websocket
    Connection: Upgrade
    Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
    Sec-WebSocket-Protocol: chat

    当中,Sec-WebSocket-Key是随机的,server用这些数据构造一个SHA-1信息摘要。

    方法为:key+migicSHA-1 加密,base-64加密,例如以下:

     

    Python中的处理代码

    MAGIC_STRING = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
    res_key = base64.b64encode(hashlib.sha1(sec_key + MAGIC_STRING).digest())

    握手完整代码

    js

    js中有处理websocket的类,初始化后自己主动发送握手包。例如以下:

    var socket = new WebSocket('ws://localhost:3368'); 

    Python

    Pythonsocket接受得到握手字符串,处理后发送

    HOST = 'localhost'
    PORT = 3368
    MAGIC_STRING = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
    HANDSHAKE_STRING = "HTTP/1.1 101 Switching Protocols
    " 
                       "Upgrade:websocket
    " 
                       "Connection: Upgrade
    " 
                       "Sec-WebSocket-Accept: {1}
    " 
                       "WebSocket-Location: ws://{2}/chat
    " 
                       "WebSocket-Protocol:chat
    
    "
     
    def handshake(con):
    #con为用socket,accept()得到的socket
    #这里省略监听。accept的代码,详细可见blog:http://blog.csdn.net/ice110956/article/details/29830627
        headers = {}
        shake = con.recv(1024)
     
        if not len(shake):
            return False
     
        header, data = shake.split('
    
    ', 1)
        for line in header.split('
    ')[1:]:
            key, val = line.split(': ', 1)
            headers[key] = val
     
        if 'Sec-WebSocket-Key' not in headers:
            print ('This socket is not websocket, client close.')
            con.close()
            return False
     
        sec_key = headers['Sec-WebSocket-Key']
        res_key = base64.b64encode(hashlib.sha1(sec_key + MAGIC_STRING).digest())
     
        str_handshake = HANDSHAKE_STRING.replace('{1}', res_key).replace('{2}', HOST + ':' + str(PORT))
        print str_handshake
        con.send(str_handshake)
    return True

    通信

    不同版本号的浏览器定义的数据帧格式不同,Python发送和接收时都要处理得到符合格式的数据包,才干通信。

    Python接收

    Python接收到浏览器发来的数据。要解析后才干得到当中的实用数据。

    浏览器包格式

     

    固定字节:

    1000 0001或是1000 0002)这里没用,忽略

    包长度字节:

    第一位肯定是1。忽略。剩下7个位能够得到一个整数(0 ~ 127),当中

    1-125)表此字节为长度字节。大小即为长度;

    (126)表接下来的两个字节才是长度;

    (127)表接下来的八个字节才是长度;

    用这样的变长的方式表示数据长度,节省数据位。

    mark掩码:

    mark掩码为包长之后的4个字节,之后的兄弟数据要与mark掩码做运算才干得到真实的数据。

    兄弟数据:

    得到真实数据的方法:将兄弟数据的每一位x,和掩码的第i%4位做xor运算,当中ix在兄弟数据中的索引。

    完整代码

    def recv_data(self, num):
        try:
            all_data = self.con.recv(num)
            if not len(all_data):
                return False
        except:
            return False
        else:
            code_len = ord(all_data[1]) & 127
            if code_len == 126:
                masks = all_data[4:8]
                data = all_data[8:]
            elif code_len == 127:
                masks = all_data[10:14]
                data = all_data[14:]
            else:
                masks = all_data[2:6]
                data = all_data[6:]
            raw_str = ""
            i = 0
            for d in data:
                raw_str += chr(ord(d) ^ ord(masks[i % 4]))
                i += 1
            return raw_str

    js端的ws对象,通过ws.send(str)就可以发送

    ws.send(str)

    Python发送

    Python要包数据发送,也须要处理。发送包格式例如以下

     

    固定字节:固定的1000 0001(x81)

    包长:依据发送数据长度是否超过1250xFFFF(65535)来生成1个或3个或9个字节,来代表数据长度。

    def send_data(self, data):
        if data:
            data = str(data)
        else:
            return False
        token = "x81"
        length = len(data)
        if length < 126:
            token += struct.pack("B", length)
        elif length <= 0xFFFF:
            token += struct.pack("!BH", 126, length)
        else:
            token += struct.pack("!BQ", 127, length)
        #struct为Python中处理二进制数的模块,二进制流为C。或网络流的形式。

    data = '%s%s' % (token, data) self.con.send(data) return True

    js端通过回调函数ws.onmessage()接受数据

    ws.onmessage = function(result,nTime){
    alert("从服务端收到的数据:");
    alert("近期一次发送数据到如今接收一共使用时间:" + nTime);
    console.log(result);
    }

    终于代码


    Python服务端

    # _*_ coding:utf-8 _*_
    __author__ = 'Patrick'
     
     
    import socket
    import threading
    import sys
    import os
    import MySQLdb
    import base64
    import hashlib
    import struct
     
    # ====== config ======
    HOST = 'localhost'
    PORT = 3368
    MAGIC_STRING = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
    HANDSHAKE_STRING = "HTTP/1.1 101 Switching Protocols
    " 
                       "Upgrade:websocket
    " 
                       "Connection: Upgrade
    " 
                       "Sec-WebSocket-Accept: {1}
    " 
                       "WebSocket-Location: ws://{2}/chat
    " 
                       "WebSocket-Protocol:chat
    
    "
     
    class Th(threading.Thread):
        def __init__(self, connection,):
            threading.Thread.__init__(self)
            self.con = connection
     
        def run(self):
            while True:
                try:
                   pass
            self.con.close()
     
        def recv_data(self, num):
            try:
                all_data = self.con.recv(num)
                if not len(all_data):
                    return False
            except:
                return False
            else:
                code_len = ord(all_data[1]) & 127
                if code_len == 126:
                    masks = all_data[4:8]
                    data = all_data[8:]
                elif code_len == 127:
                    masks = all_data[10:14]
                    data = all_data[14:]
                else:
                    masks = all_data[2:6]
                    data = all_data[6:]
                raw_str = ""
                i = 0
                for d in data:
                    raw_str += chr(ord(d) ^ ord(masks[i % 4]))
                    i += 1
                return raw_str
     
        # send data
        def send_data(self, data):
            if data:
                data = str(data)
            else:
                return False
            token = "x81"
            length = len(data)
            if length < 126:
                token += struct.pack("B", length)
            elif length <= 0xFFFF:
                token += struct.pack("!BH", 126, length)
            else:
                token += struct.pack("!BQ", 127, length)
            #struct为Python中处理二进制数的模块,二进制流为C,或网络流的形式。

    data = '%s%s' % (token, data) self.con.send(data) return True # handshake def handshake(con): headers = {} shake = con.recv(1024) if not len(shake): return False header, data = shake.split(' ', 1) for line in header.split(' ')[1:]: key, val = line.split(': ', 1) headers[key] = val if 'Sec-WebSocket-Key' not in headers: print ('This socket is not websocket, client close.') con.close() return False sec_key = headers['Sec-WebSocket-Key'] res_key = base64.b64encode(hashlib.sha1(sec_key + MAGIC_STRING).digest()) str_handshake = HANDSHAKE_STRING.replace('{1}', res_key).replace('{2}', HOST + ':' + str(PORT)) print str_handshake con.send(str_handshake) return True def new_service(): """start a service socket and listen when coms a connection, start a new thread to handle it""" sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: sock.bind(('localhost', 3368)) sock.listen(1000) #链接队列大小 print "bind 3368,ready to use" except: print("Server is already running,quit") sys.exit() while True: connection, address = sock.accept() #返回元组(socket,add),accept调用时会进入waite状态 print "Got connection from ", address if handshake(connection): print "handshake success" try: t = Th(connection, layout) t.start() print 'new thread for client ...' except: print 'start new thread error' connection.close() if __name__ == '__main__': new_service()

    js客户

    <script>
    var socket = new WebSocket('ws://localhost:3368');
    ws.onmessage = function(result,nTime){
    alert("从服务端收到的数据:");
    alert("近期一次发送数据到如今接收一共使用时间:" + nTime);
    console.log(result);
    }
    </script>

    推荐blog

    http://blog.csdn.net/fenglibing/article/details/7108982 

    http://blog.mycolorway.com/2011/11/22/a-minimal-python-websocket-server/ 

  • 相关阅读:
    LayoutInflater(布局服务)
    FOTA升级
    APK安装过程及原理详解
    Context类型
    Android应用的persistent属性
    Notification(状态栏通知)详解
    Handler消息传递机制浅析
    Selenium HTMLTestRunner 无法生成测试报告的总结
    【python】远程使用rsa登录sftp,上传下载文件
    02.性能测试中的指标
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/6881380.html
Copyright © 2011-2022 走看看