flask(三)

1.cbv的用法

from flask import Flask,views
app = Flask(__name__)

class Login(views.MethodView ):
    def get(self):
        return "hello"

app.add_url_rule("/",view_func=Login.as_view("my_login"),methods=["GET"])

app.run("0.0.0.0",5000,debug=True)

methods的用法html

也能夠在這裏用python

將cbv 中的類裏面的函數變成fbv中的函數,本質上仍是fbvweb

 

2.網絡相關

192.168.13.0-192.168.13.255

00000000,11111111,192.168.13.1不能用

能用253個
192.168.13.130/24

11111111.11111111.11111111.00000000-11111111

192.168.1.0-192.168.1.255
192.168.13.47/16

11111111.11111111.0000000.00000000

192.168.0.0-192.168.255.255
192.168.13.61/17

11111111.11111111.10000000.00000000

192.168.0.0-192.168.127.255
192.168.13.47/30

1111111.11111111.11111111.11111100

192.168.13.0-192.168.13.3
192.168.13.47/18

11111111.11111111.11000000.00000000

192.168.0.0-192.168.63.255

 

3.位運算

或運算flask

 
print(255|128)#255

11111111
10000000
11111111
(只要有1就爲1)

非運算安全

print(255|128)#127

11111111
10000000
01111111

與運算服務器

print(255|128)#128

11111111
10000000
10000000

 

4.ip地址分類

A類:255.0.0.0,包括的可用地址最多

B類:255.255.0.0

C類:255.255.255.0

 

5.flask上下文werkzeug

from werkzeug.serving import run_simple
from werkzeug.wrappers import Response,Request

@Request.application
def app(req):
    print(req.method)  #get
    print(req.path)  #/
    return Response("ok")

run_simple('0.0.0.0',5000,app)

 

6.偏函數

from functools import partial
def add(a,b):
    return a+b

par_add=partial(add,1)
print(par_add(2))#2

 

應用:函數在執行時,要帶上全部必要的參數進行調用。可是,有時參數能夠在函數被調用以前提早獲知。這種狀況下,一個函數有一個或多個參數預先就能用上,以便函數能用更少的參數進行調用。網絡

 

7.線程安全

 
import time
import threading
from threading import local

class Foo(local):
    pass

foo = Foo()
"""
{
    7172:[request,session],
    8076:[request,session],
    5784:[request,session],
}
"""

def add(i):
    foo.num=i
    time.sleep(1)
    print(foo.num,i,threading.current_thread().ident)

for i in range(20):
    th = threading.Thread(target=add,args=(i,))
    th.start()

 

8.wsgiref實例

對於python web程序來講,通常會分爲兩部分:服務器程序和應用程序。服務器程序負責對socket服務器進行封裝,並在請求到來時,對請求的各類數據進行整理。應用程序則負責具體的邏輯處理。爲了方便應用程序的開發,就出現了衆多的Web框架,例如:Django、Flask、web.py 等。不一樣的框架有不一樣的開發方式,可是不管如何,開發出的應用程序都要和服務器程序配合,才能爲用戶提供服務。這樣,服務器程序就須要爲不一樣的框架提供不一樣的支持。這樣混亂的局面不管對於服務器仍是框架,都是很差的。對服務器來講,須要支持各類不一樣框架,對框架來講,只有支持它的服務器才能被開發出的應用使用。這時候,標準化就變得尤其重要。咱們能夠設立一個標準,只要服務器程序支持這個標準,框架也支持這個標準,那麼他們就能夠配合使用。一旦標準肯定,雙方各自實現。這樣,服務器能夠支持更多支持標準的框架,框架也可使用更多支持標準的服務器。session

WSGI(Web Server Gateway Interface)是一種規範,它定義了使用python編寫的web app與web server之間接口格式,實現web app與web server間的解耦。app

python標準庫提供的獨立WSGI服務器稱爲wsgiref。框架

 
from wsgiref.simple_server import make_server

def runserver(environ,start_response):
    start_response("200 ok",[("Content-Type","text/html")])
    return [bytes("<h1>hello aa</h1>",encoding="utf-8")]

if __name__ == "__main__":
    httpd=make_server("",8000,runserver)
    print("運行了")
    httpd.serve_forever()

 

相關文章
相關標籤/搜索