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的用法

也可以在这里用

将cbv 中的类里面的函数变成fbv中的函数,本质上还是fbv

 

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.位运算

或运算

 
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 等。不同的框架有不同的开发方式,但是无论如何,开发出的应用程序都要和服务器程序配合,才能为用户提供服务。这样,服务器程序就需要为不同的框架提供不同的支持。这样混乱的局面无论对于服务器还是框架,都是不好的。对服务器来说,需要支持各种不同框架,对框架来说,只有支持它的服务器才能被开发出的应用使用。这时候,标准化就变得尤为重要。我们可以设立一个标准,只要服务器程序支持这个标准,框架也支持这个标准,那么他们就可以配合使用。一旦标准确定,双方各自实现。这样,服务器可以支持更多支持标准的框架,框架也可以使用更多支持标准的服务器。

WSGI(Web Server Gateway Interface)是一种规范,它定义了使用python编写的web app与web server之间接口格式,实现web app与web server间的解耦。

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()

 

原文地址:https://www.cnblogs.com/shanghongyun/p/10268809.html