Move socket import, remove Request.G, and add simple hello example (#12)

* Further guard import of socket to make it optional

This is so that systems without a (u)socket module can still use Microdot.
For example if the transport layer is provided by a serial link.

* Add simple hello.py example that serves a static HTML page
This commit is contained in:
Damien George
2020-07-01 08:23:17 +10:00
committed by GitHub
parent dfbe2edd79
commit c5e1873523
2 changed files with 30 additions and 1 deletions

26
examples/hello.py Normal file
View File

@@ -0,0 +1,26 @@
from microdot import Microdot, Response
app = Microdot()
htmldoc = """<!DOCTYPE html>
<html>
<head>
<title>Microdot Example Page</title>
</head>
<body>
<div>
<h1>Microdot Example Page</h1>
<p>Hello from Microdot!</p>
</div>
</body>
</html>
"""
@app.route("", methods=["GET", "POST"])
def serial_number(request):
print(request.headers)
return Response(body=htmldoc, headers={"Content-Type": "text/html"})
app.run(debug=True)

View File

@@ -43,7 +43,10 @@ except ImportError:
try:
import usocket as socket
except ImportError:
import socket
try:
import socket
except ImportError:
socket = None
def urldecode(string):