1. Background
OK, this may be a lot of friends and I like to use a variety of APIs, examples of Facebook. GitHub's. Even the API. Therefore, many people want to make their own API. Online tutorials In this regard it is very small, and today, I made a smooth, release time under the method.
First show the effect:
Using the "Curl" method, return a JSON, you can also try:
Curl-i HTTP://OSPAFZONE.DUAPP.COM/OSPAF
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvynvwdgdzagvuz29k/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "/>
2. Code
First of all, the environment, is python+flask, especially good configuration.
From flask import flask, Jsonifyapp = Flask (__name__) tasks = [ { ' id ': 1, ' title ': U ' OSPA ', ' description ': U ' this was Ospaf-api test ', ' done ': False }, { ' id ': 2, ' title ': U ' Garvin ', ' description ': U ' I am Garvin ', ' done ': False }] @app. Route ('/', methods=[' GET ') def home (): return jsonify ({' Tasks ': Tasks}) If __name__ = = ' __main__ ': app.run (debug=true)
This is the code in the local execution environment that can enter "Http://127.0.0.1:5000/" in the browser after execution. You can see the effect. The returned file is in a JSON format.
Attention:
@app. Route ('/', methods=[' GET ')
This line of code '/', corresponding to the following function definition Def home (), debug url "http://127.0.0.1:5000/"
The hypothesis is '/OSPAF ', the corresponding function defines Def OSPAF (), the debug URL "HTTP://127.0.0.1:5000/OSPAF"
OK, that should be very clear.
/********************************
* This article from the blog "Bo Li Garvin"
* Reprint Please indicate the source : Http://blog.csdn.net/buptgshengod
******************************************/
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Use Python+flask to get your own API (tutorial source code)