CentOS7 installation Python3 for flask experiments __python

Source: Internet
Author: User
Tags http post
Install Python3

Because CentOS7 originally installed the Python2.7, and can not be deleted, because many system commands are dependent on Python2, such as we often use the Yum tools.
So, we first need to install Python3 separately, we find the Python official website ftp:
https://www.python.org/ftp/python/
Then find the version we want, like Python3.6.5, and then download it with wget:

# wget Https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz

Then create a new directory:

# Mkdir/usr/local/python3

Extract the python-3.6.5.tgz that you just downloaded to the directory:

# cd/usr/local/python3
# tar zxvf/root/python-3.6.5.tgz

Then configure, compile, and install:

# CD Python-3.6.5
#./configure--prefix=/usr/local/python3
# make
Install

For ease of use, we create soft links for it:

# ln-s/usr/local/python3/bin/python3/usr/bin/python3
# ln-s/USR/LOCAL/PYTHON3/BIN/PIP3/USR/BIN/PIP3

Installation completed, check:

# Python3--version
Python 3.6.5

# pip3--version
pip 9.0.3 from/usr/local/python3/lib/python3.6/ Site-packages (Python 3.6)
Flask Experiment

Flask is a lightweight WEB framework written in Python (so we spent so much time in front of it to install Python3). It is called a micro-architecture because it uses very simple cores and feature-rich extensions. Although the flask kernel does not have a default database, validation tools, etc., it is extensible by adding ORM, verification tools, file uploads, and open authentication to enable very powerful WEB applications.
Okay, that's actually flask's installation is very simple:

# PIP3 Install flask

After the installation, we write a back-end code app.py, as follows:

#-*-Coding:utf-8-*-from
flask import flask, jsonify, render_template, request
app = Flask (__name__)

@app. Route ("/")
def Index (): Return
    render_template (' index.html ')

@app. Route ('/value ', methods=[' POST '))
def cal_value ():
  if Request.method = = ' POST ':
    a = Request.form.get (' A ', 0, type=int)
    B = Request.form.get (' B ', 0, Type=int) return
    jsonify (result = a + b)

if __name__== "__main__":
    App.run (host = ' 0.0.0.0 ', port = 8080, debug = True)

We know that Flask uses an adorner @app. Route ("/") to define the URL path. Therefore, the app.py has two routing features: one route can be accessed using the HTTP GET method, return an HTML page, and another route can be accessed using the HTTP POST method, returning the result of the addition of two parameters A and B.

Index.html's code is also simple, where JavaScript is written based on JQuery, as follows:

<! DOCTYPE html>  

The entire engineering directory structure is as follows:

# tree 
.
├──app.py
├──static
│   └──jquery.js
└──templates
    └──index.html

2 directories, 3 files

Next, we run the Web back end:

# Python3 app.py 
 * Running on http://0.0.0.0:8080/(press CTRL + to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger pin:184-286-467

Open the browser and enter ip:8080 to access:

We enter the parameters A and B in the input box, click on the calculation, you can see the results. Experiment finished ~

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.