{"id":16000674,"url":"https://github.com/majoson-chen/micropython-urouter","last_synced_at":"2025-03-17T16:30:24.125Z","repository":{"id":48485505,"uuid":"374708336","full_name":"majoson-chen/micropython-urouter","owner":"majoson-chen","description":"A lightweight HTTP request routing processing support library based on micropython.  The previous name was micro-route","archived":false,"fork":false,"pushed_at":"2023-01-16T13:32:53.000Z","size":96,"stargazers_count":50,"open_issues_count":1,"forks_count":8,"subscribers_count":0,"default_branch":"main","last_synced_at":"2024-07-29T22:14:44.492Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/majoson-chen.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}},"created_at":"2021-06-07T15:08:17.000Z","updated_at":"2024-07-18T00:27:45.000Z","dependencies_parsed_at":"2023-02-10T03:45:36.037Z","dependency_job_id":null,"html_url":"https://github.com/majoson-chen/micropython-urouter","commit_stats":null,"previous_names":["majoson-chen/micropython-urouter","whales-chen/micropython-urouter"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/majoson-chen%2Fmicropython-urouter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/majoson-chen%2Fmicropython-urouter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/majoson-chen%2Fmicropython-urouter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/majoson-chen%2Fmicropython-urouter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/majoson-chen","download_url":"https://codeload.github.com/majoson-chen/micropython-urouter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243869603,"owners_count":20361043,"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","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":[],"created_at":"2024-10-08T09:05:49.466Z","updated_at":"2025-03-17T16:30:21.551Z","avatar_url":"https://github.com/majoson-chen.png","language":"Python","readme":"# micropython-uRouter 开发文档\n\n![LOGO](/cover.png)\n\n**注意：** 本项目目前处于缓慢或者暂停更新状态，因为作者高三了，欢迎志同道合的朋友提交 `pull request` 。 同时，我需要有朋友帮我翻译和完善下文档，有意者可以提交issue或者pr哦~\n\n## 阅读 [开发文档](https://urouter.m-jay.cn) 以获得更多信息\n\n## 注意: 本框架目前正处于预览开发阶段, 如果在使用过程中遇到任何问题, 请前往提交 [issue](https://github.com/Li-Lian1069/micropython-urouter/issue)\n\n\u003cspan style=\"color: #B06AB3\"\u003e\u003cstrong\u003e\n如果您喜欢本项目, 或者本项目帮到了您, 麻烦给本项目点个 `star` 吧~\u003c/strong\u003e\n\u003c/span\u003e\n\n## I need help\nI am not expert at English, so i need someone who is good at English helping me to translate the document into English.\n\n## 这是什么\n\n这是一个工作在 `micropython` 上的一个轻量, 简单, 快速的WEB框架.\n\n**为什么要开发这个框架?**\n\n据我所知, 目前已知的可以在 `micropython` 上运行的 web 框架有 [microWebSrv](https://github.com/jczic/MicroWebSrv) , 但是这个框架需要 `_thread` 模块的支持\n\n但, 很遗憾的是, 在类似于 `esp8266` 这样的板子上并没有该模块的支持.\n\n同时, 这个模块的运行需要阻塞主线程, 这就意味着你的开发版只能一味地处理HTTP请求, 而不能处理其他的事情.\n\n因此, 我们需要一个支持单线程运行, 而且不会阻塞主线程的web框架, 于是这个框架应运而生.\n\n## 特色功能:\n本框架的开发以 `简洁`, `轻巧`, `易用`, `灵活`, `高效` 作为主旨.  \n\n本模块有两种独特的工作模式:\n- **normal-mode 普通工作模式**  \n在此模式下本框架的处理是阻塞的, 即一切请求都会被阻塞并且等待  \n\n- **dynamic-mode 动态工作模式**  \n在此模式下本框架的处理是非阻塞的, 即检查浏览器请求的操作会立即被返回(理论上), 只有在处理HTTP请求的情况下会阻塞线程.  \n\n### flask 风格的context上下文管理\n本模块拥有类似于 `flask` 框架的 `context` 管理对象(即`request`, `response`, `session`), 减少学习成本, 简化开发流程  \n\n### 魔法路径变量\n本框架支持与Flask类似的魔法路径变量, 支持使用一个规则字符串匹配多个路径.\n\n## 与 microWebSrv 的对比\n\n| 特性/框架      | mpy_uRouter | microWebSrv |\n| -------------- | ----------- | ----------- |\n| python习惯命名 | *           |             |\n| 单线程支持     | *           | *           |\n| 多线程支持     |            | *           |\n| 非阻塞支持     | *           |             |\n| 多对象支持     | *           |             |\n| 学习成本       | 低          | 高          |\n| 复杂程度       | 简单        | 复杂        |\n\n## 如何使用?\n### 通过pypi安装\n```python\n# on mpy repl.\nimport upip\n\nupip.install('micropython-uRouter')\nupip.install('micropython-ulogger')\n```\n\n### 手动安装(推荐)\n通过 `pypi` 下载的包是未经过编译的 `python` 源代码文件, `micropython` 支持将源代码文件进行编译以获得执行速度上的提升和体积上的缩小. 如果你的设备(例如`esp8266`) 在导入模块的过程中遇到内存分配失败的错误, 可以尝试使用此方式安装.\n\n步骤:  \n1. 先去本项目的 [release](https://github.com/Li-Lian1069/micropython-urouter/releases) 页面下载一个最新版本的打包版本\n2. 将下载到的文件上传到开发版的 `/lib` 目录中. (可以使用 `thonny` ide)\n3. 安装类似的步骤安装本框架的依赖库: https://github.com/Li-Lian1069/micropython-ulogger\n\n### 快速开始\n我们先来建立一个最简单的处理路由:\n```python\n# connect to network...\n\nfrom urouter import uRouter\napp = uRouter()\n\n@app.route(\"/\")\ndef index():\n    return \"\u003cp\u003eHello World!\u003cp\u003e\"\n\napp.serve_forever()\n```\n这样, 当你在浏览器地址栏输入开发板的局域网ip并按下回车时, 你的浏览器就会显示出 `Hello World!` 了.\n\n#### 监听其他访问方式\n有时候你可能需要使用 `POST` 方式获取一些数据, 我们可以设置让本框架监听 `POST` 的访问方式:\n```python\nfrom urouter import uRouter, POST\napp = uRouter()\n\n@app.router(\"/login\", methods=(POST,))\ndef login():\n    request = app.request\n    form = request.form\n    if form.get(\"user_name\") == 'admin' and form.get(\"passwd\") == \"admin\":\n        return \"Login succeed!\"\n    else:\n        return \"Login failed, check your username or you password!\"\n```\n在上面的例子中, 我们使用了 `request` 对象, 这个对象用来获取关于浏览器请求的一些信息, 例如 headers, 客户端地址, 访问方式等等. 我们使用的 `form` 对象就是从中获取的, `form` 对象是一个字典, 可以使用 `get` 方式来获取相应的数据.\n\n注意我们使用了 `methods=(POST,)` 来指定监听方式为 `POST`, 这个 `POST` 对象是我们从 `urouter` 中导入的一个常量, 需要注意的是: `methods` 参数必须传入一个可迭代的对象(`list` 或者  `tuple`), 一般的情况下推荐使用 `tuple`(如果您不需要动态修改监听方式), 因为在成员数量固定的情况下, `tuple` 比 `list` 更加节省内存, 在嵌入式设备中内存是非常有限的, 我们要在最大程度上节省不必要的内存开支.\n\n\n### 获取更多开发信息, 详见 [开发文档](https://urouter.m-jay.cn)\n\n## 注意:\n在使用本框架时, 应注意:\n- 请不要在让本模块的监听和处理函数在多个线程中运行(即保持本框架工作在单线程模式), 你可以专门申请一个线程让他工作, 但是不要让他同时处理多件事情, 这会造成 `context` 对象的混乱.\n例子:\n```python\n# connect to network...\n\nfrom _thread import start_new_thread\nfrom urouter import uRouter\napp = uRouter()\n\n@app.route(\"/\")\ndef index():\n    return \"\u003cp\u003eHello World!\u003cp\u003e\"\n\nstart_new_thread(app.serve_forever)\n# that is ok.\n\nstart_new_thread(app.serve_forever)\n# Do not start two server.\n```\n\n```python\n# connect to network...\n\nfrom _thread import start_new_thread\nfrom urouter import uRouter\napp = uRouter()\n\n@app.route(\"/\")\ndef index():\n    return \"\u003cp\u003eHello World!\u003cp\u003e\"\n\nwhile True:\n    if app.check():\n        start_new_thread(app.serve_once)\n# Don't do that.\n```\n- 本模块可以同时拥有多个app实例, 可以同时工作(未经充分测试)\n- 不要随意修改本框架的 `context` 对象(例如 `request` 和 `response`, `session`), 理论上, 您不应该修改任何未声明可以被修改的内容.\n\n","funding_links":[],"categories":["Libraries"],"sub_categories":["Communications"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmajoson-chen%2Fmicropython-urouter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmajoson-chen%2Fmicropython-urouter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmajoson-chen%2Fmicropython-urouter/lists"}