Skip to content

The correct way to implement the simplest web server #12219

Answered by shariltumin
omri-madar asked this question in ESP32

You must be logged in to vote

@omri-madar, I made wrong claim earlier, sorry. It is possible to use socket event callbacks to write request/response server without looping. The code below seems to work fine.

# To test on Linux
# curl --http0.9 http://10.0.1.73:8080
# telnet 10.0.1.73 8080

import socket, select, time, network
network.WLAN(network.AP_IF).active(False)
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("YOUR-WIFI", "YOUR-PWD")
time.sleep(5)
wlan.ifconfig()

def req_handler(cs):
    try:
       req = cs.read()
       if req:
          print('req:', req)
          rep = b'Hello\r\n'
          cs.write(rep)
       else:
          print('Client close connection')
    except Exception as e:
 …

Replies: 11 comments 10 replies

You must be logged in to vote
0 replies

You must be logged in to vote
0 replies

You must be logged in to vote
2 replies
@Fisherman48

@robert-hh

You must be logged in to vote
0 replies

You must be logged in to vote
3 replies
@robert-hh

@rkompass

@jchome

Answer selected by omri-madar

You must be logged in to vote
0 replies

You must be logged in to vote
2 replies
@rkompass

@rkompass

You must be logged in to vote
0 replies

You must be logged in to vote
0 replies

You must be logged in to vote
3 replies
@rkompass

@rkompass

@shariltumin

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
ESP32
Labels
None yet
8 participants