I think you often use a web application framework to create an API server, but you don't need something so versatile, isn't it? This Falcon can be used in such a case. Falcon The minimum requirement is one file. Feel free to get started.
Use pip
. Easy.
If it doesn't work, try adding sudo
.
$ pip install falcon
All you need is one file. Let's say sample.py
.
A resource is a class that handles requests. If you write ʻon_get or ʻon_post
, it will handle the request of the corresponding method.
sample.py
...
class SampleResource:
def on_get(self, req, resp):
obj = {
"header": "Hello!",
"body": {
"a": "hoge",
"b": "fuga",
}
}
resp.body = json.dumps(obj, ensure_ascii=False)
...
ʻAdd_route` connects the path with the class that handles the request.
sample.py
...
app = falcon.API()
app.add_route('/sample', SampleResource())
...
Please change the address and port as appropriate.
This part is not necessary when using gunicorn
etc.
sample.py
...
if __name__ == "__main__":
from wsgiref import simple_server
httpd = simple_server.make_server("127.0.0.1", 8000, app)
httpd.serve_forever()
...
It works normally with python
.
$ python sample.py
When I try to access it, it seems to work. http://localhost:8000/sample
{"body": {"a": "hoge", "b": "fuga"}, "header": "Hello!"}
gunicorn
Install with pip
.
$ pip install gunicorn
It seems to use 8000 ports by default.
$ gunicorn sample:app
[2016-12-15 20:37:20 +0900] [34136] [INFO] Starting gunicorn 19.6.0
[2016-12-15 20:37:20 +0900] [34136] [INFO] Listening at: http://127.0.0.1:8000 (34136)
...
Use -b
to change the port (address).
$ gunicorn -b 127.0.0.1:8888 sample:app
[2016-12-15 20:41:29 +0900] [34225] [INFO] Starting gunicorn 19.6.0
[2016-12-15 20:41:29 +0900] [34225] [INFO] Listening at: http://127.0.0.1:8888 (34225)
...
It is in the req
in the arguments such as ʻon_get`.
sample.py
class ParamsResource:
def on_get(self, req, resp):
params = req.params
resp.body = json.dumps(params, ensure_ascii=False)
app = falcon.API()
app.add_route('/params', ParamsResource())
/params
=> {}
/params?a=hoge&b=fuga
=> {"a": "hoge", "b": "fuga"}
I got it. It's a good idea to read the request and response here. req/resp
There is no specific means in the standard. I will prepare it myself.
I'm using MySQL in my environment, so I'll try to access it.
First, install MySQL-Python
. Introducing the familiar pip
.
$ pip install MySQL-Python
And write. Please change the DB information as appropriate.
sample.py
...
class BusinnesTermResource:
def on_get(self, req, resp):
connection = MySQLdb.connect(
host='localhost',
port=3306,
user='root',
passwd='password',
db='sample',
charset='utf8',
cursorclass=MySQLdb.cursors.DictCursor
)
cursor = connection.cursor()
sql = "select * from business_term"
cursor.execute(sql)
term = cursor.fetchone()
cursor.close()
connection.close()
resp.body = json.dumps(term, ensure_ascii=False)
app = falcon.API()
app.add_route('/businnes_term', BusinnesTermResource())
...
I got it.
{"term": "Hoge", "meening": "Fuga", "example": "sample", "id": 1}
sample.py
# -*- coding: utf-8 -*-
# sample.py
import falcon
import json
import MySQLdb
from MySQLdb.cursors import DictCursor
class BusinnesTermResource:
def on_get(self, req, resp):
connection = MySQLdb.connect(
host='localhost',
port=3306,
user='root',
passwd='password',
db='sample',
charset='utf8',
cursorclass=MySQLdb.cursors.DictCursor
)
cursor = connection.cursor()
sql = "select * from business_term"
cursor.execute(sql)
term = cursor.fetchone()
cursor.close()
connection.close()
resp.body = json.dumps(term, ensure_ascii=False)
class SampleResource:
def on_get(self, req, resp):
obj = {
"header": "Hello!",
"body": {
"a": "hoge",
"b": "fuga",
}
}
resp.body = json.dumps(obj, ensure_ascii=False)
class ParamsResource:
def on_get(self, req, resp):
params = req.params
resp.body = json.dumps(params, ensure_ascii=False)
app = falcon.API()
app.add_route('/businnes_term', BusinnesTermResource())
app.add_route('/sample', SampleResource())
app.add_route('/params', ParamsResource())
if __name__ == "__main__":
from wsgiref import simple_server
httpd = simple_server.make_server("127.0.0.1", 8000, app)
httpd.serve_forever()
It was very easy to make and it felt good. It's quick to write and start up, so it's crisp. I would like to use it for testing once, or with a suitable and good application that does not have such a large number of users.
Recommended Posts