I am attempting to incorporate the current microWebSrv2 module into the latest micropython build. We are using the NUCLEO-F767ZI board. Threading has been enabled in /ports/stm32/mpconfigport.py. Module microWebSrv2 has been frozen into the build by including the following into /ports/stm32/boards/manifest.py:
Code: Select all
freeze("$(MPY_DIR)/ports/stm32/modules/MicroWebSrv2", ("httpRequest.py", "httpResponse.py", "__init__.py", "microWebSrv2.py", "webRoute.py"))
freeze("$(MPY_DIR)/ports/stm32/modules/MicroWebSrv2/libs", ("urlUtils.py", "XAsyncSockets.py"))
freeze("$(MPY_DIR)/ports/stm32/modules/MicroWebSrv2/mods", ("PyhtmlTemplate.py", "WebSockets.py"))
To test the implementation, I placed the example from the microWebSrv2 repository (https://github.com/jczic/MicroWebSrv2/b ... er/main.py) into the PYBFLASH directory. Code is as follows:
Code: Select all
from microWebSrv2 import *
from time import sleep
from _thread import allocate_lock
# ============================================================================
# ============================================================================
# ============================================================================
@WebRoute(GET, '/test-redir')
def RequestTestRedirect(microWebSrv2, request) :
request.Response.ReturnRedirect('/test.pdf')
# ============================================================================
# ============================================================================
# ============================================================================
@WebRoute(GET, '/test-post', name='TestPost1/2')
def RequestTestPost(microWebSrv2, request) :
content = """\
<!DOCTYPE html>
<html>
<head>
<title>POST 1/2</title>
</head>
<body>
<h2>MicroWebSrv2 - POST 1/2</h2>
User address: %s<br />
<form action="/test-post" method="post">
First name: <input type="text" name="firstname"><br />
Last name: <input type="text" name="lastname"><br />
<input type="submit" value="OK">
</form>
</body>
</html>
""" % request.UserAddress[0]
request.Response.ReturnOk(content)
# ------------------------------------------------------------------------
@WebRoute(POST, '/test-post', name='TestPost2/2')
def RequestTestPost(microWebSrv2, request) :
data = request.GetPostedURLEncodedForm()
try :
firstname = data['firstname']
lastname = data['lastname']
except :
request.Response.ReturnBadRequest()
return
content = """\
<!DOCTYPE html>
<html>
<head>
<title>POST 2/2</title>
</head>
<body>
<h2>MicroWebSrv2 - POST 2/2</h2>
Hello %s %s :)<br />
</body>
</html>
""" % ( MicroWebSrv2.HTMLEscape(firstname),
MicroWebSrv2.HTMLEscape(lastname) )
request.Response.ReturnOk(content)
# ============================================================================
# ============================================================================
# ============================================================================
def OnWebSocketAccepted(microWebSrv2, webSocket) :
print('Example WebSocket accepted:')
print(' - User : %s:%s' % webSocket.Request.UserAddress)
print(' - Path : %s' % webSocket.Request.Path)
print(' - Origin : %s' % webSocket.Request.Origin)
if webSocket.Request.Path.lower() == '/wschat' :
WSJoinChat(webSocket)
else :
webSocket.OnTextMessage = OnWebSocketTextMsg
webSocket.OnBinaryMessage = OnWebSocketBinaryMsg
webSocket.OnClosed = OnWebSocketClosed
# ============================================================================
# ============================================================================
# ============================================================================
def OnWebSocketTextMsg(webSocket, msg) :
print('WebSocket text message: %s' % msg)
webSocket.SendTextMessage('Received "%s"' % msg)
# ------------------------------------------------------------------------
def OnWebSocketBinaryMsg(webSocket, msg) :
print('WebSocket binary message: %s' % msg)
# ------------------------------------------------------------------------
def OnWebSocketClosed(webSocket) :
print('WebSocket %s:%s closed' % webSocket.Request.UserAddress)
# ============================================================================
# ============================================================================
# ============================================================================
global _chatWebSockets
_chatWebSockets = [ ]
global _chatLock
_chatLock = allocate_lock()
# ------------------------------------------------------------------------
def WSJoinChat(webSocket) :
webSocket.OnTextMessage = OnWSChatTextMsg
webSocket.OnClosed = OnWSChatClosed
addr = webSocket.Request.UserAddress
with _chatLock :
for ws in _chatWebSockets :
ws.SendTextMessage('<%s:%s HAS JOINED THE CHAT>' % addr)
_chatWebSockets.append(webSocket)
webSocket.SendTextMessage('<WELCOME %s:%s>' % addr)
# ------------------------------------------------------------------------
def OnWSChatTextMsg(webSocket, msg) :
addr = webSocket.Request.UserAddress
with _chatLock :
for ws in _chatWebSockets :
ws.SendTextMessage('<%s:%s> %s' % (addr[0], addr[1], msg))
# ------------------------------------------------------------------------
def OnWSChatClosed(webSocket) :
addr = webSocket.Request.UserAddress
with _chatLock :
if webSocket in _chatWebSockets :
_chatWebSockets.remove(webSocket)
for ws in _chatWebSockets :
ws.SendTextMessage('<%s:%s HAS LEFT THE CHAT>' % addr)
# ============================================================================
# ============================================================================
# ============================================================================
print()
# Loads the PyhtmlTemplate module globally and configure it,
pyhtmlMod = MicroWebSrv2.LoadModule('PyhtmlTemplate')
pyhtmlMod.ShowDebug = True
pyhtmlMod.SetGlobalVar('TestVar', 12345)
# Loads the WebSockets module globally and configure it,
wsMod = MicroWebSrv2.LoadModule('WebSockets')
wsMod.OnWebSocketAccepted = OnWebSocketAccepted
# Instanciates the MicroWebSrv2 class,
mws2 = MicroWebSrv2()
# SSL is not correctly supported on MicroPython.
# But you can uncomment the following for standard Python.
# mws2.EnableSSL( certFile = 'SSL-Cert/openhc2.crt',
# keyFile = 'SSL-Cert/openhc2.key' )
# For embedded MicroPython, use a very light configuration,
mws2.SetEmbeddedConfig()
# All pages not found will be redirected to the home '/',
mws2.NotFoundURL = '/'
# Starts the server as easily as possible in managed mode,
mws2.StartManaged()
# Main program loop until keyboard interrupt,
try :
while mws2.IsRunning :
sleep(1)
except KeyboardInterrupt :
pass
# End,
print()
mws2.Stop()
print('Bye')
print()
# ============================================================================
# ============================================================================
# ============================================================================
Traceback (most recent call last):
File "main.py", line 1, in <module>
File "microWebSrv2.py", line 10, in <module>
ValueError: can't perform relative import
MicroPython v1.15-37-gd3eb6d68a-dirty on 2021-05-09; NUCLEO-F767ZI with STM32F767
Type "help()" for more information.
>>>
Software built in a standard PC running Linux Mint 19.3 Cinnamon, if this is relevant.
Any help would be much appreciated!