File tree Expand file tree Collapse file tree 1 file changed +59
-0
lines changed
Expand file tree Collapse file tree 1 file changed +59
-0
lines changed Original file line number Diff line number Diff line change 1+ try :
2+ import usocket as socket
3+ except :
4+ import socket
5+ import ussl as ssl
6+
7+
8+ CONTENT = b"""\
9+ HTTP/1.0 200 OK
10+
11+ Hello #%d from MicroPython!
12+ """
13+
14+ def main (use_stream = True ):
15+ s = socket .socket ()
16+
17+ # Binding to all interfaces - server will be accessible to other hosts!
18+ ai = socket .getaddrinfo ("0.0.0.0" , 8443 )
19+ print ("Bind address info:" , ai )
20+ addr = ai [0 ][4 ]
21+
22+ s .setsockopt (socket .SOL_SOCKET , socket .SO_REUSEADDR , 1 )
23+ s .bind (addr )
24+ s .listen (5 )
25+ print ("Listening, connect your browser to https://<this_host>:8443/" )
26+
27+ counter = 0
28+ while True :
29+ res = s .accept ()
30+ client_s = res [0 ]
31+ client_addr = res [1 ]
32+ print ("Client address:" , client_addr )
33+ print ("Client socket:" , client_s )
34+ client_s = ssl .wrap_socket (client_s , server_side = True )
35+ print (client_s )
36+ print ("Request:" )
37+ if use_stream :
38+ # Both CPython and MicroPython SSLSocket objects support read() and
39+ # write() methods.
40+ # Browsers are prone to terminate SSL connection abruptly if they
41+ # see unknown certificate, etc. We must continue in such case -
42+ # next request they issue will likely be more well-behaving and
43+ # will succeed.
44+ try :
45+ req = client_s .read (4096 )
46+ print (req )
47+ if req :
48+ client_s .write (CONTENT % counter )
49+ except Exception as e :
50+ print ("Exception serving request:" , e )
51+ else :
52+ print (client_s .recv (4096 ))
53+ client_s .send (CONTENT % counter )
54+ client_s .close ()
55+ counter += 1
56+ print ()
57+
58+
59+ main ()
You can’t perform that action at this time.
0 commit comments