• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

PythonからElixir Reportのレポートサーバーにアクセスするサンプルコード


Commit MetaInfo

Revisionc1f47a3d2115f927f8fe55f2e424f95b9111e114 (tree)
Time2014-12-03 01:19:15
AuthorHiromichi Matsushima <hylom@Hiro...>
CommiterHiromichi Matsushima

Log Message

add handler to get xml data from report server

Change Summary

Incremental Difference

--- /dev/null
+++ b/config.py
@@ -0,0 +1,14 @@
1+#!/usr/bin/python
2+# -*- coding: utf-8 -*-
3+
4+config = {
5+ "report_server": {
6+ "user": "admin",
7+ "password": "sa",
8+ "host": "127.0.0.1",
9+ "port": 7001
10+ },
11+ "data_source": {
12+ "traffic": "/WebAnalytics/"
13+ }
14+}
--- a/server_app.py
+++ b/server_app.py
@@ -1,20 +1,63 @@
11 #!/usr/bin/python
22 # -*- coding: utf-8 -*-
33
4+import os
5+from datetime import date, timedelta
6+
47 from tornado import ioloop
58 from tornado import web
6-import os
9+
10+from config import config
11+from report_server import ReportServer
712
813 # configurations
914 CSS_PATH = os.path.join(os.getcwd(), "www_root/css")
1015 JS_PATH = os.path.join(os.getcwd(), "www_root/js")
1116
1217 class MainHandler(web.RequestHandler):
18+ "index.htmlを返すハンドラ"
1319 def get(self):
1420 self.render("index.html")
1521
1622
23+class TrafficDataHandler(web.RequestHandler):
24+ "トラフィックデータリクエストを処理するハンドラ"
25+ def get(self):
26+ try:
27+ year = int(self.get_argument("y"))
28+ month = int(self.get_argument("m"))
29+ except ValueError:
30+ self.send_err(400)
31+ return
32+
33+ # パラメータで指定された年/月から
34+ # 開始/終了年月日を生成する
35+ start_date = date(year, month, 1)
36+ if month == 12:
37+ end_date = date(year + 1, 1, 1) + timedelta(-1)
38+ else:
39+ end_date = date(year, month + 1, 1) + timedelta(-1)
40+
41+ # レポートサーバーに投げるパラメータを作成
42+ param = {
43+ "start_date": start_date,
44+ "end_date": end_date
45+ }
46+
47+ # リクエスト送信
48+ r = ReportServer(config["report_server"]["user"],
49+ config["report_server"]["password"],
50+ config["report_server"]["host"],
51+ config["report_server"]["port"])
52+ xml = r.data(config["data_source"]["traffic"], param)
53+ # 取得したXMLをクライアントに送信
54+ self.set_header("Content-type", "application/xml")
55+ self.set_status(200)
56+ self.finish(xml)
57+
58+
1759 class DataTestHandler(web.RequestHandler):
60+ "テスト用ハンドラ"
1861 def get(self, request_path):
1962 data_dir = os.path.join(os.getcwd(), "testdata")
2063 if request_path == "sample.xml":
@@ -45,8 +88,10 @@ class DataTestHandler(web.RequestHandler):
4588 else:
4689 self.send_error(404)
4790
91+
4892 application = web.Application([
4993 ("/", MainHandler),
94+# ("/data/traffic", TrafficDataHandler),
5095 ("/data/(.*)", DataTestHandler),
5196 ("/css/(.*)", web.StaticFileHandler, {"path": CSS_PATH}),
5297 ("/js/(.*)", web.StaticFileHandler, {"path": JS_PATH}),