APIサーバー Falcon post,headerの値の取得方法
APIサーバーのFalconをインストールし、getパラメーターの取得を行ってみました。Python + Falconで高速APIサーバーを作成する
その他のパラメーターの取得方法を調べてみます。
このドキュメントを参考にしました。
https://github.com/falconry/falcon/blob/master/README.rst
POST
POSTパラメーターの取得は以下のようになりました。
json形式のデータをpostし、解析しています。
- # -*- coding:utf-8 -*-
- import json
- import falcon
- class HelloResource(object):
- # postされた時の動作
- def on_post(self, req, res):
- # postパラメーターを取得
- body = req.stream.read()
- data = json.loads(body)
- # パラメーターの取得
- name = data['name']
- msg = {
- "message": "Hello, " + name
- }
- res.body = json.dumps(msg)
- app = falcon.API()
- app.add_route("/", HelloResource())
- if __name__ == "__main__":
- from wsgiref import simple_server
- httpd = simple_server.make_server("0.0.0.0", 8000, app)
- httpd.serve_forever()
テストプログラム
- # -*- coding:utf-8 -*-
- import json
- import urllib2
- values = {'name' : 'symfo'}
- data = json.dumps(values)
- response = urllib2.urlopen('http://192.168.1.102:8000', data)
- body = response.read()
- response.close()
- data = json.loads(body)
- print(data['message'])
実行結果
$ python api_test.py
Hello, symfo
headerの取得
get_headerでヘッダーの情報が取得できます。
- # -*- coding:utf-8 -*-
- import json
- import falcon
- class HelloResource(object):
- # getされた時の動作
- def on_get(self, req, res):
- # headerを取得
- name = req.get_header('name')
- msg = {
- "message": "Hello, " + name
- }
- res.body = json.dumps(msg)
- app = falcon.API()
- app.add_route("/", HelloResource())
- if __name__ == "__main__":
- from wsgiref import simple_server
- httpd = simple_server.make_server("0.0.0.0", 8000, app)
- httpd.serve_forever()
テストプログラム
- # -*- coding:utf-8 -*-
- import json
- import urllib2
- headers = { 'name' : 'symfo' }
- req = urllib2.Request(url='http://192.168.1.102:8000', headers=headers)
- response = urllib2.urlopen(req)
- body = response.read()
- response.close()
- data = json.loads(body)
- print(data['message'])
画像ファイルのPOST
POSTのヘッダーにファイル名。
ボディに画像ファイルを設定。
サーバー側で保存してみます。
- # -*- coding:utf-8 -*-
- import json
- import falcon
- class HelloResource(object):
- # postされた時の動作
- def on_post(self, req, res):
- # headerからファイル名を取得
- filename = req.get_header('File-Name')
- # bodyから画像ファイルのバイナリ取得
- body = req.stream.read()
- # ファイルを保存
- with open(filename, 'wb') as f:
- f.write(body)
- res.body = json.dumps({'result':'ok'})
- app = falcon.API()
- app.add_route("/", HelloResource())
- if __name__ == "__main__":
- from wsgiref import simple_server
- httpd = simple_server.make_server("0.0.0.0", 8000, app)
- httpd.serve_forever()
テスト用のプログラム
- # -*- coding:utf-8 -*-
- import json
- import urllib2
- # ファイル名
- headers = { 'File-Name' : 'sample.jpg' }
- # ファイル本体を取得
- with open('cc2.jpg', 'rb') as f:
- image = f.read()
- req = urllib2.Request(url='http://192.168.1.102:8000', data=image, headers=headers)
- response = urllib2.urlopen(req)
- # 戻り値を解析
- body = response.read()
- response.close()
- data = json.loads(body)
- print(data['result'])
だいぶ使い方がわかってきました。
on_get,on_postに渡されるリクエスト
渡されるリクエストオブジェクトには、こんなメソッドがあるようです。
メモとして残しておきます。
accept
access_route
app
auth
client_accepts
client_accepts_json
client_accepts_msgpack
client_accepts_xml
client_prefers
content_length
content_type
context
context_type
cookies
date
env
expect
get_header
get_header_as_datetime
get_param
get_param_as_bool
get_param_as_date
get_param_as_int
get_param_as_list
headers
host
if_match
if_modified_since
if_none_match
if_range
if_unmodified_since
log_error
method
options
params
path
protocol
query_string
range
range_unit
relative_uri
remote_addr
stream
subdomain
uri
url
user_agent
続きです。
Falcon URLによる分岐と値の取得
コメント