Flask学习
environ为一个包含所有HTTP请求信息的dict
对象;如下所示,重点有
- PATH_INFO,url 的路径
- “SERVER_PORT”: “8000”
- “REQUEST_METHOD”: “GET”
- “wsgi.url_scheme”: “http”
- “HTTP_USER_AGENT”: “Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 OPR/57.0.3098.116”
{"XDG_SESSION_ID": "37757", "LC_IDENTIFICATION": "zh_CN.UTF-8", "LC_TIME": "zh_CN.UTF-8", "LC_NUMERIC": "zh_CN.UTF-8", "HTTP_CACHE_CONTROL": "max-age=0", "LC_ADDRESS": "zh_CN.UTF-8", "LC_PAPER": "zh_CN.UTF-8", "PATH": "/home/ubuntu/bin:/home/ubuntu/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin", "wsgi.multithread": true, "HTTP_ACCEPT": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "_": "/usr/bin/python3", "OLDPWD": "/home/ubuntu", "SCRIPT_NAME": "", "LOGNAME": "ubuntu", "LANG": "en_US.utf8", "PATH_INFO": "/hello/world", "LC_MONETARY": "zh_CN.UTF-8", "CONTENT_TYPE": "text/plain", "HTTP_UPGRADE_INSECURE_REQUESTS": "1", "SERVER_PORT": "8000", "PWD": "/home/ubuntu/mypython", "LC_TELEPHONE": "zh_CN.UTF-8", "CONTENT_LENGTH": "", "LESSCLOSE": "/usr/bin/lesspipe %s %s", "USER": "ubuntu", "wsgi.url_scheme": "http", "HTTP_ACCEPT_ENCODING": "gzip, deflate", "SHLVL": "1", "REQUEST_METHOD": "GET", "SSH_CLIENT": "222.20.59.48 51290 22", "SERVER_PROTOCOL": "HTTP/1.1", "wsgi.multiprocess": false, "HTTP_CONNECTION": "keep-alive", "MAIL": "/var/mail/ubuntu", "REMOTE_HOST": "", "LC_NAME": "zh_CN.UTF-8", "HTTP_ACCEPT_LANGUAGE": "en-US,en;q=0.9", "PROMPT_COMMAND": "history -a; ", "SHELL": "/bin/bash", "GATEWAY_INTERFACE": "CGI/1.1", "HOME": "/home/ubuntu", "HTTP_HOST": "148.70.30.113:8000", "SSH_CONNECTION": "223.20.184.48 51290 10.0.0.12 22", "SSH_TTY": "/dev/pts/0", "LESSOPEN": "| /usr/bin/lesspipe %s", "wsgi.version": [1, 0], "HISTSIZE": "3000", "HTTP_USER_AGENT": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 OPR/57.0.3098.116", "TERM": "xterm-256color", "HISTTIMEFORMAT": "%F %T ", "SERVER_NAME": "localhost.localdomain", "XDG_RUNTIME_DIR": "/run/user/500", "QUERY_STRING": "", "REMOTE_ADDR": "223.20.184.48", "LC_MEASUREMENT": "zh_CN.UTF-8", "SERVER_SOFTWARE": "WSGIServer/0.2", "wsgi.run_once": false}
flask中的Map和Rule的用法如下所示,Map的值可以初始化确定,也可以通过Map.add(rule)进行添加,其中参数为Rule类型
from werkzeug.wrappers import Request, Response
from werkzeug.routing import Map, Rule
from werkzeug.exceptions import HTTPException, NotFound
from werkzeug.wsgi import SharedDataMiddleware
from werkzeug.utils import redirect
import json
url_map = Map([
Rule('/', endpoint='new_url'),
Rule('/abc/<name>', endpoint='follow_short_link'),
Rule('/ddddddd', endpoint='short_link_details'),
Rule('/hello/world', endpoint='hello_world')
])
with open("environ.txt", 'r') as f:
environ=json.load(f)
url_adapter = url_map.bind_to_environ(environ)
b = url_adapter.build("hello_world")
print("build ")
print(b)
rv = url_adapter.match()
print("match ")
print(rv)
url_adapter.build(“hello_world”)能够根据endpoint去获取路径
url_adapter.match()会根据environ的数据进行匹配,返回endpoint和value值
flask再根据endpoint值获取相应的响应函数