esp8266开发——使用micropython下的websocket

esp8266开发——使用micropython下的websocket

说明

  • micropython的标准库中包含了websocket的代码,但是我没有测试
  • 我直接修改了webrepl的部分关于websocket的代码,原版的代码里面是无法让chrome作为客户端连接的(在数据解包部分几乎没有实现)
  • 仍然有很多websocket标准,比如分包传输等没有实现

代码

  • websocket_helper.py 这个握手协议实现得非常简单
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
try:
    import ubinascii as binascii
except:
    import binascii
try:
    import uhashlib as hashlib
except:
    import hashlib
    
DEBUG = 0

def server_handshake(sock):
    clr = sock.makefile("rwb", 0)
    l = clr.readline()
    #sys.stdout.write(repr(l))
    webkey = None
    while 1:
        l = clr.readline()
        if not l:
            raise OSError("EOF in headers")
        if l == b"\r\n":
            break
    #    sys.stdout.write(l)
        h, v = [x.strip() for x in l.split(b":", 1)]
        if DEBUG:
            print((h, v))
        if h == b'Sec-WebSocket-Key':
            webkey = v
    if not webkey:
        raise OSError("Not a websocket request")
    if DEBUG:
        print("Sec-WebSocket-Key:", webkey, len(webkey))
    respkey = webkey + b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
    respkey = hashlib.sha1(respkey).digest()
    respkey = binascii.b2a_base64(respkey)[:-1]
    resp = b"""\
HTTP/1.1 101 Switching Protocols\r
Upgrade: websocket\r
Connection: Upgrade\r
Sec-WebSocket-Accept: %s\r
\r
""" % respkey
    if DEBUG:
        print(resp)
    sock.send(resp)
# Very simplified client handshake, works for MicroPython's
# websocket server implementation, but probably not for other
# servers.
def client_handshake(sock):
    cl = sock.makefile("rwb", 0)
    cl.write(b"""\
GET / HTTP/1.1\r
Host: echo.websocket.org\r
Connection: Upgrade\r
Upgrade: websocket\r
Sec-WebSocket-Key: foo\r
\r
""")
    l = cl.readline()
#    print(l)
    while 1:
        l = cl.readline()
        if l == b"\r\n":
            break
#        sys.stdout.write(l)
  • mywebsocket.py 这个主要是建立socket监听、接收websocket的数据,解码的操作。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import socket
import websocket_helper
try:
    import network
except:
    pass
import sys
import os
try:
    import ustruct as struct
except:
    import struct
    
DEBUG = False

class websocket:

    def __init__(self, s):
        self.s = s
        
    def write(self, data):
        l = len(data)
        if l < 126:
            # TODO: hardcoded "binary" type
            hdr = struct.pack(">BB", 0x82, l)
        else:
            hdr = struct.pack(">BBH", 0x82, 126, l)
        self.s.send(hdr)
        self.s.send(data)

    def recvexactly(self, sz):
        res = b""
        while sz:
            data = self.s.recv(sz)
            if not data:
                break
            res += data
            sz -= len(data)
        return res

    def read(self):
        while True:
            hdr = self.recvexactly(2)
            assert len(hdr) == 2
            firstbyte, secondbyte = struct.unpack(">BB", hdr)

            mskenable =  True if secondbyte & 0x80 else False
            length = secondbyte & 0x7f
            if DEBUG:
                print('test length=%d' % length)
                print('mskenable=' + str(mskenable))
            if length == 126:
                hdr = self.recvexactly(2)
                assert len(hdr) == 2
                (length,) = struct.unpack(">H", hdr)
            if length == 127:
                hdr = self.recvexactly(8)
                assert len(hdr) == 8
                (length,) = struct.unpack(">Q", hdr)
            if DEBUG:
                print('length=%d' % length)
            opcode =  firstbyte & 0x0f
            if opcode == 8:
                self.s.close()
                return ''
            fin = True if firstbyte&0x80 else False
            if DEBUG:
                print('fin='+str(fin))
                print('opcode=%d'%opcode)
            if mskenable:
                hdr = self.recvexactly(4)
                assert len(hdr) == 4
                (msk1,msk2,msk3,msk4) = struct.unpack(">BBBB", hdr)
                msk = [msk1,msk2,msk3,msk4]
            #print('msk'+str(msk))
            # debugmsg("Got unexpected websocket record of type %x, skipping it" % fl)
            data = []
            while length:
                skip = self.s.recv(length)
                # debugmsg("Skip data: %s" % skip)
                length -= len(skip)
                data.extend(skip)
            newdata = []
            #解码数据
            for i,item in enumerate(data):
                j = i % 4
                newdata.append(chr(data[i] ^ msk[j]))
            res = ''.join(newdata)
            return res
print('my prog start...')
try:
    sta_if = network.WLAN(network.STA_IF)
    sta_if.active(True)
    sta_if.connect('Xiechuang-technology_5G','xc88888888')
    while True:
      if sta_if.ifconfig()[0] != '0.0.0.0':
        break
    print('succ connect wifi ap,get ipaddr:')
    print(sta_if.ifconfig())
except:
    pass
sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('0.0.0.0', 8008))
sock.listen(5)
print('websokcet listen at 8008...')
while True:
    # 这里阻塞接收客户端
    conn, address = sock.accept()
    # 接收到socket
    print('client connect...:')
    print(address)
    websocket_helper.server_handshake(conn)
    ws = websocket(conn)
    print('websocket connect succ')
    # conn.send('hello friend')
    while True:
        text = ws.read()
        if text =='':
            break
        print(text)

你可能感兴趣的:(micropython,esp8266,micropython)