欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

flask實現(xiàn)python方法轉(zhuǎn)換服務(wù)的方法

 更新時間:2022年05月30日 08:22:28   作者:Mrwhite86  
flask是一個web框架,可以通過提供的裝飾器@server.route()將普通函數(shù)轉(zhuǎn)換為服務(wù),這篇文章主要介紹了flask實現(xiàn)python方法轉(zhuǎn)換服務(wù),需要的朋友可以參考下

一.flask安裝

二.flask簡介:

flask是一個web框架,可以通過提供的裝飾器@server.route()將普通函數(shù)轉(zhuǎn)換為服務(wù)

flask是一個web框架,屬于微框架,框架很輕量,更新依賴小,依賴于werkzeug,一個wsgi工具包(web server gateway interface),為python語言定義的web服務(wù)器和web應(yīng)用程序或框架之間的一種簡單而通用的接口

三 flash實現(xiàn)python腳本web服務(wù)化-get方法

import flask,json
from flask import request

#創(chuàng)建一個服務(wù),將當前這個python文件作為一個服務(wù)
server = flask.Flask(__name__)
#使用裝飾器@server.route()可以將普通的函數(shù)轉(zhuǎn)換為服務(wù)登錄的路徑、請求方法
@server.route('/login',methods=['get','post'])
def login():
    #獲取url請求傳遞的數(shù)據(jù)
    username = request.values.get('username')
    #獲取url請求傳遞密碼、明文
    pwd = request.values.get('pwd')
    #判斷用戶名、密碼都不能為空
    if username and pwd:
        if username=='xiaoming' and pwd =='111':
            resu={'code':200,'message':'登錄成功'}
            return json.dumps(resu,ensure_ascii=False) #將字典轉(zhuǎn)換為json
        else:
            resu = {'code':-1,'message':'賬戶密碼錯誤'}
            return json.dumps(resu,ensure_ascii=False)
    else:
        resu={'code': 1001, 'message': '登錄成功'}
        return json.dumps( resu, ensure_ascii=False )

if __name__ == '__main__':
    server.run(debug=True,port=8888,host='0.0.0.0')#指定端口、host,0.0.0.0代表不管幾個網(wǎng)卡,任何ip都可以訪問

網(wǎng)頁調(diào)用查看結(jié)果:

1.無用戶登錄成功,code:1001

2.用戶登錄成功

3.用戶登錄失敗

四 flash實現(xiàn)python腳本web服務(wù)化-post方法

from flask import Flask, request, jsonify
import json
app = Flask(__name__)
app.debug = True
@app.route('/add/test',methods=['post'])
def add_stu():
    if  not request.data:   #檢測是否有數(shù)據(jù)
        return ('fail')
    student = request.data.decode('utf-8')
    #獲取到POST過來的數(shù)據(jù),因為我這?傳過來的數(shù)據(jù)需要轉(zhuǎn)換?下編碼。根據(jù)晶具體情況?定
    student_json = json.loads(student)
    a=student_json["key"]
    #調(diào)用數(shù)據(jù)處理的核心方法
    res=getData(a)
    student_json["key"]=res
    #把區(qū)獲取到的數(shù)據(jù)轉(zhuǎn)為JSON格式。
    return jsonify(student_json)
    #返回JSON數(shù)據(jù)。

def getData(parameter):
    response = f"hello {parameter} world"
    return response

if __name__ == '__main__':
    app.run(host='127.0.0.1',port=8800)

查看postman方法的調(diào)用:

到此這篇關(guān)于flask實現(xiàn)python方法轉(zhuǎn)換服務(wù)的文章就介紹到這了,更多相關(guān)python方法轉(zhuǎn)換服務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論