{"id":13541958,"url":"https://github.com/troublegum/micropyserver","last_synced_at":"2025-10-07T13:47:10.846Z","repository":{"id":36566044,"uuid":"215214381","full_name":"troublegum/micropyserver","owner":"troublegum","description":"MicroPyServer is a simple HTTP server for MicroPython projects.","archived":false,"fork":false,"pushed_at":"2025-01-12T20:04:26.000Z","size":49,"stargazers_count":143,"open_issues_count":3,"forks_count":35,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-05-24T22:06:51.727Z","etag":null,"topics":["esp32","esp32-webserver","esp8266","esp8266-webserver","micropython","micropython-webserver","webserver"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/troublegum.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-10-15T05:31:58.000Z","updated_at":"2025-05-03T12:53:40.000Z","dependencies_parsed_at":"2025-01-12T21:18:40.023Z","dependency_job_id":"e897c50f-147b-4cf8-a587-74643d8c5d2a","html_url":"https://github.com/troublegum/micropyserver","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/troublegum/micropyserver","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/troublegum%2Fmicropyserver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/troublegum%2Fmicropyserver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/troublegum%2Fmicropyserver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/troublegum%2Fmicropyserver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/troublegum","download_url":"https://codeload.github.com/troublegum/micropyserver/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/troublegum%2Fmicropyserver/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":278786692,"owners_count":26045588,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-07T02:00:06.786Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["esp32","esp32-webserver","esp8266","esp8266-webserver","micropython","micropython-webserver","webserver"],"created_at":"2024-08-01T10:00:59.372Z","updated_at":"2025-10-07T13:47:10.829Z","avatar_url":"https://github.com/troublegum.png","language":"Python","funding_links":[],"categories":["Libraries","Frameworks for Micropython"],"sub_categories":["Communications","More"],"readme":"# MicroPyServer\r\n\r\n[MicroPyServer](https://github.com/troublegum/micropyserver) is a simple HTTP server for MicroPython projects.\r\n\r\n**Important!** Version 1.1.x is not compatible with version 1.0.1 and older.\r\n\r\n## Install\r\n\r\nDownload a code and unpack it into your project folder.\r\nUse [Thonny IDE](https://thonny.org/) or other IDE for upload your code in ESP8266/ESP32 board.\r\n\r\n## Quick start\r\n\r\n### Typical Wi-Fi connection code for ESP board\r\n```\r\nimport esp\r\nimport network\r\nimport time\r\nimport ubinascii\r\n\r\nwlan_id = \"your wi-fi\"\r\nwlan_pass = \"your password\"\r\n\r\nmac = ubinascii.hexlify(network.WLAN().config('mac'),':').decode()\r\nprint(\"MAC: \" + mac)\r\n\r\nwlan = network.WLAN(network.STA_IF)\r\nwlan.active(True)\r\nwhile wlan.status() is network.STAT_CONNECTING:\r\n    time.sleep(1)\r\nwhile not wlan.isconnected():\r\n    wlan.connect(wlan_id, wlan_pass)\r\nprint(\"Connected... IP: \" + wlan.ifconfig()[0])  \r\n```\r\n\r\n### Typical modules that you should import\r\n```\r\nimport esp\r\nimport network\r\nimport machine\r\nimport os\r\nimport time \u003c--- if used typical connection code from example\r\nimport ubinascii \u003c--- if used typical connection code from example\r\nimport utils \u003c--- if user utils\r\nfrom micropyserver import MicroPyServer\r\n```\r\n\r\n### Hello world example\r\n\r\nType in browser http://IP_ADDRESS_ESP/ and you will see \"HELLO WORLD\" text.\r\n\r\n```\r\nfrom micropyserver import MicroPyServer\r\n\r\n''' there should be a wi-fi connection code here '''\r\n\r\ndef hello_world(request):\r\n    ''' request handler '''\r\n    server.send(\"HELLO WORLD!\")\r\n\r\nserver = MicroPyServer()\r\n''' add route '''\r\nserver.add_route(\"/\", hello_world)\r\n''' start server '''\r\nserver.start()\r\n```\r\n\r\n### Add some routes\r\n\r\nType in browser http://IP_ADDRESS_ESP/ or http://IP_ADDRESS_ESP/another_action and your will see text \"THIS IS INDEX PAGE!\" or \"THIS IS ANOTHER ACTION!\".\r\n\r\n```\r\nfrom micropyserver import MicroPyServer\r\n\r\n''' there should be a wi-fi connection code here '''\r\n\r\ndef show_index(request):\r\n    ''' main request handler '''\r\n    server.send(\"THIS IS INDEX PAGE!\")\r\n    \r\ndef another_action(request):\r\n    ''' another action handler '''\r\n    server.send(\"THIS IS ANOTHER ACTION!\")\r\n\r\nserver = MicroPyServer()\r\n''' add routes '''\r\nserver.add_route(\"/\", show_index)\r\nserver.add_route(\"/another_action\", another_action)\r\n''' start server '''\r\nserver.start()\r\n```\r\n\r\n### Send JSON response example\r\n\r\nType in browser http://IP_ADDRESS_ESP/ and you will see JSON response.\r\n\r\n```\r\nfrom micropyserver import MicroPyServer\r\nimport json\r\n\r\n''' there should be a wi-fi connection code here '''\r\n\r\ndef return_json(request):\r\n    ''' request handler '''\r\n    json_str = json.dumps({\"param_one\": 1, \"param_two\": 2})\r\n    server.send(\"HTTP/1.0 200 OK\\r\\n\")\r\n    server.send(\"Content-Type: application/json\\r\\n\\r\\n\")\r\n    server.send(json_str)\r\n\r\nserver = MicroPyServer()\r\n''' add route '''\r\nserver.add_route(\"/\", return_json)\r\n''' start server '''\r\nserver.start()\r\n```\r\n\r\n### Access denied example\r\n\r\nType in browser http://IP_ADDRESS_ESP/ and you will see \"THIS IS INDEX PAGE!\" text or \"ACCESS DENIED!\" if your IP not equal \"127.0.0.1\".\r\n\r\n```\r\nfrom micropyserver import MicroPyServer\r\n\r\n''' there should be a wi-fi connection code here '''\r\n\r\ndef show_index(request):\r\n    ''' main request handler '''\r\n    server.send(\"THIS IS INDEX PAGE!\")\r\n    \r\ndef on_request_handler(request, address):\r\n    if str(address[0]) != \"127.0.0.1\":\r\n        server.send(\"HTTP/1.0 403\\r\\n\\r\\n\")\r\n        server.send(\"ACCESS DENIED!\")\r\n        return False        \r\n    return True\r\n\r\n\r\nserver = MicroPyServer()\r\n''' add route '''\r\nserver.add_route(\"/\", show_index)\r\n''' add request handler '''\r\nserver.on_request(on_request_handler)\r\n''' start server '''\r\nserver.start()\r\n``` \r\n\r\n### Turn ON / OFF a LED example\r\n\r\nYou can remote control a LED via internet. Use your browser for on/off LED. Type in browser http://IP_ADDRESS_ESP/on or http://IP_ADDRESS_ESP/off.\r\n\r\n![schema](https://habrastorage.org/webt/jb/xu/aj/jbxuaj0nr8fnqllbq27p_vfx3bw.png)\r\n\r\n```\r\nfrom micropyserver import MicroPyServer\r\nimport machine\r\n\r\n''' there should be a wi-fi connection code here '''   \r\n    \r\ndef do_on(request):\r\n    ''' on request handler '''\r\n    pin.value(1)\r\n    server.send(\"ON\")\r\n\r\ndef do_off(request):\r\n    ''' off request handler '''\r\n    pin.value(0)\r\n    server.send(\"OFF\")\r\n    \r\ndef do_index(request):\r\n    ''' index request handler '''    \r\n    server.send(\"SWITCH ON/OFF\")\r\n\r\npin = machine.Pin(13, machine.Pin.OUT)\r\nserver = MicroPyServer()\r\n''' add routes '''\r\nserver.add_route(\"/\", do_index)\r\nserver.add_route(\"/on\", do_on)\r\nserver.add_route(\"/off\", do_off)\r\n''' start server '''\r\nserver.start()    \r\n```    \r\n\r\n### Use utils for create response\r\n```\r\nfrom micropyserver import MicroPyServer\r\nimport utils\r\n\r\n''' there should be a wi-fi connection code here '''\r\n\r\ndef hello_world(request):\r\n    ''' request handler '''\r\n    utils.send_response(server, \"HELLO WORLD!\")\r\n\r\ndef not_found(request):\r\n    ''' request handler '''\r\n    utils.send_response(server, \"404\", 404)\r\n\r\nserver = MicroPyServer()\r\n''' add routes '''\r\nserver.add_route(\"/\", hello_world)\r\nserver.add_route(\"/404\", not_found)\r\n''' start server '''\r\nserver.start()\r\n```\r\n\r\n### Parse HTTP request. Get query params from request.\r\nType in browser http://IP_ADDRESS_ESP/?param_one=one\u0026param_two=two\r\n\r\n```\r\n''' Example of HTTP request: GET /?param_one=one\u0026param_two=two HTTP/1.1\\r\\nHost: localhost\\r\\n\\r\\n '''\r\nfrom micropyserver import MicroPyServer\r\nimport utils\r\n\r\n''' there should be a wi-fi connection code here '''\r\n\r\ndef show_params(request):\r\n    ''' request handler '''\r\n\tparams = utils.get_request_query_params(request)\t\r\n\tprint(params)\r\n\t''' will return {\"param_one\": \"one\", \"param_two\": \"two\"} '''\r\n\r\nserver = MicroPyServer()\r\n''' add route '''\r\nserver.add_route(\"/\", show_params)\r\n''' start server '''\r\nserver.start()\r\n\r\n```\r\n\r\n### Custom 404 page\r\n1. Upload HTML 404 page file in ESP8266 with name 404.html.\r\n2. Add not found handler in your code.\r\n\r\n404 HTML page:\r\n```\r\n\u003chtml\u003e\r\n\u003chead\u003e\r\n    \u003cmeta charset=\"UTF-8\" /\u003e\r\n    \u003ctitle\u003e404 Error Page\u003c/title\u003e\r\n\u003c/head\u003e\r\n\u003cbody\u003e\r\n    \u003ch1\u003e404 Page not found\u003c/h1\u003e\r\n\u003c/body\u003e\r\n\u003c/html\u003e\r\n```\r\n\r\nCode:\r\n```\r\nfrom micropyserver import MicroPyServer\r\n\r\n''' there should be a wi-fi connection code here '''\r\n\r\ndef not_found_handler(request):    \r\n    server.send(\"HTTP/1.0 404\\r\\n\")\r\n    server.send(\"Content type: text/html\\r\\n\\r\\n\")\r\n    file = open(\"404.html\")\r\n    for line in file:\r\n        server.send(line)\r\n    file.close()  \r\n\r\nserver = MicroPyServer()\r\nserver.on_not_found(not_found_handler)\r\n```\r\n\r\n\r\n## MicroPyServer methods\r\n\r\n**MicroPyServer(host=\"0.0.0.0\", port=80)** - constructor\r\n\r\n**start()** - start server\r\n\r\n**stop()** - stop server\r\n\r\n**add_route(path, handler, method=\"GET\")** - add new route\r\n\r\n**send(response)** - send response to client\r\n\r\n**get_request()** - return current request\r\n\r\n**on_request(handler)** - set handler on every request\r\n\r\n**on_not_found(handler)** - set handler on 404\r\n\r\n**on_error(handler)** - set handler on server error\r\n\r\n\r\n## Utils methods\r\n\r\n**send_response(server, response, http_code=200, content_type=\"text/html\", extend_headers=None)** - send response to client\r\n\r\n**get_request_method(request)** - return HTTP request method (example of return value: POST)\r\n\r\n**get_request_query_string(request)** - return query string from HTTP request (example of return value: param_one=one\u0026param_two=two)\r\n\r\n**parse_query_string(query_string)** - return params from query string (example of return value: {\"param_one\": \"one\", \"param_two\": \"two\"})\r\n\r\n**get_request_query_params(request)** - return query params from HTTP request (example of return value: {\"param_one\": \"one\", \"param_two\": \"two\"})\r\n\r\n**get_request_post_params(request)** - return params from POST request (example of return value: {\"param_one\": \"one\", \"param_two\": \"two\"})\r\n\r\n**unquote(string)** - unquote string\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftroublegum%2Fmicropyserver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftroublegum%2Fmicropyserver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftroublegum%2Fmicropyserver/lists"}