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)文章
python global的創(chuàng)建和修改實例講解
在本篇文章里小編給大家整理了一篇關(guān)于python global的創(chuàng)建和修改實例講解內(nèi)容,有興趣的朋友們可以學習下。2021-09-09python使用socket實現(xiàn)的傳輸demo示例【基于TCP協(xié)議】
這篇文章主要介紹了python使用socket實現(xiàn)的傳輸demo,結(jié)合實例形式分析了Python使用socket庫基于TCP協(xié)議實現(xiàn)的客戶端與服務(wù)器端相關(guān)操作技巧,需要的朋友可以參考下2019-09-09Github?Copilot結(jié)合python的使用方法詳解
最近也是聽說github出了一種最新的插件叫做copilot,于是申請了,下面這篇文章主要給大家介紹了關(guān)于Github?Copilot結(jié)合python使用的相關(guān)資料,需要的朋友可以參考下2022-04-04python里使用正則的findall函數(shù)的實例詳解
這篇文章主要介紹了python里使用正則的findall函數(shù)的實例詳解的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-10-10在Django的視圖中使用數(shù)據(jù)庫查詢的方法
這篇文章主要介紹了在Django的視圖中使用數(shù)據(jù)庫查詢的方法,是Python的Django框架使用的基礎(chǔ)操作,需要的朋友可以參考下2015-07-07淺談opencv自動光學檢測、目標分割和檢測(連通區(qū)域和findContours)
這篇文章主要介紹了淺談opencv自動光學檢測、目標分割和檢測(連通區(qū)域和findContours),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06python 多線程對post請求服務(wù)器測試并發(fā)的方法
今天小編就為大家分享一篇python 多線程對post請求服務(wù)器測試并發(fā)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-06-06