{"id":12156,"date":"2017-09-20T10:00:58","date_gmt":"2017-09-20T01:00:58","guid":{"rendered":"http:\/\/sitest.jp\/blog\/?p=12156"},"modified":"2022-04-13T19:53:58","modified_gmt":"2022-04-13T10:53:58","slug":"python-%e3%81%ae%e3%82%a6%e3%82%a7%e3%83%96%e3%83%95%e3%83%ac%e3%83%bc%e3%83%a0%e3%83%af%e3%83%bc%e3%82%af%e3%80%8c-pyramid-%e3%80%8d%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6-api-%e3%82%b5%e3%83%bc","status":"publish","type":"post","link":"https:\/\/sitest.jp\/blog\/?p=12156","title":{"rendered":"Python \u306e\u30a6\u30a7\u30d6\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u300c Pyramid \u300d\u3092\u4f7f\u3063\u3066 API \u30b5\u30fc\u30d0\u30fc\u3092\u4f5c\u3063\u3066\u307f\u3088\u3046\uff01"},"content":{"rendered":"<p>\u307f\u306a\u3055\u3093\u306f Python \u3067\u30a6\u30a7\u30d6\u30b5\u30fc\u30d0\u30fc\u3092\u69cb\u7bc9\u3059\u308b\u6642\u306b\u3001\u3069\u306e\u3088\u3046\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u4f7f\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u304b\uff1f<\/p>\n<p>Rails \u30e9\u30a4\u30af\u306e Django \u3001\u8efd\u91cf\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306e Bottle \u3084 Flask \u306a\u3069\u3001\u5927\u5c0f\u69d8\u3005\u306a\u601d\u60f3\u3067\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u304c\u958b\u767a\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f Pyramid \u3068\u547c\u3070\u308c\u308b\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u4f7f\u7528\u3057\u3066\u3001API \u30b5\u30fc\u30d0\u30fc\u3092\u4f5c\u6210\u3059\u308b\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\uff01<\/p>\n<p><!--more--><\/p>\n<div class=\"frame_headding\">\n<h3 style=\"text-align: left;\">Pyramid \u3068\u306f\uff1f<\/h3>\n<\/div>\n<p><a target=\"_blank\" rel=\"nofollow noopener noreferrer\" href=\"https:\/\/trypyramid.com\/\">Pyramid<\/a> \u306f\u3001\u5c0f\u3055\u3044\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u304b\u3089\u30b9\u30b1\u30fc\u30eb\u3057\u305f\u5927\u898f\u6a21\u958b\u767a\u307e\u3067\u67d4\u8edf\u306a\u8a2d\u8a08\u304c\u3067\u304d\u308b\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3059\u3002<br \/>\nPython \u306e\u30a6\u30a7\u30d6\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306e\u4e2d\u3067\u306f\u3001\u3044\u3061\u65e9\u304f Python3 \u306b\u5bfe\u5fdc\u3057\u305f\u3053\u3068\u3067\u3082\u77e5\u3089\u308c\u3066\u3044\u307e\u3059\u3002<br \/>\n\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u601d\u60f3\u3067\u8a2d\u8a08\u3055\u308c\u3066\u304a\u308a\u3001\u69d8\u3005\u306a\u30e2\u30b8\u30e5\u30fc\u30eb\uff08 <a target=\"_blank\" rel=\"nofollow noopener noreferrer\" href=\"https:\/\/github.com\/uralbash\/awesome-pyramid\">Awesome Pyramid<\/a> \uff09\u304c\u958b\u767a\u3001\u63d0\u4f9b\u3055\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>Flask \u306e\u3088\u3046\u306b1\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u66f8\u3051\u308b\u624b\u8efd\u3055\u3068\u3001\u30e2\u30b8\u30e5\u30fc\u30eb\u3092\u7d44\u307f\u5408\u308f\u305b\u308b\u3053\u3068\u306b\u3088\u3063\u3066\u5927\u898f\u6a21\u958b\u767a\u306b\u3082\u5bfe\u5fdc\u3067\u304d\u308b\u8a2d\u8a08\u306f\u3068\u3066\u3082\u7d20\u6674\u3089\u3057\u3044\u3082\u306e\u3060\u3068\u611f\u3058\u3066\u3044\u307e\u3059\u3002<\/p>\n<div class=\"frame_headding\">\n<h3 style=\"text-align: left;\">Pyramid \u3067 API \u30b5\u30fc\u30d0\u30fc\u3092\u4f5c\u308b<\/h3>\n<\/div>\n<h4 style=\"border-bottom: solid 4px #73bc9b; margin-bottom: 30px;\">\u74b0\u5883\u69cb\u7bc9<\/h4>\n<p>\u307e\u305a\u306f\u74b0\u5883\u69cb\u7bc9\u3092\u3057\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<br \/>\n\u4f7f\u7528\u3059\u308b\u74b0\u5883\u3068\u3057\u3066\u306f\u4ee5\u4e0b\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<ul>\n<li>Python 2.7<\/li>\n<li>Pyramid 1.9.1<\/li>\n<\/ul>\n<pre><code>pip install pyramid<\/code><\/pre>\n<p><code>pip<\/code> \u3067 Pyramid \u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3068\u3001\u5fc5\u8981\u306a\u30e9\u30a4\u30d6\u30e9\u30ea\u304c\u4e00\u7dd2\u306b\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3055\u308c\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f\u7c21\u5358\u306a API \u30b5\u30fc\u30d0\u30fc\u3092\u4f5c\u3063\u3066\u3044\u304d\u307e\u3059\u306e\u3067\u3001\u4ed6\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u306f\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u307e\u305b\u3093\u3002<\/p>\n<h4 style=\"border-bottom: solid 4px #73bc9b; margin-bottom: 30px;\">Hello World\uff01<\/h4>\n<p>\u307e\u305a\u306f\u3001\u3088\u304f\u3042\u308b\u304a\u7d04\u675f\u304b\u3089\u3067\u3059\u3002<br \/>\nPyramid \u3092\u4f7f\u7528\u3057\u3066\u300cHello World\u300d\u3092\u51fa\u529b\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u307f\u307e\u3059\u3002<\/p>\n<pre><code class=\"python\"># main.py\r\n# coding: utf-8\r\nfrom wsgiref.simple_server import make_server\r\nfrom pyramid.config import Configurator\r\nfrom pyramid.view import view_config\r\n\r\n\r\n@view_config(route_name='hello', renderer='string')\r\ndef hello_world(request):\r\n    return 'Hello World!'\r\n\r\nif __name__ == '__main__':\r\n    config = Configurator()\r\n    config.add_route('hello', '\/hello')\r\n    config.scan()\r\n    app = config.make_wsgi_app()\r\n    server = make_server('127.0.0.1', 8080, app)\r\n    server.serve_forever()\r\n<\/code><\/pre>\n<p>\u4e0a\u8a18\u3092 <code>main.py<\/code> \u306b\u8a18\u8ff0\u3057\u3001 <code>python main.py<\/code> \u3068\u5b9f\u884c\u3057\u3066\u3001<br \/>\nhttp:\/\/127.0.0.1:8080\/hello \u306b\u30a2\u30af\u30bb\u30b9\u3057\u3066\u307f\u3066\u4e0b\u3055\u3044\u3002<br \/>\n\u7121\u4e8b\u3001Hello World! \u304c\u51fa\u529b\u3055\u308c\u305f\u3067\u3057\u3087\u3046\u304b\uff1f<\/p>\n<p><a href=\"https:\/\/storage.googleapis.com\/sitest-wp\/blog\/wp-content\/uploads\/2017\/07\/127_0_0_1_8080_hello.png\"><img loading=\"lazy\" src=\"https:\/\/storage.googleapis.com\/sitest-wp\/blog\/wp-content\/uploads\/2017\/07\/127_0_0_1_8080_hello.png\" alt=\"\" width=\"480\" height=\"228\" class=\"alignnone size-full wp-image-12162\" \/><\/a><\/p>\n<p>\u4eca\u56de\u306f API \u30b5\u30fc\u30d0\u30fc\u69cb\u7bc9\u306e\u305f\u3081\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u30a8\u30f3\u30b8\u30f3\u306b\u306f\u89e6\u308c\u307e\u305b\u3093\u304c\u3001\u3088\u304f Python \u3067\u4f7f\u7528\u3055\u308c\u308b\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u8a00\u8a9e\u3068\u3057\u3066 Flask \u3067\u6a19\u6e96\u7684\u306b\u4f7f\u308f\u308c\u3066\u3044\u308b <a target=\"_blank\" rel=\"nofollow noopener noreferrer\" href=\"http:\/\/jinja.pocoo.org\/docs\/2.9\/\">jinja2<\/a> \u304c\u3042\u308a\u307e\u3059\u3002<br \/>\n<a target=\"_blank\" rel=\"nofollow noopener noreferrer\" href=\"https:\/\/docs.pylonsproject.org\/projects\/pyramid_jinja2\/en\/latest\/\">pyramid_jinja2<\/a> \u3092\u4f7f\u3046\u3053\u3068\u3067 Pyramid \u3067\u3082\u4f7f\u7528\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<h4 style=\"border-bottom: solid 4px #73bc9b; margin-bottom: 30px;\">\u30ec\u30b9\u30dd\u30f3\u30b9\u3092 JSON \u306b\u3057\u3066\u307f\u3088\u3046\uff01<\/h4>\n<p>\u6b21\u306b Pyramid \u304b\u3089\u306e\u30ec\u30b9\u30dd\u30f3\u30b9\u3092 JSON \u306b\u3057\u3066\u3001API \u30b5\u30fc\u30d0\u30fc\u3068\u3057\u3066\u52d5\u4f5c\u3059\u308b\u3088\u3046\u306b\u5909\u66f4\u3057\u3066\u3044\u304d\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code class=\"python\">@view_config(route_name='hello', renderer='json')\r\ndef hello_world(request):\r\n    return {'message': 'Hello World!'}\r\n<\/code><\/pre>\n<p><code>renderer<\/code> \u3092 <code>string<\/code> \u304b\u3089 <code>json<\/code> \u306b\u5909\u66f4\u3057\u3001<code>return<\/code> \u3059\u308b\u4e2d\u8eab\u3092 <code>dict<\/code> \u5f62\u5f0f\u306b\u5909\u66f4\u3057\u307e\u3059\u3002<br \/>\n\u518d\u5ea6\u30b5\u30fc\u30d0\u30fc\u3092\u7acb\u3061\u4e0a\u3052\u5148\u307b\u3069\u306e URL \u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u3001<code>{\"message\": \"Hello World!\"}<\/code> \u304c\u8868\u793a\u3055\u308c\u3066\u3044\u308b\u3068\u601d\u3044\u307e\u3059\u3002<br \/>\n\u30ec\u30b9\u30dd\u30f3\u30b9\u30d8\u30c3\u30c0\u30fc\u3082\u81ea\u52d5\u7684\u306b <code>application\/json<\/code> \u3092\u30bb\u30c3\u30c8\u3057\u3066\u304f\u308c\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u3053\u308c\u3060\u3051\u3067 JSON \u3092\u30ec\u30b9\u30dd\u30f3\u30b9\u306b\u3059\u308b API \u30b5\u30fc\u30d0\u30fc\u3092\u7acb\u3066\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3057\u305f\u3002<\/p>\n<p>\u305d\u308c\u3067\u306f\u3001\u4ee5\u4e0b\u306e\u30c7\u30fc\u30bf\u3092 JSON \u3067\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u304d\u307e\u3059\u3002<\/p>\n<pre><code class=\"json\">{\r\n    'john': {\r\n        'name': 'John Doe',\r\n        'age': 25,\r\n        'gender': 'male'\r\n    },\r\n    'jane': {\r\n        'name': 'Jane Smith',\r\n        'age': 28,\r\n        'gender': 'female'\r\n    }\r\n}<\/code><\/pre>\n<p>\u30eb\u30fc\u30c6\u30a3\u30f3\u30b0\u306f\u3001\u30c7\u30fc\u30bf\u5168\u4f53\u3092\u53d6\u5f97\u3059\u308b <code>\/persons<\/code> \u3068\u3001\u500b\u5225\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b <code>\/person\/{name}<\/code> \u306e2\u7a2e\u985e\u3092\u4f5c\u6210\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre><code class=\"python\">\r\n@view_config(route_name='person', renderer='json')\r\ndef show_person(request):\r\n    name = request.matchdict['name']\r\n    return Persons[name]\r\n\r\n@view_config(route_name='persons', renderer='json')\r\ndef get_persons(request):\r\n    return Persons\r\n\r\nif __name__ == '__main__':\r\n    config = Configurator()\r\n    config.add_route('person', '\/person\/{name}')\r\n    config.add_route('persons', '\/persons')\r\n    config.scan()\r\n    app = config.make_wsgi_app()\r\n    server = make_server('127.0.0.1', 8080, app)\r\n    server.serve_forever()\r\n<\/code><\/pre>\n<p><code>main.py<\/code> \u3092\u4e0a\u8a18\u306e\u3088\u3046\u306b\u4fee\u6b63\u3057\u3001\u30b5\u30fc\u30d0\u30fc\u3092\u518d\u8d77\u52d5\u3057\u3066\u307f\u307e\u3057\u3087\u3046\u3002<br \/>\nhttp:\/\/127.0.0.1:8080\/persons \u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068\u5168\u4f53\u306e\u30c7\u30fc\u30bf\u3001<br \/>\nhttp:\/\/127.0.0.1:8080\/person\/john \u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3068 John \u306e\u30c7\u30fc\u30bf\u3092\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<div class=\"frame_headding\">\n<h3 style=\"text-align: left;\">\u307e\u3068\u3081<\/h3>\n<\/div>\n<p>\u4eca\u56de\u306f Pyramid \u3092\u4f7f\u3063\u305f API \u30b5\u30fc\u30d0\u30fc\u69cb\u7bc9\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>Bottle \u3084 Flask \u3060\u3068\u3061\u3087\u3063\u3068\u7269\u8db3\u308a\u306a\u3044\u3001\u3060\u3051\u3069 Django \u306e\u3088\u3046\u306a\u30d5\u30eb\u30b9\u30bf\u30c3\u30af\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306f\u91cd\u3044\u3068\u3044\u3046\u65b9\u306b\u306f\u3001\u3068\u3066\u3082\u304a\u3059\u3059\u3081\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3067\u3059\u3002<\/p>\n<p>\u4eca\u56de\u3054\u7d39\u4ecb\u3057\u305f\u3088\u3046\u306b1\u30d5\u30a1\u30a4\u30eb\u304b\u3089\u3067\u3082\u59cb\u3081\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u306e\u3067\u3001\u305c\u3072\u7686\u3055\u3093\u3082\u6311\u6226\u3057\u3066\u307f\u3066\u306f\u3069\u3046\u3067\u3057\u3087\u3046\u304b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u307f\u306a\u3055\u3093\u306f Python \u3067\u30a6\u30a7\u30d6\u30b5\u30fc\u30d0\u30fc\u3092\u69cb\u7bc9\u3059\u308b\u6642\u306b\u3001\u3069\u306e\u3088\u3046\u306a\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u4f7f\u7528\u3055\u308c\u3066\u3044\u307e\u3059\u304b\uff1f Rails \u30e9\u30a4\u30af\u306e Django \u3001\u8efd\u91cf\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306e Bottle \u3084 Flask \u306a\u3069\u3001\u5927\u5c0f\u69d8\u3005\u306a\u601d\u60f3\u3067\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u304c\u958b\u767a\u3055\u308c\u3066\u3044\u307e\u3059\u3002 \u4eca\u56de\u306f Pyramid \u3068\u547c\u3070\u308c\u308b\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u3092\u4f7f\u7528\u3057\u3066\u3001API \u30b5\u30fc\u30d0\u30fc\u3092\u4f5c\u6210\u3059\u308b\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\uff01<\/p>\n","protected":false},"author":1,"featured_media":12158,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[27],"tags":[],"acf":{"keywords":"python,\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af,pyramid,api,\u30b5\u30fc\u30d0\u30fc,\u69cb\u7bc9","description":"Python \u3068\u6700\u8fd1\u4eba\u6c17\u306e\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af Pyramid \u3092\u4f7f\u7528\u3057\u3066\u7c21\u5358\u306b API \u30b5\u30fc\u30d0\u30fc\u3092\u69cb\u7bc9\u3059\u308b\u65b9\u6cd5\u3092\u3054\u7d39\u4ecb\u3057\u307e\u3059\uff01","ogp_url":"","banner":""},"_links":{"self":[{"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/posts\/12156"}],"collection":[{"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/comments?post=12156"}],"version-history":[{"count":14,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/posts\/12156\/revisions"}],"predecessor-version":[{"id":25224,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/posts\/12156\/revisions\/25224"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/media\/12158"}],"wp:attachment":[{"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/media?parent=12156"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/categories?post=12156"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sitest.jp\/blog\/wp-json\/wp\/v2\/tags?post=12156"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}