|
| 1 | +""" |
| 2 | +MicroPyServer is a simple HTTP server for MicroPython projects. |
| 3 | +
|
| 4 | +@see https://github.com/troublegum/micropyserver |
| 5 | +
|
| 6 | +The MIT License |
| 7 | +
|
| 8 | +Copyright (c) 2019 troublegum. https://github.com/troublegum/micropyserver |
| 9 | +
|
| 10 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 11 | +of this software and associated documentation files (the "Software"), to deal |
| 12 | +in the Software without restriction, including without limitation the rights |
| 13 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 14 | +copies of the Software, and to permit persons to whom the Software is |
| 15 | +furnished to do so, subject to the following conditions: |
| 16 | +
|
| 17 | +The above copyright notice and this permission notice shall be included in |
| 18 | +all copies or substantial portions of the Software. |
| 19 | +
|
| 20 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 21 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 22 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFINGEMENT. IN NO EVENT SHALL THE |
| 23 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 24 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 25 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 26 | +THE SOFTWARE. |
| 27 | +""" |
| 28 | + |
| 29 | +""" HTTP response codes """ |
| 30 | +HTTP_CODES = { |
| 31 | + 100: 'Continue', |
| 32 | + 101: 'Switching protocols', |
| 33 | + 102: 'Processing', |
| 34 | + 200: 'Ok', |
| 35 | + 201: 'Created', |
| 36 | + 202: 'Accepted', |
| 37 | + 203: 'Non authoritative information', |
| 38 | + 204: 'No content', |
| 39 | + 205: 'Reset content', |
| 40 | + 206: 'Partial content', |
| 41 | + 207: 'Multi status', |
| 42 | + 208: 'Already reported', |
| 43 | + 226: 'Im used', |
| 44 | + 300: 'Multiple choices', |
| 45 | + 301: 'Moved permanently', |
| 46 | + 302: 'Found', |
| 47 | + 303: 'See other', |
| 48 | + 304: 'Not modified', |
| 49 | + 305: 'Use proxy', |
| 50 | + 307: 'Temporary redirect', |
| 51 | + 308: 'Permanent redirect', |
| 52 | + 400: 'Bad request', |
| 53 | + 401: 'Unauthorized', |
| 54 | + 402: 'Payment required', |
| 55 | + 403: 'Forbidden', |
| 56 | + 404: 'Not found', |
| 57 | + 405: 'Method not allowed', |
| 58 | + 406: 'Not acceptable', |
| 59 | + 407: 'Proxy authentication required', |
| 60 | + 408: 'Request timeout', |
| 61 | + 409: 'Conflict', |
| 62 | + 410: 'Gone', |
| 63 | + 411: 'Length required', |
| 64 | + 412: 'Precondition failed', |
| 65 | + 413: 'Request entity too large', |
| 66 | + 414: 'Request uri too long', |
| 67 | + 415: 'Unsupported media type', |
| 68 | + 416: 'Request range not satisfiable', |
| 69 | + 417: 'Expectation failed', |
| 70 | + 418: 'I am a teapot', |
| 71 | + 422: 'Unprocessable entity', |
| 72 | + 423: 'Locked', |
| 73 | + 424: 'Failed dependency', |
| 74 | + 426: 'Upgrade required', |
| 75 | + 428: 'Precondition required', |
| 76 | + 429: 'Too many requests', |
| 77 | + 431: 'Request header fields too large', |
| 78 | + 500: 'Internal server error', |
| 79 | + 501: 'Not implemented', |
| 80 | + 502: 'Bad gateway', |
| 81 | + 503: 'Service unavailable', |
| 82 | + 504: 'Gateway timeout', |
| 83 | + 505: 'Http version not supported', |
| 84 | + 506: 'Variant also negotiates', |
| 85 | + 507: 'Insufficient storage', |
| 86 | + 508: 'Loop detected', |
| 87 | + 510: 'Not extended', |
| 88 | + 511: 'Network authentication required', |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +def send_response(server, response, http_code=200, content_type="text/html", extend_headers=None): |
| 93 | + """ send response """ |
| 94 | + server.send("HTTP/1.0 " + str(http_code) + " " + HTTP_CODES.get(http_code) + "\r\n") |
| 95 | + server.send("Content type:" + content_type + "\r\n") |
| 96 | + if extend_headers is not None: |
| 97 | + for header in extend_headers: |
| 98 | + server.send(header + "\r\n") |
| 99 | + server.send("\r\n") |
| 100 | + server.send(response) |
0 commit comments