二,使用框架完成http python伺服器

2021-07-11 22:03:58 字數 2477 閱讀 4663

參看:

**皆來著原作者,保留**內署名。

了解了http協議和html文件,我們其實就明白了乙個web應用的本質就是:

瀏覽器傳送乙個http請求;

伺服器收到請求,生成乙個html文件;

伺服器把html文件作為http響應的body傳送給瀏覽器;

瀏覽器收到http響應,從http body取出html文件並顯示。

所以,最簡單的web應用就是先把html用檔案儲存好,用乙個現成的http伺服器軟體,接收使用者請求,從檔案中讀取html,返回。apache、nginx、lighttpd等這些常見的靜態伺服器就是幹這件事情的。

# hello.py

def(environ, start_response):

start_response('200 ok', [('content-type', 'text/html')])

return

''

建立伺服器:

# server.py

# 從wsgiref模組匯入:

# 開始監聽http請求:

():# 需要從request物件讀取表單內容:

if request.form['username']=='admin'

and request.form['password']=='password':

return

''return

''if __name__ == '__main__':

from flask import flask, request, render_template

defhome

():return render_template('home.html')

defsignin_form

():return render_template('form.html')

defsignin

(): username = request.form['username']

password = request.form['password']

if username=='admin'

and password=='password':

return render_template('signin-ok.html', username=username)

return render_template('form.html', message='bad username or password', username=username)

if __name__ == '__main__':

編寫靜態html(以form.html為例):

please sign intitle>

head>

style="color:red">

}p>

action="/signin"

method="post">

please sign in:legend>

name="username"

placeholder="username"

value="

}">

p>

name="password"

placeholder="password"

type="password">

p>

type="submit">sign inbutton>

p>

form>

body>

html>

Maya FEM節點框架完成

這幾天把物理模擬框架移植到maya之中了。maya程式設計有一點比較關鍵,就是要讓自己的程式邏輯適應maya的節點求值機制。在物理模擬中,往往需要進行時間積分,對此我的解決辦法是,寫乙個節點rigsimulator,存放模擬的狀態資料 例如 位置 速度 加速度 過去模擬的結果 再寫乙個mel命令ri...

通訊錄主題框架 完成增刪查改

define crt secure no warnings include include include 實現乙個通訊錄程式,完成聯絡人資訊的儲存.1.新增 2.刪除 3.修改記錄 4.查詢記錄 5.列印全部記錄 6.排序記錄 7.清空全部記錄 管理 1.把基本資訊抽象並描述出來 結構體 2.需要...

驅動框架7 使用gpiolib完成led驅動

1 第1步 使用gpio request申請要使用的乙個gpio 2 第2步 gpio direction input gpio direction output 設定輸入 輸出模式 3 第3步 設定輸出值gpio set value 獲取io口值gpio get value。1 在led1上編寫 ...