A simple API server made by flask+sqlite3, support multithreading
python 2.7
flask
requests
requests
is not really necessary, it is only used for testing API in our case.
https://www.python.org/downloads/
pip install flask
pip install requests
python server.py
The default ip address is 127.0.0.1
and the default port is 5000
, you can change them in const.py
. Set ip address to 0.0.0.0
to have the server available externally.
The error and info messages would be saved in server.log
.
Also, remember to change the username and password of SMS in const.py
if you want to use the API to send short message(here I use service of SMS King).
Make sure that you use post to make every requests, and follow the data type of example.
Here I use requests
to demonstrate how to use the APIs, You can also see them in client.py
.
First, you need to import requests
:
import requests
Devices can report positions by add_pos
.
url = 'http://127.0.0.1:5000/api/v1/add_pos'
payload = {'device_id': 123, 'lng': 79.123456, 'lat': 84.567890}
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
print r.content
App can get the latest position of the device by get_pos
.
url = 'http://127.0.0.1:5000/api/v1/get_pos'
payload = {'device_id': 123}
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
print r.content
Other applications(in localhost) can fetch the data by sending any query to do_query
.
url = 'http://127.0.0.1:5000/api/v1/do_query'
payload = {'query': 'select * from records', 'args': []}
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
print r.content
Parameter arg in payload should be remained even if it's empty, just like the example shows.
Devices can use send_msg
to send the short messages to the target phone.
url = 'http://127.0.0.1:5000/api/v1/send_msg'
payload = {'phone_num': '0987654321'}
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
print r.content
This project is licensed under the MIT License
一個以flask+sqlite3做的簡易API server,支援multithreading
python 2.7
flask
requests
requests
在此只是拿來測試API用,並非必要
https://www.python.org/downloads/
pip install flask
pip install requests
python server.py
ip的初始值是127.0.0.1
,port則是5000
,你可以在const.py
改動他們。把ip設為0.0.0.0
則可將server對外開放。錯誤和運行訊息會存在server.log
。
另外要注意的是如果要使用server的傳送簡訊功能,需在const.py
設定SMS帳號密碼,這裡我是使用簡訊王的SMS。
注意,任何request都須以post傳送,並且遵守範例中的data型式
這裡我用requests
示範如何使用這些API,你可以在clinet.py
看到完整的範例,首先你得import requests
import requests
裝置可以藉由add_pos
回報他們的位置。
url = 'http://127.0.0.1:5000/api/v1/add_pos'
payload = {'device_id': 123, 'lng': 79.123456, 'lat': 84.567890}
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
print r.content
App可以藉由get_pos
獲取裝置最新的位置.
url = 'http://127.0.0.1:5000/api/v1/get_pos'
payload = {'device_id': 123}
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
print r.content
Localhost下的其他程式可以藉由do_query
問任何query以獲取自己想要的data。
url = 'http://127.0.0.1:5000/api/v1/do_query'
payload = {'query': 'select * from records', 'args': []}
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
print r.content
在payload中,參數args就算是空的也要留著,如同範例所示
裝置可以藉由send_msg
傳送簡訊至目標手機.
url = 'http://127.0.0.1:5000/api/v1/send_msg'
payload = {'phone_num': '0987654321'}
headers = {'content-type': 'application/json'}
r = requests.post(url, data=json.dumps(payload), headers=headers)
print r.content