jczic / MicroWebSrv2

The last Micro Web Server for IoTs (MicroPython) or large servers (CPython), that supports WebSockets, routes, template engine and with really optimized architecture (mem allocations, async I/Os). Ready for ESP32, STM32 on Pyboard, Pycom's chipsets (WiPy, LoPy, ...). Robust, efficient and documented!
https://github.com/jczic/MicroWebSrv2
MIT License
662 stars 97 forks source link

Event at the end of a response #16

Closed jczic closed 4 years ago

jczic commented 4 years ago

But I have another question... Please

@WebRoute(POST, '/test-post', name='TestPost2/2') def RequestTestPost(microWebSrv2, request) : data = request.GetPostedURLEncodedForm() try :

    first = data['Firstname']
    last  = data['Lastname']

    f = open('data.txt','w')
    f.write(first+'\n')
    f.write(last)
    f.close()

except :
    request.Response.ReturnBadRequest()
    return
content   = """\
<!DOCTYPE html>
<html>
    <head>
        <title>POST 2/2</title>
    </head>
    <body>
        <h2>MicroWebSrv2 - POST 2/2</h2>
        your information has been saved.. Restrating the microcontroller :)<br />
    </body>
</html>
"""
request.Response.ReturnOk(content)
machine.reset()

After saving the first name and last name to a file. I want to return a response and then I want to reboot the microcontroller. but It is only rebooting the but not showing the response in the browser.

Originally posted by @aqieez in https://github.com/jczic/MicroWebSrv2/issues/10#issuecomment-558077044

jczic commented 4 years ago

About this question, there is actually no method to handle the end of the response sent... I can add an event property in few time. You'll be able to have a method to handle this with or without a keep-alive context.

aqieez commented 4 years ago

Alright. and also if I call any method from a handler it won't show any response in the browser.

@webroute(POST, '/test-post', name='TestPost2/2') def RequestTestPost(microWebSrv2, request) : data = request.GetPostedURLEncodedForm() try :

first = data['Firstname']
last  = data['Lastname']

f = open('data.txt','w')
f.write(first+'\n')
f.write(last)
f.close()
#**********************************Calling a method***********************
someMethod()

except : request.Response.ReturnBadRequest() return content = """\ <!DOCTYPE html>

POST 2/2 your information has been saved.. Restrating the microcontroller :)

""" request.Response.ReturnOk(content)

jczic commented 4 years ago

That's depend of methods... You are in a thread during the process of the request.

jczic commented 4 years ago

Ok @aqieez, you can set an event property to handle the response sent 👍 See https://github.com/jczic/MicroWebSrv2#response-prop (OnSent)