SSL/TLS Support
This commit is contained in:
20
examples/tls/README.md
Normal file
20
examples/tls/README.md
Normal file
@@ -0,0 +1,20 @@
|
||||
This directory contains examples that demonstrate how to start TLS servers.
|
||||
|
||||
To run these examples, SSL certificate and private key files need to be
|
||||
created. When running under CPython, the files should be in PEM format, named
|
||||
`cert.pem` and `key.pem`. When running under MicroPython, they should be in DER
|
||||
format, and named `cert.der` and `key.der`.
|
||||
|
||||
To quickly create a self-signed SSL certificate, use the following command:
|
||||
|
||||
```bash
|
||||
openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365
|
||||
```
|
||||
|
||||
To convert the resulting PEM files to DER format for MicroPython, use these
|
||||
commands:
|
||||
|
||||
```bash
|
||||
openssl x509 -in localhost.pem -out localhost.der -outform DER
|
||||
openssl rsa -in localhost-key.pem -out localhost-key.der -outform DER
|
||||
```
|
||||
35
examples/tls/hello_async_tls.py
Normal file
35
examples/tls/hello_async_tls.py
Normal file
@@ -0,0 +1,35 @@
|
||||
import ssl
|
||||
from microdot_asyncio import Microdot
|
||||
|
||||
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>
|
||||
<p><a href="/shutdown">Click to shutdown the server</a></p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
'''
|
||||
|
||||
|
||||
@app.route('/')
|
||||
async def hello(request):
|
||||
return htmldoc, 200, {'Content-Type': 'text/html'}
|
||||
|
||||
|
||||
@app.route('/shutdown')
|
||||
async def shutdown(request):
|
||||
request.app.shutdown()
|
||||
return 'The server is shutting down...'
|
||||
|
||||
|
||||
sslctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
||||
sslctx.load_cert_chain('cert.pem', 'key.pem')
|
||||
app.run(port=4443, debug=True, ssl=sslctx)
|
||||
36
examples/tls/hello_tls.py
Normal file
36
examples/tls/hello_tls.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import sys
|
||||
from microdot import Microdot
|
||||
from microdot_ssl import create_ssl_context
|
||||
|
||||
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>
|
||||
<p><a href="/shutdown">Click to shutdown the server</a></p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
'''
|
||||
|
||||
|
||||
@app.route('/')
|
||||
def hello(request):
|
||||
return htmldoc, 200, {'Content-Type': 'text/html'}
|
||||
|
||||
|
||||
@app.route('/shutdown')
|
||||
def shutdown(request):
|
||||
request.app.shutdown()
|
||||
return 'The server is shutting down...'
|
||||
|
||||
|
||||
ext = 'der' if sys.implementation.name == 'micropython' else 'pem'
|
||||
sslctx = create_ssl_context('cert.' + ext, 'key.' + ext)
|
||||
app.run(port=4443, debug=True, ssl=sslctx)
|
||||
Reference in New Issue
Block a user