首先,按照《http://www.gaohaiyan.com/3771.html#5.模块的 webrepl 设置》中开启了webrepl。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
from websocket import create_connection # pip3 install websocket # pip3 install websocket-client def init(url, pswd): try: ws = create_connection(url=url, timeout=3) # 创建连接。超时s。 ws.recv() # 访问模块 ws.send(pswd + '\r\n') # 输入密码 ans = getAnswer(ws) # 等待响应 if 'err' == ans: return None return ws except BaseException as e: print('错误', e.args) return None def chat(ws): code = input("code:") if 'over' == code: # 断开指令 ws.close() # 断开连接 return cs = [] while '-o' != code: # 代码结束指令 cs.append(code) code = input("code:") for c in cs: if '-o' != c: ws.send(c + '\r\n') ans = getAnswer(ws) print(ans) chat(ws) def getAnswer(ws): answer = '' prompt = '>>>' # 模块正常响应后的返回字符 while prompt not in answer: try: answer += ws.recv() except BaseException as e: print('意外', e.args) return 'err' return answer if '__main__' == __name__: ws = init('ws://192.168.2.5:8266', '123456') if ws: chat(ws) |
- end
声明
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/3842.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设