|
| 1 | +# HTTP |
| 2 | + |
| 3 | +## SSL context |
| 4 | + |
| 5 | +One can enable SSL for HTTP server by providing an SSL context while creating the server: |
| 6 | + |
| 7 | +```python linenums="1" title="Use SSL" |
| 8 | +import ssl, os |
| 9 | + |
| 10 | +cert_file = f'assets{os.sep}security{os.sep}certificate.pem' |
| 11 | +key_file = f'assets{os.sep}security{os.sep}key.pem' |
| 12 | +ssl_context = ssl.SSLContext(protocol=ssl.PROTOCOL_TLS_SERVER) |
| 13 | +ssl_context.load_cert_chain(cert_file, keyfile=key_file) |
| 14 | +ssl_context.minimum_version = ssl.TLSVersion.TLSv1_3 |
| 15 | + |
| 16 | +http_server = HTTPServer(port=8090, ssl_context=ssl_context) |
| 17 | + |
| 18 | +Oscilloscope(id='oscilloscope').run(servers=[http_server]) |
| 19 | +# OR |
| 20 | +Oscilloscope( |
| 21 | + id='oscilloscope', |
| 22 | +).run_with_http_server( |
| 23 | + port=8090, |
| 24 | + ssl_context=ssl_context, |
| 25 | +) |
| 26 | +``` |
| 27 | + |
| 28 | +## Register Custom Routes & Methods |
| 29 | + |
| 30 | +The default routes are created as follows: |
| 31 | + |
| 32 | +| Resource | Path | Description | |
| 33 | +| ----------------- | ------------------------------ | ---------------------------- | |
| 34 | +| Property | `/<thing-id>/<foo-bar>` | for property `foo_bar` | |
| 35 | +| Action | `/<thing-id>/<foo-bar>` | for action `foo_bar` | |
| 36 | +| Event | `/<thing-id>/<foo-bar>` | for event `foo_bar` | |
| 37 | +| Thing Model | `/<thing-id>/resources/wot-tm` | to get the Thing Model | |
| 38 | +| Thing Description | `/<thing-id>/resources/wot-td` | to get the Thing Description | |
| 39 | + |
| 40 | +## Allow CORS |
| 41 | + |
| 42 | +On the web browser, one may want to access the HTTP server from a different domain name, especially during development or in private networks. In such cases, one needs to enable CORS headers: |
| 43 | + |
| 44 | +```python linenums="1" title="Enable CORS" |
| 45 | +http_server = HTTPServer(port=8090, config=dict(cors=True)) |
| 46 | +Oscilloscope(id='oscilloscope').run(servers=[http_server]) |
| 47 | +# OR |
| 48 | +Oscilloscope( |
| 49 | + id='oscilloscope', |
| 50 | +).run_with_http_server( |
| 51 | + port=8090, |
| 52 | + config=dict(cors=True), |
| 53 | +) |
| 54 | +``` |
| 55 | + |
| 56 | +CORS headers are set only for authenticated clients. |
| 57 | + |
| 58 | +## Remotely Stop |
| 59 | + |
| 60 | +If one wishes to remotely stop the HTTP server, one needs to exit both the served `Thing` instance as well as the server itself. This can be done as follows: |
| 61 | + |
| 62 | +```python linenums="1" title="Remotely Stop HTTP Server" |
| 63 | +import requests |
| 64 | + |
| 65 | +response = requests.post('https://my-pc:8090/my-thing-id/exit') |
| 66 | +assert response.status_code == 204 |
| 67 | +response = requests.post('https://my-pc:8090/stop') |
| 68 | +assert response.status_code == 204 |
| 69 | +``` |
| 70 | + |
| 71 | +Make sure to use a [security scheme](../security.md) to prevent unauthorized access to the exit action. |
0 commit comments