WEB Server



  • @jczic I'm following your library, discovered some days ago.
    Many thanks for sharing! I will dig in a couple of days with it, at the moment I miss some more "elaborated" example (for novices as me).
    Will try to make a kind of wifimanager saving/retrieving values to flash in order to connect to a WiFi or keep settings.
    Again, many thanks for sharing this, great work!



  • @pierrot10 Yes, the default IP for LoPy is 192.168.4.1.
    If you want, you can also use https://github.com/jczic/MicroDNSSrv to resolve names and connect to the good IP.



  • @pierrot10
    if you start lopy in AP mode then it default ip is 192.168.*4*.1 not 0

    and what you got on UART? It start without an error?

    and please format your future post
    http://commonmark.org/help/



  • @jczic I am trying hard to make working MicroWebServer. In fact IsStarted return me always false. I really have no idea what I have to do.
    I tried different solution but without succes. WOuld it be possible to tell what wrong I did? Did I missed apackage?

    I supposed, I should connect the the WLAN 'lypo4_1' and open a browser with the 192.168.0.1?

    My structure is
    myProject/flash/www
    myProject/js
    myProject/main.py
    myProject/boot.py
    myProject/microWebSocket.py
    myProject/microWebSrv.py
    myProject/microWebTemplate.py

    Here is my code

    #import socket
    #import time
    #import binascii
    import pycom

    from network import WLAN
    #from pysense import Pysense
    from microWebSrv import MicroWebSrv

    #py = Pysense()
    print('\n\n** Init WLAN mode and WAP2')
    wlan = WLAN(mode=WLAN.AP,ssid="lypo4_1",auth=(WLAN.WPA2,'00000000')) # we call the constructor without params
    print('\n\n** Next...')

    ----------------------------------------------------------------------------

    def _httpHandlerTestGet(httpClient, httpResponse) :
    content = """
    <!DOCTYPE html>
    <html lang=fr>
    <head>
    <meta charset="UTF-8" />
    <title>TEST GET</title>
    </head>
    <body>
    <h1>TEST GET</h1>
    Client IP address = %s
    <br />
    <form action="/test" method="post" accept-charset="ISO-8859-1">
    First name: <input type="text" name="firstname"><br />
    Last name: <input type="text" name="lastname"><br />
    <input type="submit" value="Submit">
    </form>
    </body>
    </html>
    """ % httpClient.GetIPAddr()
    httpResponse.WriteResponseOk( headers = None,
    contentType = "text/html",
    contentCharset = "UTF-8",
    content = content )

    def _httpHandlerTestPost(httpClient, httpResponse) :
    formData = httpClient.ReadRequestPostedFormData()
    firstname = formData["firstname"]
    lastname = formData["lastname"]
    content = """
    <!DOCTYPE html>
    <html lang=fr>
    <head>
    <meta charset="UTF-8" />
    <title>TEST POST</title>
    </head>
    <body>
    <h1>TEST POST</h1>
    Firstname = %s<br />
    Lastname = %s<br />
    </body>
    </html>
    """ % ( MicroWebSrv.HTMLEscape(firstname),
    MicroWebSrv.HTMLEscape(lastname) )
    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")

    print('\n\n** routeHandle')
    routeHandlers = [
    ( "/test", "GET", _httpHandlerTestGet ),
    ( "/test", "POST", _httpHandlerTestPost )
    ]
    print('\n\n** MicroWebserv')
    #srv = MicroWebSrv(routeHandlers=routeHandlers)
    #srv = MicroWebSrv(routeHandlers=None, port=80, webPath="/flash/www")
    srv = MicroWebSrv()
    srv.MaxWebSocketRecvLen = 256
    srv.WebSocketThreaded = False
    srv.AcceptWebSocketCallback = _acceptWebSocketCallback
    #srv.Start(threaded=True)
    srv.Start()
    print('\n\n** Ready')

    if srv.IsStarted():
    print('Web started')
    else:
    print('WEB NOT started')



  • Hi,

    If you are interested in a web server and a template language (.pyhtml) for pycom modules in micropython, you can try this release :
    https://github.com/jczic/MicroWebSrv

    (Be careful though, this takes memory.)



  • also this can be somehow helpfull(it is normal python but..) if you need extend functionality
    https://hg.python.org/cpython/file/3.6/Lib/http/server.py



  • @constantinos

    I don't think that there's something builtin like Python's SimpleHTTPServer but the following topics might help you:

    https://github.com/RinusW/WiPy/tree/master/AiCWebserver
    http://forum.micropython.org/viewtopic.php?t=1033



  • OK. Your are right. I have to be more specific.
    In ESP8266 Arduino Core a httpserver class exists. It is very easy to setup a web server. I was wondering if exists something similar for pyCom.



  • @constantinos
    What do you mean?


Log in to reply
 

Pycom on Twitter