Some code to learn


import network

sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.scan()
sta_if.connect("los_tres_ositos", "VQLzxuGs1")
sta_if.isconnected()

from microWebSrv import MicroWebSrv
# ----------------------------------------------------------------------------
@MicroWebSrv.route('/test')
def _httpHandlerTestGet(httpClient, httpResponse) :

@MicroWebSrv.route('/test', 'POST')
def _httpHandlerTestPost(httpClient, httpResponse) :

@MicroWebSrv.route('/ggerman')
def _httpHandlerGgermanGet(httpClient, httpResponse) :

@MicroWebSrv.route('/ggerman', 'POST')
def _httpHandlerGgermanPost(httpClient, httpResponse) :

@MicroWebSrv.route('/edit/') # /edit/123 -> args['index']=123
@MicroWebSrv.route('/edit//abc/') # /edit/123/abc/bar -> args['index']=123 args['foo']='bar'
@MicroWebSrv.route('/edit') # /edit -> args={}
def _httpHandlerEditWithArgs(httpClient, httpResponse, args={}) :

def _acceptWebSocketCallback(webSocket, httpClient) :
print("WS ACCEPT")
webSocket.RecvTextCallback = _recvTextCallback
webSocket.RecvBinaryCallback = _recvBinaryCallback
webSocket.ClosedCallback = _closedCallback

def _recvTextCallback(webSocket, msg) :
print("WS RECV TEXT : %s" % msg)
webSocket.SendText("Reply for %s" % msg)

def _recvBinaryCallback(webSocket, data) :
print("WS RECV DATA : %s" % data)

def _closedCallback(webSocket) :
print("WS CLOSED")
# ----------------------------------------------------------------------------
#routeHandlers = [
# ( "/test", "GET", _httpHandlerTestGet ),
# ( "/test", "POST", _httpHandlerTestPost )
#]
srv = MicroWebSrv(webPath='www/')
srv.MaxWebSocketRecvLen = 256
srv.WebSocketThreaded = False
srv.AcceptWebSocketCallback = _acceptWebSocketCallback
srv.Start(threaded=False)

Complete

import network

sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.scan()
sta_if.connect("los_tres_ositos", "VQLzxuGs1")
sta_if.isconnected()

from microWebSrv import MicroWebSrv
# ----------------------------------------------------------------------------
@MicroWebSrv.route('/test')
def _httpHandlerTestGet(httpClient, httpResponse) :
content = """\

TEST GET

TEST GET

Client IP address = %s

First name:
Last name:

""" % httpClient.GetIPAddr()
httpResponse.WriteResponseOk( headers = None,
contentType = "text/html",
contentCharset = "UTF-8",
content = content )

@MicroWebSrv.route('/test', 'POST')
def _httpHandlerTestPost(httpClient, httpResponse) :
formData = httpClient.ReadRequestPostedFormData()
firstname = formData["firstname"]
lastname = formData["lastname"]
content = """\

TEST POST

TEST POST

Firstname = %s
Lastname = %s

""" % ( MicroWebSrv.HTMLEscape(firstname),
MicroWebSrv.HTMLEscape(lastname) )
httpResponse.WriteResponseOk( headers = None,
contentType = "text/html",
contentCharset = "UTF-8",
content = content )

@MicroWebSrv.route('/ggerman')
def _httpHandlerGgermanGet(httpClient, httpResponse) :
content = """\

ggerman

ggerman

Client IP address = %s

First name:
Last name:

""" % httpClient.GetIPAddr()
httpResponse.WriteResponseOk( headers = None,
contentType = "text/html",
contentCharset = "UTF-8",
content = content )

@MicroWebSrv.route('/ggerman', 'POST')
def _httpHandlerGgermanPost(httpClient, httpResponse) :
formData = httpClient.ReadRequestPostedFormData()
firstname = formData["firstname"]
lastname = formData["lastname"]
content = """\

ggerman

ggerman

Firstname = %s
Lastname = %s

""" % ( MicroWebSrv.HTMLEscape(firstname),
MicroWebSrv.HTMLEscape(lastname) )
httpResponse.WriteResponseOk( headers = None,
contentType = "text/html",
contentCharset = "UTF-8",
content = content )

@MicroWebSrv.route('/edit/') # /edit/123 -> args['index']=123
@MicroWebSrv.route('/edit//abc/') # /edit/123/abc/bar -> args['index']=123 args['foo']='bar'
@MicroWebSrv.route('/edit') # /edit -> args={}
def _httpHandlerEditWithArgs(httpClient, httpResponse, args={}) :
content = """\

TEST EDIT

"""
content += "

EDIT item with {} variable arguments

"\
.format(len(args))

if 'index' in args :
content += "

index = {}

".format(args['index'])

if 'foo' in args :
content += "

foo = {}

".format(args['foo'])

content += """

"""
httpResponse.WriteResponseOk( headers = None,
contentType = "text/html",
contentCharset = "UTF-8",
content = content )

# ----------------------------------------------------------------------------

def _acceptWebSocketCallback(webSocket, httpClient) :
print("WS ACCEPT")
webSocket.RecvTextCallback = _recvTextCallback
webSocket.RecvBinaryCallback = _recvBinaryCallback
webSocket.ClosedCallback = _closedCallback

def _recvTextCallback(webSocket, msg) :
print("WS RECV TEXT : %s" % msg)
webSocket.SendText("Reply for %s" % msg)

def _recvBinaryCallback(webSocket, data) :
print("WS RECV DATA : %s" % data)

def _closedCallback(webSocket) :
print("WS CLOSED")

# ----------------------------------------------------------------------------

#routeHandlers = [
# ( "/test", "GET", _httpHandlerTestGet ),
# ( "/test", "POST", _httpHandlerTestPost )
#]

srv = MicroWebSrv(webPath='www/')
srv.MaxWebSocketRecvLen = 256
srv.WebSocketThreaded = False
srv.AcceptWebSocketCallback = _acceptWebSocketCallback
srv.Start(threaded=False)

# ----------------------------------------------------------------------------

Leave a comment