Make your own API with Python+flask (tutorial with source code)

Source: Internet
Author: User

1. Background

Ok, maybe a lot of friends like me often use a variety of APIs, such as Facebook, GitHub, and even API. So many people also want to make their own API. Online on this aspect of the course is very few, today I have done one, the method published.

First show the effect:

Using the "Curl" method, return a JSON, you can also try:

Curl-i HTTP://OSPAFZONE.DUAPP.COM/OSPAF



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 local running environment of the code, after the execution can be entered in the browser "Http://127.0.0.1:5000/", you can see the effect, the return is a JSON-formatted file.



Attention:

@app. Route ('/', methods=[' GET ')

This line of code '/' corresponds to the following function definition Def home (), debug url "http://127.0.0.1:5000/"

If it is '/OSPAF ', the corresponding function defines Def OSPAF (), the debug URL "HTTP://127.0.0.1:5000/OSPAF"


OK, it should be said very clearly.


/********************************

* This article from the blog "Bo Li Garvin"

* Reprint Please indicate the source : Http://blog.csdn.net/buptgshengod

******************************************/





Make your own API with Python+flask (tutorial with source code)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.