{"id":59,"date":"2014-07-09T17:22:30","date_gmt":"2014-07-09T08:22:30","guid":{"rendered":"http:\/\/appw.jp\/2014\/?p=59"},"modified":"2016-10-28T14:16:40","modified_gmt":"2016-10-28T05:16:40","slug":"python-websocket-%e3%81%ae%e3%83%81%e3%83%a3%e3%83%83%e3%83%88%e3%82%b5%e3%83%b3%e3%83%97%e3%83%ab%e3%81%ab%e3%80%81mongodb-%e4%bf%9d%e5%ad%98%e3%82%92%e4%bb%98%e3%81%91%e5%8a%a0%e3%81%88%e3%81%be","status":"publish","type":"post","link":"https:\/\/appw.jp\/2014\/?p=59","title":{"rendered":"Python WebSocket \u306e\u30c1\u30e3\u30c3\u30c8\u30b5\u30f3\u30d7\u30eb\u306b\u3001MongoDB \u4fdd\u5b58\u3092\u4ed8\u3051\u52a0\u3048\u307e\u3057\u305f"},"content":{"rendered":"<p>Python \u3067 WebSocket \u3092\u5229\u7528\u3057\u305f\u30b5\u30f3\u30d7\u30eb\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3001\u691c\u7d22\u3057\u3066\u307f\u308b\u3068\u3001\u7c21\u5358\u306a\u30c1\u30e3\u30c3\u30c8\u30d7\u30ed\u30b0\u30e9\u30e0\u3068\u3057\u3066\u3068\u3066\u3082\u591a\u304f\u898b\u3064\u304b\u308a\u307e\u3059\u3002<\/p>\n<p>\u4eca\u56de\u306f\u3001\u305d\u306e\u3088\u3046\u306a\u30c1\u30e3\u30c3\u30c8\u306e\u30b5\u30f3\u30d7\u30eb\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u53c2\u7167\u3057\u306a\u304c\u3089\u3001\u305d\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\u3092 MongoDB \u306b\u4fdd\u5b58\u3059\u308b\u3088\u3046\u306b\u66f8\u304d\u52a0\u3048\u3066\u307f\u307e\u3059\u3002\u3055\u3089\u306b\u3001\u521d\u671f\u8868\u793a\u306b\u6700\u65b0\uff15\u4ef6\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u8868\u793a\u3059\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<p>\u30b5\u30f3\u30d7\u30eb\u30d7\u30ed\u30b0\u30e9\u30e0\u3067\u306f\u3001\u30d5\u30ec\u30fc\u30e0\u30ef\u30fc\u30af\u306b <a href=\"http:\/\/flask.pocoo.org\/\" target=\"_blank\">Flask<\/a> \u3092\u5229\u7528\u3057\u305f\u3082\u306e\u304c\u3088\u304f\u307f\u3089\u308c\u305f\u306e\u3067\u3001\u305d\u308c\u306b\u306a\u3089\u3063\u3066 Flask \u3092\u5229\u7528\u3059\u308b\u3053\u3068\u306b\u3057\u3001\u307e\u305f\u3001WSGI Server \u306b <a href=\"http:\/\/flask.pocoo.org\/\" target=\"_blank\">gevent<\/a> \u3092\u5229\u7528\u3057\u307e\u3059\u3002 <\/p>\n<p>\u6b21\u306e\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3001ws-chat.py \u3067\u30bb\u30fc\u30d6\u3057\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport os,sys\r\nimport gevent\r\nfrom gevent import monkey; monkey.patch_all()\r\nfrom geventwebsocket.handler import WebSocketHandler\r\nfrom gevent.pywsgi import WSGIServer\r\nfrom flask import Flask, render_template, request\r\nimport time\r\nfrom datetime import datetime\r\n\r\nsys.path.append(os.path.dirname(os.path.abspath(__file__)) + '\/..\/model')\r\n\r\nfrom mchat import (ChatLog, connecter)\r\n\r\n\r\napp = Flask(__name__)\r\n\r\n\r\nparticipants = set()\r\n\r\n@app.route('\/chat')\r\ndef chat():\r\n\r\n    ws = request.environ.get('wsgi.websocket')\r\n    participants.add(ws)\r\n    try:\r\n        wlist = &#x5B;]\r\n        posts = ChatLog.objects.order_by(&quot;-id&quot; , &quot;-datetime&quot;)&#x5B;:5]\r\n        for post in posts:\r\n            wlist.append(post.text)\r\n        for value in reversed(wlist):\r\n            ws.send(value)\r\n        while True:\r\n            m = ws.receive()\r\n            print &quot;recv message %s&quot; % m\r\n            if m is None:\r\n                break\r\n            for p in participants:\r\n                print &quot;send message %s&quot; % m\r\n                p.send(m)\r\n            now = int(time.mktime(datetime.now().timetuple()))\r\n            ChatLog(text=m, user='WS', created_at=now).save()\r\n    finally:\r\n        participants.remove(ws)\r\n\r\n    return &quot;&quot;\r\n\r\n@app.route('\/')\r\ndef index():\r\n    return render_template('ws-chat.html')\r\n        \r\nif __name__ == &quot;__main__&quot;:\r\n    connecter()\r\n    server = WSGIServer(('0.0.0.0',8080), app, handler_class=WebSocketHandler)\r\n    server.serve_forever()\r\n<\/pre>\n<p>\u305d\u3057\u3066\u3001\u30c6\u30f3\u30d7\u30ec\u30fc\u30c8\u3092\u5229\u7528\u3057\u307e\u3059\u3002<\/p>\n<p>ws-chat.py \u3092\u30bb\u30fc\u30d6\u3057\u305f\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306e\u76f4\u4e0b\u306b templates \u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u4f5c\u6210\u3057\u3066 ws-chat.html \u3067\u30bb\u30fc\u30d6\u3057\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=utf-8&quot;&gt;\r\n&lt;script src=&quot;http:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/1.11.1\/jquery.min.js&quot;&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\nwindow.onload = function() {\r\n  var data = {};\r\n  var s = new WebSocket(&quot;ws:\/\/127.0.0.1:8080\/chat&quot;);\r\n  s.onopen = function() {\r\n    s.send('New participant joined');\r\n  };\r\n  s.onmessage = function(e) {\r\n    $(&quot;#out&quot;).prepend(&quot;&lt;p&gt;&quot; + e.data + &quot;&lt;\/p&gt;&quot;);\r\n  };\r\n  $('#in').keyup(function(e){\r\n    if (e.keyCode == 13) {\r\n      var line = $(this).val();\r\n      $(this).val('');\r\n      s.send(line);\r\n      return false;\r\n    }\r\n  });\r\n};\r\n&lt;\/script&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&lt;h3&gt;Chat&lt;\/h3&gt;\r\n&lt;p&gt;Message: &lt;input id=&quot;in&quot; \/&gt;&lt;\/p&gt;\r\n&lt;div id=&quot;out&quot;&gt;\r\n&lt;\/div&gt;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/pre>\n<p>MongoDB \u30a2\u30af\u30bb\u30b9\u306f\u5225\u306e\u30e2\u30b8\u30e5\u30fc\u30eb\u3067\u300c<a href=\"http:\/\/appw.jp\/2014\/?p=47\">Mosquitto \u306e payload \u3092 MongoDB \u306b\u4fdd\u5b58\u3057\u3066\u307f\u308b<\/a>\u300d\u3067\u4f5c\u6210\u3057\u307e\u3057\u305f\u3002<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom mongoengine import *\r\n\r\nclass ChatLog(Document):\r\n    text = StringField(required=True)\r\n    user = StringField(required=True)\r\n    created_at = LongField(required=True)\r\n\r\ndef connecter():\r\n    con = connect('chattest')\r\n    print con\r\n<\/pre>\n<p>\u5b9f\u884c\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython ws-chat.py\r\n<\/pre>\n<p>\u30b5\u30fc\u30d0\u30fc\u74b0\u5883\u306f\u3001<a href=\"http:\/\/px.a8.net\/svt\/ejp?a8mat=25TTDR+2BY85U+50+4ATZKY\" target=\"_blank\">ConoHa VPS<\/a><img loading=\"lazy\" decoding=\"async\" border=\"0\" width=\"1\" height=\"1\" src=\"http:\/\/www19.a8.net\/0.gif?a8mat=25TTDR+2BY85U+50+4ATZKY\" alt=\"\"> \u3067\u3001CentOS 6.5 \u3067\u3059\u3002<\/p>\n<p>\u3053\u3053\u3078\u3001Android \u304b\u3089\u63a5\u7d9a\u3057\u3066\u3059\u3079\u3066\u3092\u884c\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>Android \u306e VNC \u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u30a2\u30d7\u30ea bVNC Pro \u304b\u3089\u3064\u306a\u304c\u308b\u3088\u3046\u306b\u3001Desktop \u3068 VNC \u74b0\u5883\u3092\u4f5c\u6210\u3057\u3066\u3044\u307e\u3059\u306e\u3067\u3001\u3053\u308c\u3092\u5229\u7528\u3057\u3066\u78ba\u8a8d\u306a\u3069\u3092\u304a\u3053\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><a href=\"http:\/\/appw.jp\/2014\/files\/2014\/07\/wpid-wp-14048941228491.png\"><img decoding=\"async\" title=\"wp-1404894122849\" class=\"alignnone size-full\" alt=\"image\" src=\"http:\/\/appw.jp\/2014\/files\/2014\/07\/wpid-wp-1404894122849.png\" \/><\/a><br \/>\n<svg width=\"320\" height=\"320\"><image width=\"320\" height=\"320\" xlink:href=\"\/\/appw.jp\/images\/ChatSample.svg\" src=\"http:\/\/appw.jp\/2014\/files\/2014\/07\/sketch-140467369307801.jpg\" \/><\/svg><\/p>\n<p>\u7b2c\uff11\u56de\u306e\u300c<a href=\"http:\/\/appw.jp\/2014\/?p=47\">Mosquitto \u306e payload \u3092 MongoDB \u306b\u4fdd\u5b58\u3057\u3066\u307f\u308b<\/a>\u300d\u3067\u306f\u3001Mosquitto \u306e payload \u3092 MongoDB \u306b\u4fdd\u5b58\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u7b2c\uff12\u56de\u306e\u300c<a href=\"http:\/\/appw.jp\/2014\/?p=54\">MongoDB \u306b\u4fdd\u5b58\u3057\u305f Mosquitto \u306e payload \u3092 Server-Sent Events \u3067\u8868\u793a\u3057\u3066\u307f\u308b<\/a>\u300d\u3067\u306f\u3001HTML5 \u306e Server-Sent Events \u3068 PHP \u3067\u30d6\u30e9\u30a6\u30b6\u306b\u8868\u793a\u3057\u307e\u3057\u305f\u3002<\/p>\n<p>\u4eca\u56de\u306e\u7b2c\uff13\u56de\u306f\u3001Python \u3067 WebSocket \u3092\u5229\u7528\u3057\u305f\u7c21\u5358\u306a\u30c1\u30e3\u30c3\u30c8\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u53c2\u8003\u306b MongoDB \u30a2\u30af\u30bb\u30b9\u3092\u66f8\u304d\u52a0\u3048\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u7b2c\uff14\u56de\u306f\u3001\u7b2c\uff13\u56de\u306e Python \u306e WebSocket \u30c1\u30e3\u30c3\u30c8\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u53c2\u8003\u306b\u3057\u3064\u3064\u3001\u30d6\u30e9\u30a6\u30b6\u304b\u3089 POST \u30e1\u30bd\u30c3\u30c9\u3067 Mosquitto \u3078 Publish \u3057\u3001\u307e\u305f\u3001Server-Sent Events \u3067\u30d6\u30e9\u30a6\u30b6\u306b\u8868\u793a\u3059\u308b\u65b9\u5f0f\u3092 Python \u3067\u8a66\u3057\u3066\u307f\u307e\u3059\u3002<br \/>\n\u300c<a href=\"http:\/\/appw.jp\/2014\/?p=68\">Mosquitto Publish \u3068 Server-Sent Events \u3067 Python \u30c1\u30e3\u30c3\u30c8\u30b5\u30f3\u30d7\u30eb\u3092\u6539\u9020<\/a>\u300d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python \u3067 WebSocket \u3092\u5229\u7528\u3057\u305f\u30b5\u30f3\u30d7\u30eb\u30d7\u30ed\u30b0\u30e9\u30e0\u306f\u3001\u691c\u7d22\u3057\u3066\u307f\u308b\u3068\u3001\u7c21\u5358\u306a\u30c1\u30e3\u30c3\u30c8\u30d7\u30ed\u30b0\u30e9\u30e0\u3068\u3057\u3066\u3068\u3066\u3082\u591a\u304f\u898b\u3064\u304b\u308a\u307e\u3059\u3002 \u4eca\u56de\u306f\u3001\u305d\u306e\u3088\u3046\u306a\u30c1\u30e3\u30c3\u30c8\u306e\u30b5\u30f3\u30d7\u30eb\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u53c2\u7167\u3057\u306a\u304c\u3089\u3001\u305d\u306e\u30e1\u30c3\u30bb\u30fc\u30b8\u3092 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-59","post","type-post","status-publish","format-standard","hentry","category-vps"],"_links":{"self":[{"href":"https:\/\/appw.jp\/2014\/index.php?rest_route=\/wp\/v2\/posts\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/appw.jp\/2014\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/appw.jp\/2014\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/appw.jp\/2014\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/appw.jp\/2014\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=59"}],"version-history":[{"count":9,"href":"https:\/\/appw.jp\/2014\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":163,"href":"https:\/\/appw.jp\/2014\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions\/163"}],"wp:attachment":[{"href":"https:\/\/appw.jp\/2014\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/appw.jp\/2014\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/appw.jp\/2014\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}