Use Python+flask to get your own API (tutorial source code)

Source: Internet
Author: User

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)

Related Article

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.