{ "info": { "author": "Keijack", "author_email": "keijack.wu@gmail.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7" ], "description": "# python-simple-http-server\n\n## Discription\n\nThis is a simple http server, use MVC like design.\n\n## Support Python Version\n\nPython 2.7 / 3.6+ (It should also work at 3.5, not test)\n\n## Why choose\n\n* Lightway.\n* Filter chain support.\n* Spring MVC like request mapping.\n* Easy to use.\n* Free style controller writing.\n\n## How to use\n\n### Install\n\n```shell\npip install simple_http_server\n```\n\n### Write Controllers\n\n```python\n\nfrom simple_http_server import request_map\nfrom simple_http_server import Response\nfrom simple_http_server import MultipartFile\nfrom simple_http_server import Parameter\nfrom simple_http_server import Parameters\nfrom simple_http_server import Header\nfrom simple_http_server import JSONBody\nfrom simple_http_server import HttpError\nfrom simple_http_server import StaticFile\nfrom simple_http_server import Headers\nfrom simple_http_server import Cookies\nfrom simple_http_server import Cookie\nfrom simple_http_server import Redirect\n\n\n@request_map(\"/index\")\ndef my_ctrl():\n return {\"code\": 0, \"message\": \"success\"} # You can return a dictionary, a string or a `simple_http_server.simple_http_server.Response` object.\n\n\n@request_map(\"/say_hello\", method=[\"GET\", \"POST\"])\ndef my_ctrl2(name, name2=Parameter(\"name\", default=\"KEIJACK\")):\n \"\"\"name and name2 is the same\"\"\"\n return \"hello, %s, %s\" % (name, name2)\n\n\n@request_map(\"/error\")\ndef my_ctrl3():\n return Response(status_code=500)\n\n\n@request_map(\"/exception\")\ndef exception_ctrl():\n raise HttpError(400, \"Exception\")\n\n@request_map(\"/upload\", method=\"GET\")\ndef show_upload():\n root = os.path.dirname(os.path.abspath(__file__))\n return StaticFile(\"%s/my_dev/my_test_index.html\" % root, \"text/html; charset=utf-8\")\n\n@request_map(\"/upload\", method=\"POST\")\ndef my_upload(img=MultipartFile(\"img\")):\n root = os.path.dirname(os.path.abspath(__file__))\n img.save_to_file(root + \"/my_dev/imgs/\" + img.filename)\n return \"upload ok!\"\n\n\n@request_map(\"/post_txt\", method=\"POST\")\ndef normal_form_post(txt):\n return \"hi, %s\" % txt\n\n@request_map(\"/tuple\")\ndef tuple_results():\n # The order here is not important, we consider the first `int` value as status code,\n # All `Headers` object will be sent to the response\n # And the first valid object whose type in (str, unicode, dict, StaticFile, bytes) will\n # be considered as the body\n return 200, Headers({\"my-header\": \"headers\"}), {\"success\": True}\n\n\"\"\"\n\" Cookie_sc will not be written to response. It's just some kind of default\n\" value\n\"\"\"\n@request_map(\"tuple_cookie\")\ndef tuple_with_cookies(all_cookies=Cookies(), cookie_sc=Cookie(\"sc\")):\n print(\"=====> cookies \")\n print(all_cookies)\n print(\"=====> cookie sc \")\n print(cookie_sc)\n print(\"======<\")\n import datetime\n expires = datetime.datetime(2018, 12, 31)\n\n cks = Cookies()\n # cks = cookies.SimpleCookie() # you could also use the build-in cookie objects\n cks[\"ck1\"] = \"keijack\"\n cks[\"ck1\"][\"path\"] = \"/\"\n cks[\"ck1\"][\"expires\"] = expires.strftime(Cookies.EXPIRE_DATE_FORMAT)\n # You can ignore status code, headers, cookies even body in this tuple.\n return Header({\"xx\": \"yyy\"}), cks, \"OK\"\n\n\"\"\"\n\" If you visit /a/b/xyz/x\uff0cthis controller function will be called, and `path_val` will be `xyz`\n\"\"\"\n@request_map(\"/a/b/{path_val}/x\")\ndef my_path_val_ctr(path_val=PathValue()):\n return \"%s\" % path_val\n\n\n@request_map(\"/redirect\")\ndef redirect():\n return Redirect(\"/index\")\n```\n\n### Write filters\n\n```python\nfrom simple_http_server import filter_map\n\n# Please note filter will map a regular expression, not a concrect url.\n@filter_map(\"^/tuple\")\ndef filter_tuple(ctx):\n print(\"---------- through filter ---------------\")\n # add a header to request header\n ctx.request.headers[\"filter-set\"] = \"through filter\"\n if \"user_name\" not in ctx.request.parameter:\n ctx.response.send_redirect(\"/index\")\n elif \"pass\" not in ctx.request.parameter:\n ctx.response.send_error(400, \"pass should be passed\")\n # you can also raise a HttpError\n # raise HttpError(400, \"pass should be passed\")\n else:\n # you should always use do_chain method to go to the next\n ctx.do_chain()\n```\n\n### Start your server\n\n```python\n# If you place the controllers method in the other files, you should import them here.\n\nimport simple_http_server.server as server\nimport my_test_ctrl\n\n\ndef main(*args):\n server.start()\n\nif __name__ == \"__main__\":\n main()\n```\n\n## Problems\n\n### Unicode supporting\n\nAlthough I have tried to fixed the unicode problem in python 2.7, it still may cause some problems for the reason that python 2.7 is quite unfriendly for unicodes. The best way to ensure unicode works is to use python 3.6+\n\n### Multipul threading safety\n\nFor this is a SIMPLE http server, I have not done much work to ensure multipul threading safety. It may cause some problem if you trid to write data to `Request` and `Response` objects in multipul threads in one request scope (including in filters and controller functions).\n\n", "description_content_type": "", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/keijack/python-simple-http-server", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "simple-http-server", "package_url": "https://pypi.org/project/simple-http-server/", "platform": "", "project_url": "https://pypi.org/project/simple-http-server/", "project_urls": { "Homepage": "https://github.com/keijack/python-simple-http-server" }, "release_url": "https://pypi.org/project/simple-http-server/0.1.7/", "requires_dist": null, "requires_python": "", "summary": "This is a simple http server, use MVC like design.", "version": "0.1.7" }, "last_serial": 4469734, "releases": { "0.1.7": [ { "comment_text": "", "digests": { "md5": "9ff76a258cd564303ee3070d61c57441", "sha256": "c64dbf1eedca4896ecd69fc5c5bec2dd78b8fc9b10bd6725e3b625ed9aef8301" }, "downloads": -1, "filename": "simple_http_server-0.1.7-py3-none-any.whl", "has_sig": false, "md5_digest": "9ff76a258cd564303ee3070d61c57441", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 21019, "upload_time": "2018-11-09T14:17:14", "url": "https://files.pythonhosted.org/packages/7a/89/f1883d3e9ff76e2cf2ff962768c54318612c9bb7bd1e71a19cea7440c996/simple_http_server-0.1.7-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "ded16cfb8a16e8510ae823a03edeb6f6", "sha256": "3be8f1775be377109b8c471fa6acf5369e9cd96dbc15ce4c38538c51562e90e6" }, "downloads": -1, "filename": "simple_http_server-0.1.7.tar.gz", "has_sig": false, "md5_digest": "ded16cfb8a16e8510ae823a03edeb6f6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12092, "upload_time": "2018-11-09T14:17:16", "url": "https://files.pythonhosted.org/packages/af/15/352f455cd850b89a731f03b1f64e390840e10a95eff5b1a21f30c2ce4a91/simple_http_server-0.1.7.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "9ff76a258cd564303ee3070d61c57441", "sha256": "c64dbf1eedca4896ecd69fc5c5bec2dd78b8fc9b10bd6725e3b625ed9aef8301" }, "downloads": -1, "filename": "simple_http_server-0.1.7-py3-none-any.whl", "has_sig": false, "md5_digest": "9ff76a258cd564303ee3070d61c57441", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 21019, "upload_time": "2018-11-09T14:17:14", "url": "https://files.pythonhosted.org/packages/7a/89/f1883d3e9ff76e2cf2ff962768c54318612c9bb7bd1e71a19cea7440c996/simple_http_server-0.1.7-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "ded16cfb8a16e8510ae823a03edeb6f6", "sha256": "3be8f1775be377109b8c471fa6acf5369e9cd96dbc15ce4c38538c51562e90e6" }, "downloads": -1, "filename": "simple_http_server-0.1.7.tar.gz", "has_sig": false, "md5_digest": "ded16cfb8a16e8510ae823a03edeb6f6", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 12092, "upload_time": "2018-11-09T14:17:16", "url": "https://files.pythonhosted.org/packages/af/15/352f455cd850b89a731f03b1f64e390840e10a95eff5b1a21f30c2ce4a91/simple_http_server-0.1.7.tar.gz" } ] }