coolapi


Namecoolapi JSON
Version 1.0.3 PyPI version JSON
download
home_pageNone
Summary简单好用的异步 web 框架
upload_time2023-07-14 17:41:13
maintainerNone
docs_urlNone
authorNone
requires_python>=3.7
licenseNone
keywords coolapi tornado flask fastapi django aiohttp web
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 项目描述

简单好用的异步 web 框架。

# 关于作者

作者:lcctoor.com

域名:lcctoor.com

邮箱:lcctoor@outlook.com

[主页](https://lcctoor.github.io/me/) \| [微信](https://lcctoor.github.io/me/author/WeChatQR-max.jpg) \| [Python交流群](https://lcctoor.github.io/me/lccpy/WechatReadersGroupQR-original.jpg) \| [捐赠](https://lcctoor.github.io/me/donation/donationQR-1rmb-max.jpg)

# Bug提交、功能提议

您可以通过 [Github-Issues](https://github.com/lcctoor/lccpy/issues)、[微信](https://lcctoor.github.io/me/author/WeChatQR-max.jpg) 与我联系。

# 安装

```
pip install coolapi
```

# 教程

#### 导入

```python
import asyncio
from coolapi import handler, creat_server
```

#### 一个最简单的例子

```python
class LoginHandler(handler):
    urls = ['/login/?']
    # url必须以'/'开头
    # '?'是正则表达式语法, 相当于 urls = ['/login', '/login/']

    async def get(self):
        return self.write('You are accessing the get method of login')
  
    async def post(self):
        return self.write('You are accessing the post method of login')

async def main():
    handlers = [LoginHandler]
    http_server = creat_server(handlers=handlers, port=5050)  # 启动http异步任务
    await asyncio.sleep(float('inf'))  # float('inf')表示无穷大

asyncio.run(main())

# 此时可在浏览器访问:
# http://localhost:5050/login
# http://localhost:5050/login/
```

#### 设置 cookie

```python
class PersonalHandler(handler):
    urls = ['/personal/?']

    async def get(self):
        self.set_cookie(name='username', value='tony')
        return self.write('Welcome to the personal center!')
```

#### 重定向

```python
def judge_login(self):
    ...

class PersonalHandler(handler):
    urls = ['/personal/?']

    async def get(self):
        if judge_login(self):
            return self.write('Welcome to the personal center!')
        else:
            return self.redirect('/login')  # 重定向到'/login'
```

#### 常用的视图操作

| 功能         | 代码                                           |
| ------------ | ---------------------------------------------- |
| 返回响应     | return self.write('......')                    |
| 设置 cookie  | self.set_cookie(name='username', value='tony') |
| 获取 cookie  | self.get_cookie(name='username')               |
| 重定向       | return self.redirect('/login')                 |
| 获取访客ip   | ip = self.get_ip( )                            |
| 获取请求数据 | body = self.get_body( )                        |

#### 使用通配路由

路由支持正则表达式,因此可实现通配路由:

```python
class ArticleHandler(handler):
    urls = ['/article/(\d+)/([a-z0-9]+)/?']

    async def get(self, userid, article_id):
        text = f"The article you are reading is '{article_id}' written by {userid}"
        return self.write(text)

# 运行后可在浏览器访问:
# http://localhost:5050/article/641872/r9mf44
# http://localhost:5050/article/740357/8d1h6d
# ...
```

```python
class DocumentionHandler(handler):
    urls = ['/documention/python([\d.]+)/?']

    async def get(self, version):
        text = f"You are reading the documentation for Python{version}"
        return self.write(text)

# 运行后可在浏览器访问:
# http://localhost:5050/documention/python3
# http://localhost:5050/documention/python3.9
# ...
```

#### 启动 http 服务

```python
class LoginHandler(handler):
    urls = ['/login/?']
    async def get(self):
        return self.write('You are accessing the get method of login')

class ArticleHandler(handler):
    ...

async def main():
    http_server = creat_server(
  
        handlers = [LoginHandler, ArticleHandler],  # 视图类列表
  
        port = 5050,  # 端口, 默认为80
  
        static_path = None,  # 静态文件服务, 默认为None
  
        debug = False,  # 启用调试模式, 默认为False
  
        # 支持更多参数, 可参考 https://www.tornadoweb.org/en/stable/web.html#tornado.web.Application
    )
  
    await asyncio.sleep(float('inf'))  # float('inf')表示无穷大

asyncio.run(main())
```

#### 更多用法

coolapi 是对 tornado 的二次封装,其中:

1、coolapi.handler(视图类)继承自 [tornado.web.RequestHandler](https://www.tornadoweb.org/en/stable/web.html#request-handlers),可使用 tornado.web.RequestHandler 的任何功能。

2、creat_server 支持 [tornado.web.Application](https://www.tornadoweb.org/en/stable/web.html#tornado.web.Application) 的所有参数。

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "coolapi",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.7",
    "maintainer_email": null,
    "keywords": "coolapi,tornado,flask,fastapi,django,aiohttp,web",
    "author": null,
    "author_email": "\"lcctoor.com\" <lcctoor@outlook.com>",
    "download_url": "https://files.pythonhosted.org/packages/f4/7f/f928caf688b0e1cad510093cc5665dc63f6188c25133946575ed23e4bc1b/coolapi-1.0.3.tar.gz",
    "platform": null,
    "description": "# \u9879\u76ee\u63cf\u8ff0\n\n\u7b80\u5355\u597d\u7528\u7684\u5f02\u6b65 web \u6846\u67b6\u3002\n\n# \u5173\u4e8e\u4f5c\u8005\n\n\u4f5c\u8005\uff1alcctoor.com\n\n\u57df\u540d\uff1alcctoor.com\n\n\u90ae\u7bb1\uff1alcctoor@outlook.com\n\n[\u4e3b\u9875](https://lcctoor.github.io/me/) \\| [\u5fae\u4fe1](https://lcctoor.github.io/me/author/WeChatQR-max.jpg) \\| [Python\u4ea4\u6d41\u7fa4](https://lcctoor.github.io/me/lccpy/WechatReadersGroupQR-original.jpg) \\| [\u6350\u8d60](https://lcctoor.github.io/me/donation/donationQR-1rmb-max.jpg)\n\n# Bug\u63d0\u4ea4\u3001\u529f\u80fd\u63d0\u8bae\n\n\u60a8\u53ef\u4ee5\u901a\u8fc7 [Github-Issues](https://github.com/lcctoor/lccpy/issues)\u3001[\u5fae\u4fe1](https://lcctoor.github.io/me/author/WeChatQR-max.jpg) \u4e0e\u6211\u8054\u7cfb\u3002\n\n# \u5b89\u88c5\n\n```\npip install coolapi\n```\n\n# \u6559\u7a0b\n\n#### \u5bfc\u5165\n\n```python\nimport asyncio\nfrom coolapi import handler, creat_server\n```\n\n#### \u4e00\u4e2a\u6700\u7b80\u5355\u7684\u4f8b\u5b50\n\n```python\nclass LoginHandler(handler):\n    urls = ['/login/?']\n    # url\u5fc5\u987b\u4ee5'/'\u5f00\u5934\n    # '?'\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5, \u76f8\u5f53\u4e8e urls = ['/login', '/login/']\n\n    async def get(self):\n        return self.write('You are accessing the get method of login')\n  \n    async def post(self):\n        return self.write('You are accessing the post method of login')\n\nasync def main():\n    handlers = [LoginHandler]\n    http_server = creat_server(handlers=handlers, port=5050)  # \u542f\u52a8http\u5f02\u6b65\u4efb\u52a1\n    await asyncio.sleep(float('inf'))  # float('inf')\u8868\u793a\u65e0\u7a77\u5927\n\nasyncio.run(main())\n\n# \u6b64\u65f6\u53ef\u5728\u6d4f\u89c8\u5668\u8bbf\u95ee\uff1a\n# http://localhost:5050/login\n# http://localhost:5050/login/\n```\n\n#### \u8bbe\u7f6e cookie\n\n```python\nclass PersonalHandler(handler):\n    urls = ['/personal/?']\n\n    async def get(self):\n        self.set_cookie(name='username', value='tony')\n        return self.write('Welcome to the personal center!')\n```\n\n#### \u91cd\u5b9a\u5411\n\n```python\ndef judge_login(self):\n    ...\n\nclass PersonalHandler(handler):\n    urls = ['/personal/?']\n\n    async def get(self):\n        if judge_login(self):\n            return self.write('Welcome to the personal center!')\n        else:\n            return self.redirect('/login')  # \u91cd\u5b9a\u5411\u5230'/login'\n```\n\n#### \u5e38\u7528\u7684\u89c6\u56fe\u64cd\u4f5c\n\n| \u529f\u80fd         | \u4ee3\u7801                                           |\n| ------------ | ---------------------------------------------- |\n| \u8fd4\u56de\u54cd\u5e94     | return self.write('......')                    |\n| \u8bbe\u7f6e cookie  | self.set_cookie(name='username', value='tony') |\n| \u83b7\u53d6 cookie  | self.get_cookie(name='username')               |\n| \u91cd\u5b9a\u5411       | return self.redirect('/login')                 |\n| \u83b7\u53d6\u8bbf\u5ba2ip   | ip = self.get_ip( )                            |\n| \u83b7\u53d6\u8bf7\u6c42\u6570\u636e | body = self.get_body( )                        |\n\n#### \u4f7f\u7528\u901a\u914d\u8def\u7531\n\n\u8def\u7531\u652f\u6301\u6b63\u5219\u8868\u8fbe\u5f0f\uff0c\u56e0\u6b64\u53ef\u5b9e\u73b0\u901a\u914d\u8def\u7531\uff1a\n\n```python\nclass ArticleHandler(handler):\n    urls = ['/article/(\\d+)/([a-z0-9]+)/?']\n\n    async def get(self, userid, article_id):\n        text = f\"The article you are reading is '{article_id}' written by {userid}\"\n        return self.write(text)\n\n# \u8fd0\u884c\u540e\u53ef\u5728\u6d4f\u89c8\u5668\u8bbf\u95ee\uff1a\n# http://localhost:5050/article/641872/r9mf44\n# http://localhost:5050/article/740357/8d1h6d\n# ...\n```\n\n```python\nclass DocumentionHandler(handler):\n    urls = ['/documention/python([\\d.]+)/?']\n\n    async def get(self, version):\n        text = f\"You are reading the documentation for Python{version}\"\n        return self.write(text)\n\n# \u8fd0\u884c\u540e\u53ef\u5728\u6d4f\u89c8\u5668\u8bbf\u95ee\uff1a\n# http://localhost:5050/documention/python3\n# http://localhost:5050/documention/python3.9\n# ...\n```\n\n#### \u542f\u52a8 http \u670d\u52a1\n\n```python\nclass LoginHandler(handler):\n    urls = ['/login/?']\n    async def get(self):\n        return self.write('You are accessing the get method of login')\n\nclass ArticleHandler(handler):\n    ...\n\nasync def main():\n    http_server = creat_server(\n  \n        handlers = [LoginHandler, ArticleHandler],  # \u89c6\u56fe\u7c7b\u5217\u8868\n  \n        port = 5050,  # \u7aef\u53e3, \u9ed8\u8ba4\u4e3a80\n  \n        static_path = None,  # \u9759\u6001\u6587\u4ef6\u670d\u52a1, \u9ed8\u8ba4\u4e3aNone\n  \n        debug = False,  # \u542f\u7528\u8c03\u8bd5\u6a21\u5f0f, \u9ed8\u8ba4\u4e3aFalse\n  \n        # \u652f\u6301\u66f4\u591a\u53c2\u6570, \u53ef\u53c2\u8003 https://www.tornadoweb.org/en/stable/web.html#tornado.web.Application\n    )\n  \n    await asyncio.sleep(float('inf'))  # float('inf')\u8868\u793a\u65e0\u7a77\u5927\n\nasyncio.run(main())\n```\n\n#### \u66f4\u591a\u7528\u6cd5\n\ncoolapi \u662f\u5bf9 tornado \u7684\u4e8c\u6b21\u5c01\u88c5\uff0c\u5176\u4e2d\uff1a\n\n1\u3001coolapi.handler\uff08\u89c6\u56fe\u7c7b\uff09\u7ee7\u627f\u81ea [tornado.web.RequestHandler](https://www.tornadoweb.org/en/stable/web.html#request-handlers)\uff0c\u53ef\u4f7f\u7528 tornado.web.RequestHandler \u7684\u4efb\u4f55\u529f\u80fd\u3002\n\n2\u3001creat_server \u652f\u6301 [tornado.web.Application](https://www.tornadoweb.org/en/stable/web.html#tornado.web.Application) \u7684\u6240\u6709\u53c2\u6570\u3002\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "\u7b80\u5355\u597d\u7528\u7684\u5f02\u6b65 web \u6846\u67b6",
    "version": "1.0.3",
    "project_urls": {
        "HomePage": "https://lcctoor.github.io/lccpy/?package=coolapi"
    },
    "split_keywords": [
        "coolapi",
        "tornado",
        "flask",
        "fastapi",
        "django",
        "aiohttp",
        "web"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "26b2a415a87cc389daa6e0b41ad224fc280c2a4aa968d4d2f0f8a67173c04533",
                "md5": "529e2b249c06b77b6a640ab9b594844d",
                "sha256": "60e02ab3c1681ac4da1414583a61514519f44f8d9909f3ca2a0a78c4669bf2c5"
            },
            "downloads": -1,
            "filename": "coolapi-1.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "529e2b249c06b77b6a640ab9b594844d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.7",
            "size": 11796,
            "upload_time": "2023-07-14T17:41:11",
            "upload_time_iso_8601": "2023-07-14T17:41:11.312897Z",
            "url": "https://files.pythonhosted.org/packages/26/b2/a415a87cc389daa6e0b41ad224fc280c2a4aa968d4d2f0f8a67173c04533/coolapi-1.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "f47ff928caf688b0e1cad510093cc5665dc63f6188c25133946575ed23e4bc1b",
                "md5": "2f2340ddabdb36eb42c6b9c3fdd4b46f",
                "sha256": "91853063910d2ad2cf536cac7fd74bba528c1e93731ecdc5b3ef174a54ce85f0"
            },
            "downloads": -1,
            "filename": "coolapi-1.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "2f2340ddabdb36eb42c6b9c3fdd4b46f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.7",
            "size": 8293,
            "upload_time": "2023-07-14T17:41:13",
            "upload_time_iso_8601": "2023-07-14T17:41:13.895415Z",
            "url": "https://files.pythonhosted.org/packages/f4/7f/f928caf688b0e1cad510093cc5665dc63f6188c25133946575ed23e4bc1b/coolapi-1.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-14 17:41:13",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "coolapi"
}
        
Elapsed time: 2.33882s