4445| 1
|
[高级教程] 原始构建物联网系列之使用掌控板构建网页服务器 |
掌控板构建服务器解决问题:用户通过手机或浏览器访问由掌控板构建服务器的指定页面,可选择需要查看的掌控板环境选项,提交后返回具体选项信息。 碰到问题:手机web页面显示正常,浏览器页面显示乱码。 解决方案:无论怎样修改掌控板服务端字符编码,都是utf-8输出方案,最后只好修改网页字符集为utf-8,实现匹配。 构建服务端的意义:若服务端放在互联网中则在网络的任何一个终端都可以查看掌控板周边所处环境及启动周边设备,即使处于局域网中亦可以构建反弹机制同样实现掌控板周边环境的操控。 代码及效果如后所示: [mw_shl_code=python,true]import network import socket from mpython import * oled.fill(0) oled.show() wlan=network.WLAN(network.STA_IF) serverListenSocket=None port=7513 httpHead=b'HTTP/1.0 200 OK\r\n' httpHeadEnd=b'\r\n' #httpHtmlContent=b"<html><head><title>this is mPython testhtml</title><META http-equiv=\"Content-Type\" content=\"text/html; charset=GBK\"></head><body><form method=\"GET\" action=\"index.jsp\">显示项目:<input type=\"radio\" name=\"xsxm\" value=\"light\" %s>亮度<input type=\"radio\" name=\"xsxm\" value=\"sound\" %s>响度<input type=\"submit\" name=\"tjan\" value=\"确定\"><BR>" httpHtmlContent=b"<html lang=\"zh-CN\"><head><meta charset=\"utf-8\"><title>this is mPython testhtml</title></head><body><form method=\"GET\" action=\"index.jsp\">显示项目:<input type=\"radio\" name=\"xsxm\" value=\"light\" %s>亮度<input type=\"radio\" name=\"xsxm\" value=\"sound\" %s>响度<input type=\"submit\" name=\"tjan\" value=\"确定\"><BR>" #print(httpHtmlContent.encode("utf-8")) httpC=httpHead.decode()+httpHeadEnd.decode()+httpHtmlContent.decode() #print(httpC.encode("utf-8")) #print(httpC.encode("GBK")) try: wlan.active(True) wlan.connect("jsz","jsz123123") print('connecting to network...') while(wlan.ifconfig()[0]=='0.0.0.0'): time.sleep_ms(500) print('.',end="") print('WiFi Connection Successful,Network Config:%s' %str(wlan.ifconfig())) #掌控板连接路由器结束 ip= wlan.ifconfig()[0] serverListenSocket = socket.socket() serverListenSocket.bind((ip,port)) serverListenSocket.listen(5) serverListenSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) print ('http waiting...') oled.DispChar("%s:%s" %(ip,port),0,0) oled.DispChar('accepting.....',0,16) oled.show() while True: clientConnSocket,clientSocketAddr = serverListenSocket.accept() print("Client address:", clientSocketAddr) print("Client socket:", clientConnSocket) req = clientConnSocket.recv(4096) print("Request:") print(req) reqs=bytes.decode(req) if(reqs.find("GET / ")>=0 or reqs.find("GET /index.jsp")>=0): xmxxlightchecked="" xmxxsoundchecked="" if(reqs.find("xsxm=light")>=0): xmxxlightchecked="Checked" httpC=httpC+bytes.decode(b"<BR>亮度:"+str(light.read())) print(b"<BR>亮度:") elif(reqs.find("xsxm=sound")>=0): xmxxsoundchecked="Checked" httpC=httpC+bytes.decode(b"<BR>响度:"+str(sound.read())) clientConnSocket.send(httpC.encode("GBK")%(xmxxlightchecked,xmxxsoundchecked)) else: clientConnSocket.send(httpHead.decode()+httpHeadEnd.decode()+"this isn't valid req") clientConnSocket.close() except: print("except") oled.fill(0) oled.DispChar("except",0,0) oled.show() if(serverListenSocket): serverListenSocket.close() wlan.disconnect() [/mw_shl_code] 有需要的同仁可以添加公众号linkallStudio获取详细资料。 亦可扫描: 关注公众号。 |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed