{"id":553,"date":"2019-09-02T20:34:07","date_gmt":"2019-09-02T11:34:07","guid":{"rendered":"http:\/\/hirlab.net\/nblog\/?p=553"},"modified":"2020-01-20T18:08:45","modified_gmt":"2020-01-20T09:08:45","slug":"responder%e3%81%ae%e4%bd%bf%e3%81%84%e6%96%b9%e3%81%be%e3%81%a8%e3%82%81%e3%80%90responder-v1-3-1%e3%80%91","status":"publish","type":"post","link":"https:\/\/hirlab.net\/nblog\/category\/programming\/art_553\/","title":{"rendered":"Responder\u306e\u4f7f\u3044\u65b9\u307e\u3068\u3081\u3010Responder v1.3.1\u3011"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_11 counter-hierarchy counter-decimal ez-toc-light-blue\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\">\u30b3\u30f3\u30c6\u30f3\u30c4<\/p>\n<span class=\"ez-toc-title-toggle\"><a class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\"><i class=\"ez-toc-glyphicon ez-toc-icon-toggle\"><\/i><\/a><\/span><\/div>\n<nav><ul class=\"ez-toc-list ez-toc-list-level-1\"><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/hirlab.net\/nblog\/category\/programming\/art_553\/#Responder\" title=\"Responder\">Responder<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/hirlab.net\/nblog\/category\/programming\/art_553\/#%E3%83%AD%E3%83%BC%E3%82%AB%E3%83%AB%E3%82%B5%E3%83%BC%E3%83%90%E3%81%AE%E7%AB%8B%E3%81%A1%E4%B8%8A%E3%81%92%E3%81%A8%E3%83%AB%E3%83%BC%E3%83%86%E3%82%A4%E3%83%B3%E3%82%B0\" title=\"\u30ed\u30fc\u30ab\u30eb\u30b5\u30fc\u30d0\u306e\u7acb\u3061\u4e0a\u3052\u3068\u30eb\u30fc\u30c6\u30a4\u30f3\u30b0\">\u30ed\u30fc\u30ab\u30eb\u30b5\u30fc\u30d0\u306e\u7acb\u3061\u4e0a\u3052\u3068\u30eb\u30fc\u30c6\u30a4\u30f3\u30b0<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/hirlab.net\/nblog\/category\/programming\/art_553\/#%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%82%92%E5%88%86%E3%81%91%E3%82%88%E3%81%86\" title=\"\u30d5\u30a1\u30a4\u30eb\u3092\u5206\u3051\u3088\u3046\">\u30d5\u30a1\u30a4\u30eb\u3092\u5206\u3051\u3088\u3046<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/hirlab.net\/nblog\/category\/programming\/art_553\/#run_py\" title=\"run.py\">run.py<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/hirlab.net\/nblog\/category\/programming\/art_553\/#urls_py\" title=\"urls.py\">urls.py<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/hirlab.net\/nblog\/category\/programming\/art_553\/#controllers_py\" title=\"controllers.py\">controllers.py<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/hirlab.net\/nblog\/category\/programming\/art_553\/#Jinja2%E3%81%AE%E3%83%95%E3%82%A3%E3%83%AB%E3%82%BF%E3%82%92%E8%BF%BD%E5%8A%A0%E3%81%99%E3%82%8B\" title=\"Jinja2\u306e\u30d5\u30a3\u30eb\u30bf\u3092\u8ffd\u52a0\u3059\u308b\">Jinja2\u306e\u30d5\u30a3\u30eb\u30bf\u3092\u8ffd\u52a0\u3059\u308b<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/hirlab.net\/nblog\/category\/programming\/art_553\/#Jinja2%E3%81%AE%E3%82%B0%E3%83%AD%E3%83%BC%E3%83%90%E3%83%AB%E5%A4%89%E6%95%B0%E3%82%92%E8%BF%BD%E5%8A%A0%E3%81%99%E3%82%8B\" title=\"Jinja2\u306e\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3092\u8ffd\u52a0\u3059\u308b\">Jinja2\u306e\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3092\u8ffd\u52a0\u3059\u308b<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/hirlab.net\/nblog\/category\/programming\/art_553\/#%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3%EF%BC%8C%E3%83%AD%E3%82%B0%E3%82%A2%E3%82%A6%E3%83%88%E5%87%A6%E7%90%86\" title=\"\u30ed\u30b0\u30a4\u30f3\uff0c\u30ed\u30b0\u30a2\u30a6\u30c8\u51e6\u7406\">\u30ed\u30b0\u30a4\u30f3\uff0c\u30ed\u30b0\u30a2\u30a6\u30c8\u51e6\u7406<\/a><ul class=\"ez-toc-list-level-3\"><li class=\"ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/hirlab.net\/nblog\/category\/programming\/art_553\/#Session%E3%81%AB%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3%E3%83%A6%E3%83%BC%E3%82%B6%E3%83%BC%E3%82%92%E7%99%BB%E9%8C%B2%E3%81%99%E3%82%8B\" title=\"Session\u306b\u30ed\u30b0\u30a4\u30f3\u30e6\u30fc\u30b6\u30fc\u3092\u767b\u9332\u3059\u308b\">Session\u306b\u30ed\u30b0\u30a4\u30f3\u30e6\u30fc\u30b6\u30fc\u3092\u767b\u9332\u3059\u308b<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/hirlab.net\/nblog\/category\/programming\/art_553\/#Cookie%E3%81%AB%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3%E3%83%A6%E3%83%BC%E3%82%B6%E3%82%92%E7%99%BB%E9%8C%B2%E3%81%99%E3%82%8B\" title=\"Cookie\u306b\u30ed\u30b0\u30a4\u30f3\u30e6\u30fc\u30b6\u3092\u767b\u9332\u3059\u308b\">Cookie\u306b\u30ed\u30b0\u30a4\u30f3\u30e6\u30fc\u30b6\u3092\u767b\u9332\u3059\u308b<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-3\"><a class=\"ez-toc-link ez-toc-heading-12\" href=\"https:\/\/hirlab.net\/nblog\/category\/programming\/art_553\/#%E3%83%AD%E3%82%B0%E3%82%A2%E3%82%A6%E3%83%88%E5%87%A6%E7%90%86\" title=\"\u30ed\u30b0\u30a2\u30a6\u30c8\u51e6\u7406\">\u30ed\u30b0\u30a2\u30a6\u30c8\u51e6\u7406<\/a><\/li><\/ul><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-13\" href=\"https:\/\/hirlab.net\/nblog\/category\/programming\/art_553\/#404%E3%82%A8%E3%83%A9%E3%83%BC%E3%83%8F%E3%83%B3%E3%83%89%E3%83%AA%E3%83%B3%E3%82%B0\" title=\"404\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\">404\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0<\/a><\/li><li class=\"ez-toc-page-1 ez-toc-heading-level-2\"><a class=\"ez-toc-link ez-toc-heading-14\" href=\"https:\/\/hirlab.net\/nblog\/category\/programming\/art_553\/#%E3%81%BE%E3%81%A8%E3%82%81\" title=\"\u307e\u3068\u3081\">\u307e\u3068\u3081<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"Responder\"><\/span>Responder<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Responder\u3068\u306fPython\u3067\u5229\u7528\u53ef\u80fd\u306aWebAPI\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3059\uff0e<\/p>\n<p>\u516c\u5f0f\u306e\u6700\u65b0\u60c5\u5831\u3084\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u306f \u00a0<span style=\"font-size: 20px;\"><a href=\"https:\/\/python-responder.org\/en\/latest\/\" target=\"_blank\" rel=\"noopener noreferrer\">A familiar HTTP Service Framework<\/a>\u00a0<span style=\"font-size: 16px;\">\u3067\u3059\uff0e<\/span><\/span><\/p>\n<p>\u73fe\u5728\u6ce8\u76ee\u3055\u308c\u3066\u3044\u308bResponder\u3067\u3059\u304c\uff0c<mark>\u3042\u307e\u308a\u306b\u3082\u60c5\u5831\u304c\u5c11\u306a\u3059\u304e\u308b<\/mark>\u306e\u304c\u73fe\u72b6\u3067\u3059\uff0e<\/p>\n<p>\u65e5\u672c\u8a9e\u3067\u7d39\u4ecb\u3057\u3066\u3044\u308b\u30da\u30fc\u30b8\u3082\u3042\u308a\u307e\u3059\u304c\uff0c\u516c\u5f0f\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u3092\u548c\u8a33\u3057\u305f\u3082\u306e\u3070\u304b\u308a\u3067\u3059\uff0e<\/p>\n<p>\u3057\u304b\u3057\uff0c\u3068\u3042\u308b\u4e8b\u60c5\u3067\u79c1\u81ea\u8eab\u304cResponder\u3092\u4f7f\u308f\u306a\u3051\u308c\u3070\u3044\u3051\u306a\u304f\u306a\u308a\uff0c\u8272\u3005\u3068\u8abf\u67fb\u3092\u3057\uff0c\u7d50\u679c\u3068\u3057\u3066\u624b\u306b\u5165\u308c\u305f\u77e5\u8b58\u3092\u3053\u306e\u8a18\u4e8b\u3067\u7d39\u4ecb\u3057\u307e\u3059\uff0e<\/p>\n<p>\u30cd\u30c3\u30c8\u306e\u60c5\u5831\u3092\u304b\u304d\u96c6\u3081\u305f\u90e8\u5206\u3068\uff0c\u30bd\u30fc\u30b9\u3092\u898b\u3066\u300c\u306a\u308b\u307b\u3069\u3053\u3046\u3044\u3046\u3053\u3068\u304b\uff01\u300d\u3068\u5224\u660e\u3057\u305f\u90e8\u5206\u304c\u5165\u308a\u6df7\u3058\u3063\u3066\u3044\u307e\u3059\uff0e<\/p>\n<p>\u53c2\u8003\u307e\u3067\u306b\uff0e\uff0e\uff0e<\/p>\n<p>\u3061\u306a\u307f\u306bResponder\u306fPython3.6\u4ee5\u964d\u3067\u306a\u3044\u3068\u52d5\u4f5c\u3057\u307e\u305b\u3093\uff0e<\/p>\n<p>\u307e\u305f\uff0c\u672c\u8a18\u4e8b\u3092\u66f8\u3044\u3066\u3044\u308b\u6bb5\u968e\u3067\u306f<mark>Responder\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306f1.3.1<\/mark>\u3067\u3059\uff0e<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E3%83%AD%E3%83%BC%E3%82%AB%E3%83%AB%E3%82%B5%E3%83%BC%E3%83%90%E3%81%AE%E7%AB%8B%E3%81%A1%E4%B8%8A%E3%81%92%E3%81%A8%E3%83%AB%E3%83%BC%E3%83%86%E3%82%A4%E3%83%B3%E3%82%B0\"><\/span>\u30ed\u30fc\u30ab\u30eb\u30b5\u30fc\u30d0\u306e\u7acb\u3061\u4e0a\u3052\u3068\u30eb\u30fc\u30c6\u30a4\u30f3\u30b0<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u30ed\u30fc\u30ab\u30eb\u30b5\u30fc\u30d0\u306e\u7acb\u3061\u4e0a\u3052\u306f\uff0c\u30c1\u30e5\u30fc\u30c8\u30ea\u30a2\u30eb\u306b\u8f09\u3063\u3066\u3044\u308b\u901a\u308a\uff0e<\/p>\n<p>\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u304c\u6700\u4f4e\u9650\u3042\u308c\u3070OK\u3067\u3059\uff0e<\/p>\n<pre class=\"lang:python decode:true\">import responder\r\napi = responder.API()\r\n\r\nif __name__ == '__main__':\r\n    api.run()<\/pre>\n<p>\u3082\u3057\u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u304c\u66f8\u3044\u3066\u3042\u308b\u30d5\u30a1\u30a4\u30eb\u3092<mark><strong>run.py<\/strong><\/mark>\u3068\u3059\u308b\u306a\u3089\u3070\uff0c<\/p>\n<pre class=\"lang:sh decode:true \">$ python run.py<\/pre>\n<p>\u3067OK\u3067\u3059\uff0e<\/p>\n<p>\u305d\u3057\u3066\uff0c\u30eb\u30fc\u30c6\u30a4\u30f3\u30b0\u306f<\/p>\n<pre class=\"lang:python decode:true\">import responder\r\napi = responder.API()\r\n\r\n@api.route('\/')\r\ndef index(req, resp):\r\n    resp.content = api.template('index.html')\r\n\r\n\r\nif __name__ == '__main__':\r\n    api.run()<\/pre>\n<p>\u306e\u3088\u3046\u306b\u3057\u307e\u3059\uff0e<\/p>\n<p><span style=\"font-size: 24px;\">\u304c\uff0c<\/span><\/p>\n<p>\u3053\u3046\u3044\u3046\u98a8\u306b\u66f8\u3044\u3066\u3044\u304f\u3068\uff0c<mark><strong>run.py<\/strong><\/mark>\u3072\u3068\u3064\u306e\u30d5\u30a1\u30a4\u30eb\u306b\u66f8\u304b\u306a\u3051\u308c\u3070\u3044\u3051\u306a\u304f\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\uff0e<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB%E3%82%92%E5%88%86%E3%81%91%E3%82%88%E3%81%86\"><\/span>\u30d5\u30a1\u30a4\u30eb\u3092\u5206\u3051\u3088\u3046<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u30b9\u30de\u30fc\u30c8\u306b\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u305f\u3081\u306b\uff0c\u307e\u305f\u7ba1\u7406\u3057\u3084\u3059\u3044\u3088\u3046\u306b\u30d5\u30a1\u30a4\u30eb\u306f\u7528\u9014\u306b\u3088\u3063\u3066\u5206\u3051\u308b\u306e\u304c\u666e\u901a\u3067\u3059\uff0e<\/p>\n<p>\u306a\u306e\u3067\u5206\u3051\u307e\u3059\uff0e<\/p>\n<p>\u4f8b\u3048\u3070\uff0c<mark><strong>run.py<\/strong>\u306f\u30b5\u30fc\u30d0\u7acb\u3061\u4e0a\u3052\u7528<\/mark>\u306b\u3057\u307e\u3059\uff0e<\/p>\n<p>\u305d\u3057\u3066\u65b0\u305f\u306b\uff0c<mark>\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u5c02\u7528\u306e<strong>urls.py<\/strong><\/mark>\uff0c\u5404\u30eb\u30fc\u30c6\u30a4\u30f3\u30b0\u306e<mark>\u51e6\u7406\u5c02\u7528\u306e<strong>controllers.py<\/strong><\/mark>\u3068\u5206\u3051\u307e\u3059\uff0e<\/p>\n<p>\u305d\u3046\u3059\u308b\u3068\uff0c\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u3051\u307e\u3059\uff0e<\/p>\n<h3><span class=\"ez-toc-section\" id=\"run_py\"><\/span>run.py<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre class=\"lang:python decode:true \">from urls import api\r\n\r\nif __name__ == '__main__':\r\n    api.run()\r\n<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"urls_py\"><\/span>urls.py<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre class=\"lang:ps decode:true\">from controllers import api\r\nfrom controllers import IndexController, CreateController\r\n\r\napi.add_route('\/', IndexController)\r\napi.add_route('\/create\/{hogehoge}', CreateController)<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"controllers_py\"><\/span>controllers.py<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<pre class=\"lang:c++ decode:true \">import responder\r\napi = responder.API()\r\n\r\n\r\nclass IndexController:\r\n    async def on_get(self, req, resp):\r\n        resp.html = api.template('index.html')\r\n\r\n\r\nclass CreateController:\r\n    async def on_get(self, req, resp, hogehoge):\r\n        # \u4f55\u304b\u3057\u3089\u306e\u51e6\u7406\r\n        pass\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>\u3053\u306e\u65b9\u304c\u30b9\u30de\u30fc\u30c8\u3067\u3059\uff0e<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Jinja2%E3%81%AE%E3%83%95%E3%82%A3%E3%83%AB%E3%82%BF%E3%82%92%E8%BF%BD%E5%8A%A0%E3%81%99%E3%82%8B\"><\/span>Jinja2\u306e\u30d5\u30a3\u30eb\u30bf\u3092\u8ffd\u52a0\u3059\u308b<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p><img loading=\"lazy\" class=\"aligncenter size-full wp-image-566\" src=\"https:\/\/hirlab.net\/nblog\/wp-content\/uploads\/2019\/09\/jinja.png\" alt=\"jinja\" width=\"1200\" height=\"480\" srcset=\"https:\/\/hirlab.net\/nblog\/wp-content\/uploads\/2019\/09\/jinja.png 1200w, https:\/\/hirlab.net\/nblog\/wp-content\/uploads\/2019\/09\/jinja-300x120.png 300w, https:\/\/hirlab.net\/nblog\/wp-content\/uploads\/2019\/09\/jinja-768x307.png 768w, https:\/\/hirlab.net\/nblog\/wp-content\/uploads\/2019\/09\/jinja-1024x410.png 1024w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/p>\n<p>Responder\u3067\u306fHTML\u306e\u751f\u6210\u3092<mark><strong>Jinja2<\/strong><\/mark>\u3068\u3044\u3046\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30a8\u30f3\u30b8\u30f3\u3092\u4f7f\u7528\u3057\u3066\u3044\u307e\u3059\uff0e<\/p>\n<p>Responder\u3067\u306f\uff0cJinja2\u306e\u74b0\u5883\u306fAPI\u30af\u30e9\u30b9\u306e<span class=\"lang:python decode:true crayon-inline \">jinja_env<\/span>\u00a0\u3068\u3044\u3046\u30e1\u30f3\u30d0\u5909\u6570\u304c\u6301\u3063\u3066\u3044\u307e\u3059\uff0e<\/p>\n<p>\u306a\u306e\u3067\uff0c\u3082\u3057\u30d5\u30a3\u30eb\u30bf\u3092\u8ffd\u52a0\u3057\u305f\u3051\u308c\u3070\uff0c<\/p>\n<pre class=\"lang:python decode:true \"># \u8ffd\u52a0\u3057\u305f\u3044\u95a2\u6570\r\ndef static_filter(path):\r\n    return '\/static\/' + path\r\n\r\n\r\ndef image_filter(path):\r\n    return '\/static\/images\/' + path\r\n\r\n\r\n# \u30d5\u30a3\u30eb\u30bf\u306f\u8f9e\u66f8\u578b\u306a\u306e\u3067\uff0c\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8ffd\u52a0\u3067\u304d\u308b\r\napi.jinja_env.filters.update(\r\n    static=jinja_env.static_filter,\r\n    image=jinja_env.image_filter,\r\n)<\/pre>\n<p>\u3053\u306e\u3088\u3046\u306b\u66f8\u304d\u307e\u3059\uff0e<\/p>\n<p>\u305d\u3046\u3059\u308b\u3068\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u5074\u3067\u306f\uff0c<\/p>\n<pre class=\"lang:xhtml decode:true\">{{ 'sample.png' | image }}<\/pre>\n<p>\u306e\u3088\u3046\u306b\u30d1\u30a4\u30d7\u3092\u4f7f\u3063\u3066\u66f8\u3051\u307e\u3059\uff0e<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Jinja2%E3%81%AE%E3%82%B0%E3%83%AD%E3%83%BC%E3%83%90%E3%83%AB%E5%A4%89%E6%95%B0%E3%82%92%E8%BF%BD%E5%8A%A0%E3%81%99%E3%82%8B\"><\/span>Jinja2\u306e\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3092\u8ffd\u52a0\u3059\u308b<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>\u3053\u308c\u3082\u5148\u307b\u3069\u306e\u30d5\u30a3\u30eb\u30bf\u3068\u3084\u308b\u3053\u3068\u306f\u4f3c\u3066\u3044\u307e\u3059\uff0e<\/p>\n<p>\u8f9e\u66f8\u578b\u306e\u74b0\u5883\u5909\u6570<span class=\"lang:python decode:true crayon-inline \">globals<\/span>\u00a0\u306b\u8ffd\u52a0\u3059\u308b\u3060\u3051\u3067\u3059\uff0e<\/p>\n<pre class=\"lang:python decode:true \"># \u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u306b\u767b\u9332\u3059\u308b\u5834\u5408\r\napi.jinja_env.globals['static'] = '\/static\/'<\/pre>\n<p>\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u5074\u3067\u306f\uff0c<\/p>\n<pre class=\"lang:xhtml decode:true \">{{ static + '\/style.css' }}<\/pre>\n<p>\u306e\u3088\u3046\u306b\u66f8\u304d\u307e\u3059\uff0e<\/p>\n<p><script async src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<!-- article-mid --><br \/>\n<ins class=\"adsbygoogle\" style=\"display: block;\" data-ad-client=\"ca-pub-7123758399787647\" data-ad-slot=\"2091992211\" data-ad-format=\"auto\" data-full-width-responsive=\"true\"><\/ins><br \/>\n<script>\n     (adsbygoogle = window.adsbygoogle || []).push({});\n<\/script><\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3%EF%BC%8C%E3%83%AD%E3%82%B0%E3%82%A2%E3%82%A6%E3%83%88%E5%87%A6%E7%90%86\"><\/span>\u30ed\u30b0\u30a4\u30f3\uff0c\u30ed\u30b0\u30a2\u30a6\u30c8\u51e6\u7406<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Responder\u3067\u306e\u30ed\u30b0\u30a4\u30f3\u304a\u3088\u3073\u30ed\u30b0\u30a2\u30a6\u30c8\u51e6\u7406\u306fResponder\u306eResponse\u30af\u30e9\u30b9\u304c\u6301\u3064Session\u3068Cookie\u3092\u7528\u3044\u307e\u3059\uff0e<br \/>\n(\u8a72\u5f53\u30d5\u30a1\u30a4\u30eb: Responder.models.py)<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Session%E3%81%AB%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3%E3%83%A6%E3%83%BC%E3%82%B6%E3%83%BC%E3%82%92%E7%99%BB%E9%8C%B2%E3%81%99%E3%82%8B\"><\/span>Session\u306b\u30ed\u30b0\u30a4\u30f3\u30e6\u30fc\u30b6\u30fc\u3092\u767b\u9332\u3059\u308b<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u30bb\u30c3\u30b7\u30e7\u30f3\u306f\u8f9e\u66f8\u578b\u5909\u6570\u3067\u3059\u306e\u3067\uff0c<\/p>\n<pre class=\"lang:python decode:true \">resp.session['user_id'] = user_id<\/pre>\n<p>\u306e\u3088\u3046\u306a\u5f62\u3067\u826f\u3044\u3067\u3059\uff0e<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Cookie%E3%81%AB%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3%E3%83%A6%E3%83%BC%E3%82%B6%E3%82%92%E7%99%BB%E9%8C%B2%E3%81%99%E3%82%8B\"><\/span>Cookie\u306b\u30ed\u30b0\u30a4\u30f3\u30e6\u30fc\u30b6\u3092\u767b\u9332\u3059\u308b<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Responder\u3067\u306fPython\u306eSimpleCookie\u30af\u30e9\u30b9\u3092\u4f7f\u3063\u3066\u304a\u308a\uff0cResponder\u306eResponse\u30af\u30e9\u30b9\u306b\u306f<span class=\"lang:python decode:true crayon-inline \">set_cookie()<\/span>\u00a0\u3068\u3044\u3046\u95a2\u6570\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u306e\u3067\u305d\u308c\u3092\u4f7f\u3044\u307e\u3059\uff0e<\/p>\n<p>\u5148\u306e\u95a2\u6570\u306b\u306f\u5f15\u6570\u3068\u3057\u3066\uff0c<\/p>\n<pre class=\"lang:python decode:true \">def set_cookie(\r\n        self,\r\n        key,  # \u30ad\u30fc\r\n        value=\"\",  # \u767b\u9332\u3059\u308b\u5024\r\n        expires=None,  # \u3044\u3064\u7834\u68c4\u3055\u308c\u308b\u304b\r\n        max_age=None,  # \u4f55\u79d2\u5f8c\u306b\u7834\u68c4\u3055\u308c\u308b\u304b\r\n    )<\/pre>\n<p>\u306a\u3069\u3092\u53d6\u308a\u307e\u3059\uff0e(\u4e0a\u8a18\u306f\u5f15\u6570\u306e\u4e00\u90e8\u3067\uff0c\u4ed6\u306b\u3082\u3042\u308a\u307e\u3059)<\/p>\n<p>\u30ed\u30b0\u30a4\u30f3\u6642\u306b\u306f\uff0c<\/p>\n<pre class=\"lang:python decode:true \">resp.set_cookie(key='user_id', value=user_id, expires=None, max_age=None)\r\nresp.set_cookie(key='token', value=token, expires=None, max_age=None)<\/pre>\n<p>\u306e\u3088\u3046\u306b\u3057\u307e\u3059\uff0e<\/p>\n<h3><span class=\"ez-toc-section\" id=\"%E3%83%AD%E3%82%B0%E3%82%A2%E3%82%A6%E3%83%88%E5%87%A6%E7%90%86\"><\/span>\u30ed\u30b0\u30a2\u30a6\u30c8\u51e6\u7406<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>\u30ed\u30b0\u30a2\u30a6\u30c8\u306f\u5148\u306eSession\u3068Cookie\u3092\u7834\u68c4\u3059\u308c\u3070\u826f\u3044\u3060\u3051\u3067\u3059\uff0e<\/p>\n<pre class=\"lang:python decode:true\">resp.set_cookie(key='user_id', value='', expires=0, max_age=0)\r\nresp.set_cookie(key='token', value='', expires=0, max_age=0)\r\nresp.session.pop('user_id')<\/pre>\n<h2><span class=\"ez-toc-section\" id=\"404%E3%82%A8%E3%83%A9%E3%83%BC%E3%83%8F%E3%83%B3%E3%83%89%E3%83%AA%E3%83%B3%E3%82%B0\"><\/span>404\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Responder\u306e\u554f\u984c\u306f\u3053\u308c\u3067\u3059\uff0e<\/p>\n<p>\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u304c\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u306f\u3067\u304d\u306a\u3044\u3088\u3046\u3067\u3059\uff0e(v1.3.1)<\/p>\n<p>\u4ed6\u306e\u65b9\u304c\u66f8\u3044\u305f\u8a18\u4e8b\u3082\u53c2\u7167\u3057\u307e\u3057\u305f\u304c\uff0c\u30b9\u30de\u30fc\u30c8\u306b\u89e3\u6c7a\u3059\u308b\u65b9\u6cd5\u306f\u306a\u3055\u305d\u3046\u3067\u3059\uff0e<\/p>\n<blockquote><p>\u53c2\u8003\uff1a<br \/>\n<a href=\"https:\/\/ohshige.hatenablog.com\/entry\/2019\/02\/25\/190000\">Responder\u306b\u306f\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u6a5f\u80fd\u304c\u306a\u3044\uff1f<\/a><br \/>\n<a href=\"https:\/\/note.mu\/jurit\/n\/n0fd736f0089c\">responder\u3067\u7406\u89e3\u3059\u308bWeb\u30b5\u30fc\u30d0\u30fc #2 \u30eb\u30fc\u30c6\u30a3\u30f3\u30b0<\/a><\/p><\/blockquote>\n<p>\u305d\u3053\u3067\u50d5\u304c\u601d\u3044\u3064\u3044\u305f<mark><strong>\u4e00\u756a\u7c21\u5358\u306a\u65b9\u6cd5\u306f\uff0cResponder\u306e\u30bd\u30fc\u30b9\u3092\u3044\u3058\u308b\u65b9\u6cd5<\/strong><\/mark>\u3067\u3059\uff0e<\/p>\n<p>\u30bd\u30fc\u30b9\u3092\u3044\u3058\u308b\u306e\u306f\u30ca\u30f3\u30bb\u30f3\u30b9\u304b\u3082\u3057\u308c\u307e\u305b\u3093\u304c\uff0c\u3053\u308c\u304c\u4e00\u756a\u7c21\u5358\u3067\u3059(\u591a\u5206)\uff0e<\/p>\n<p>\u307e\u305a\u3044\u3058\u308b\u306e\u306f\uff0cResponder\u306e<mark><strong>api.py<\/strong><\/mark>\u306eAPI\u30af\u30e9\u30b9\uff0e<\/p>\n<p>\u305d\u306e494\u884c\u76ee\u3042\u305f\u308a\u306b\u3042\u308b\uff0c<span class=\"lang:python decode:true crayon-inline \">default_response()<\/span>\u00a0\u3067\u3059\uff0e<\/p>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u4fee\u6b63\u3057\u307e\u3059\uff0e<\/p>\n<pre class=\"start-line:494 lang:python mark:15-16 decode:true\">def default_response(\r\n    self, req=None, resp=None, websocket=False, notfound=False, error=False\r\n):\r\n    if websocket:\r\n        return\r\n\r\n    if resp.status_code is None:\r\n        resp.status_code = 200\r\n\r\n    if self.default_endpoint and notfound:\r\n        self.default_endpoint(req=req, resp=resp)\r\n    else:\r\n        if notfound:\r\n            resp.status_code = status_codes.HTTP_404\r\n            # resp.text = \"Not found.\" \u3053\u3053\u3092\u4ee5\u4e0b\u306b\u5909\u66f4\r\n            resp.content = self.template('404.html')\r\n        if error:\r\n            resp.status_code = status_codes.HTTP_500\r\n            resp.text = \"Application error.\"\r\n<\/pre>\n<p>\u3053\u308c\u3067\uff0c\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u306e<mark><strong>404.html<\/strong><\/mark>\u306b\u5168\u3066\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\u3055\u308c\uff0c\u7d20\u3063\u6c17\u306a\u3044\u300cNot Found.\u300d\u3068\u306f\u304a\u3055\u3089\u3070\u3067\u3059\uff0e<\/p>\n<h2><span class=\"ez-toc-section\" id=\"%E3%81%BE%E3%81%A8%E3%82%81\"><\/span>\u307e\u3068\u3081<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Responder\u3092\u5c11\u3057\u89e6\u3063\u3066\u307f\u305f\u611f\u60f3\u3068\u3057\u3066\u306f\uff0c<mark>\u30b7\u30f3\u30d7\u30eb\u3067\u7121\u99c4\u304c\u306a\u304f\u597d\u304d\u3067\u3059\uff0e<\/mark><\/p>\n<p>Responder\u306e\u5f37\u307f\u306f\u975e\u540c\u671f\u51e6\u7406\u306b\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u304c\uff0c\u50d5\u306f\u307e\u3060\u3046\u307e\u3044\u4f7f\u3044\u65b9\u3092\u6a21\u7d22\u4e2d\u3067\u3059\uff0e<\/p>\n<p>Python\u306eWeb\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306f<strong><mark>Django\u304c\u6709\u540d\u3067\u3059\u304c\uff0c\u307e\u3063\u305f\u304f\u7cfb\u7d71\u304c\u7570\u306a\u308a<\/mark><\/strong>\uff0c\u3069\u3061\u3089\u304b\u3068\u3044\u3046\u3068Flask\u306b\u8fd1\u3044\u3067\u3059\uff0e<\/p>\n<p>Django\u306e\u3088\u3046\u306a\u30a2\u30d7\u30ea\u3092Responder\u3067\u3082\u4f5c\u308c\u307e\u3059\u304c\uff0c\u5c11\u3057\u5927\u5909\u3067\u3057\u3087\u3046\uff0e<\/p>\n<p>\u307e\u305f\uff0cResponder\u3092\u5b66\u3079\u3070\uff0c<mark>\u52dd\u624b\u306bSQLAlchemy\u306a\u3069\u306ePython\u306e\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u7ba1\u7406\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3084Jinja2\u306e\u77e5\u8b58\u304c\u540c\u6642\u306b\u3064\u3044\u3066\u304d\u307e\u3059<\/mark>\uff0e<\/p>\n<p><strong>\u306a\u306e\u3067\u30aa\u30b9\u30b9\u30e1\u3067\u3059\uff0e<\/strong><\/p>\n<p>\u30d6\u30e9\u30c3\u30af\u30dc\u30c3\u30af\u30b9\u5316\u3057\u305f\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306f\u4fbf\u5229\u3067\u3059\u304c\uff0c\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u306e\u52c9\u5f37\u306b\u306f\u4e0d\u5411\u304d\u306a\u306e\u3067...<\/p>\n<p>\u3068\uff0c\u307e\u3042\u8272\u3005\u3068\u3064\u3089\u3064\u3089\u66f8\u304d\u307e\u3057\u305f\u304c\uff0c\u307e\u3060Responder\u306f\u767a\u5c55\u9014\u4e0a\u3067\uff0c\u4eca\u5f8c\u3082\u30a2\u30c3\u30d7\u30c7\u30fc\u30c8\u304c\u671f\u5f85\u3067\u304d\u307e\u3059\uff0e<\/p>\n<p>\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0\u306a\u3093\u304b\u306f\uff0c\u3044\u305a\u308c\u3082\u3063\u3068\u6271\u3044\u3084\u3059\u304f\u306a\u308b\u306e\u3067\u306f\u306a\u3044\u3067\u306f\uff0c\u3068\u601d\u3044\u307e\u3059\uff0e<\/p>\n<p>\u307f\u306a\u3055\u3093\u3082\u3053\u308c\u3092\u6a5f\u306bResponder\u89e6\u3063\u3066\u307f\u3066\u304f\u3060\u3055\u3044\uff0e<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30b3\u30f3\u30c6\u30f3\u30c4 Responder\u30ed\u30fc\u30ab\u30eb\u30b5\u30fc\u30d0\u306e\u7acb\u3061\u4e0a\u3052\u3068\u30eb\u30fc\u30c6\u30a4\u30f3\u30b0\u30d5\u30a1\u30a4\u30eb\u3092\u5206\u3051\u3088\u3046run.pyurls.pycontrollers.pyJinja2\u306e\u30d5\u30a3\u30eb\u30bf\u3092\u8ffd\u52a0\u3059\u308bJinja2\u306e\u30b0\u30ed\u30fc\u30d0\u30eb\u5909\u6570\u3092\u8ffd\u52a0\u3059\u308b\u30ed\u30b0\u30a4\u30f3\uff0c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":564,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[50,51],"_links":{"self":[{"href":"https:\/\/hirlab.net\/nblog\/wp-json\/wp\/v2\/posts\/553"}],"collection":[{"href":"https:\/\/hirlab.net\/nblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hirlab.net\/nblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hirlab.net\/nblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hirlab.net\/nblog\/wp-json\/wp\/v2\/comments?post=553"}],"version-history":[{"count":22,"href":"https:\/\/hirlab.net\/nblog\/wp-json\/wp\/v2\/posts\/553\/revisions"}],"predecessor-version":[{"id":653,"href":"https:\/\/hirlab.net\/nblog\/wp-json\/wp\/v2\/posts\/553\/revisions\/653"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/hirlab.net\/nblog\/wp-json\/wp\/v2\/media\/564"}],"wp:attachment":[{"href":"https:\/\/hirlab.net\/nblog\/wp-json\/wp\/v2\/media?parent=553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hirlab.net\/nblog\/wp-json\/wp\/v2\/categories?post=553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hirlab.net\/nblog\/wp-json\/wp\/v2\/tags?post=553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}