基於Flask的最簡Web請求

2021-08-22 15:03:32 字數 926 閱讀 4552

這裡使用了flask作為web服務的框架,其簡單精悍,非常易學易用。

**如下:

import logging.config

import json

from flask import flask

from flask import make_response, request

logging.config.fileconfig("logging.conf")

logger = logging.getlogger("filelogger")

print("hello world")

deftestinfo

(): logger.info("test information in test block")

keyword = request.args.get('key', '')

resultjson = {}

resultjson['msg'] = 'it is a great world'

resultjson['status'] = 0

resultjson['code'] = keyword

jsonstr = json.dumps(resultjson)

response = make_response(jsonstr)

return response

if __name__ == "__main__":

print("main...")

**中的logging.conf是配置日誌的路徑、格式以及檔案大小的,大家也可以直接去掉,將日誌輸出到控制台。

這裡定義了乙個get請求,並以json的方式,返回結果資訊。

main『是在python程式定義程式主入口的方法。

這裡的例項非常簡單,複雜的例子大家可以自行上網參照其他內容。

最簡Flask 路由例項

import time input time 2020 05 01 result time time.strptime input time,y m d print result time.tm yday time.strptime 將指定格式的時間轉換為9元素 九元素 年 日 時 分 秒 一周的第...

python 基於flask的簡單web查詢程式

3個.py檔案 create html.py 利用pandas 將result生成 形式 mysql.py 連線資料庫獲取資料.from flask import flask from flask import request from create html import convert to h...

Flask的請求鉤子與上下文簡覽

在客戶端和伺服器互動的過程中,有些準備工作或掃尾工作需要處理,比如 在請求開始時,建立資料庫連線 在請求結束時,指定資料的互動格式。為了讓 每個檢視函式避免編寫重複功能的 flask提供了通用設施的功能,即請求鉤子。通過請求鉤子,我們可以對請求進行預處理 preprocessing 和後處理 pos...