flask實(shí)現(xiàn)python方法轉(zhuǎn)換服務(wù)的方法
一.flask安裝

二.flask簡介:
flask是一個(gè)web框架,可以通過提供的裝飾器@server.route()將普通函數(shù)轉(zhuǎn)換為服務(wù)
flask是一個(gè)web框架,屬于微框架,框架很輕量,更新依賴小,依賴于werkzeug,一個(gè)wsgi工具包(web server gateway interface),為python語言定義的web服務(wù)器和web應(yīng)用程序或框架之間的一種簡單而通用的接口
三 flash實(shí)現(xiàn)python腳本web服務(wù)化-get方法
import flask,json
from flask import request
#創(chuàng)建一個(gè)服務(wù),將當(dāng)前這個(gè)python文件作為一個(gè)服務(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':'賬戶密碼錯(cuò)誤'}
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代表不管幾個(gè)網(wǎng)卡,任何ip都可以訪問網(wǎng)頁調(diào)用查看結(jié)果:
1.無用戶登錄成功,code:1001

2.用戶登錄成功

3.用戶登錄失敗

四 flash實(shí)現(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ù),因?yàn)槲疫@?傳過來的數(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實(shí)現(xiàn)python方法轉(zhuǎn)換服務(wù)的文章就介紹到這了,更多相關(guān)python方法轉(zhuǎn)換服務(wù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)擴(kuò)展內(nèi)置類型的方法分析
這篇文章主要介紹了Python實(shí)現(xiàn)擴(kuò)展內(nèi)置類型的方法,結(jié)合實(shí)例形式分析了Python嵌入內(nèi)置類型擴(kuò)展及子類方式擴(kuò)展的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-10-10
python實(shí)現(xiàn)讀取excel文件中所有sheet操作示例
這篇文章主要介紹了python實(shí)現(xiàn)讀取excel文件中所有sheet操作,涉及Python基于openpyxl模塊的Excel文件讀取、遍歷相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
為什么str(float)在Python 3中比Python 2返回更多的數(shù)字
很多朋友質(zhì)疑為什么str(float)在Python 3中比Python 2返回更多的數(shù)字,在Python 2.7中,一個(gè)float的repr返回最接近十七位數(shù)的十進(jìn)制數(shù);這足以精確地識(shí)別每個(gè)可能的IEEE浮點(diǎn)值。對此問題很多朋友都很疑問,下面小編給大家簡單介紹下,需要的朋友可以參考下2018-10-10
Python實(shí)現(xiàn)的矩陣轉(zhuǎn)置與矩陣相乘運(yùn)算示例
這篇文章主要介紹了Python實(shí)現(xiàn)的矩陣轉(zhuǎn)置與矩陣相乘運(yùn)算,結(jié)合實(shí)例形式分析了Python針對矩陣進(jìn)行轉(zhuǎn)置與相乘運(yùn)算的相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-03-03

