Werkzeug学习笔记

来源:互联网 发布:linux 修改文件权限 编辑:程序博客网 时间:2024/05/29 17:02

useragent模块

  1. UserAgent(ua:str)
    # ua测试字符串    ua = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36'    # 实例化    user_agent = useragents.UserAgent(ua)    .platform -> windows    .version -> 60.0.3112.101    .string -> Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36    .language -> None    .brower -> chrome

urls模块

routing模块

wsgi模块

  1. get_current_url(environ, root_only=False, strip_querystring=False,host_only=False, trusted_hosts=None)
  2. host_is_trusted(hostname, trusted_list) -> 暂时不清楚
  3. get_host(environ, trusted_hosts=None) -> 传入env,获取host
  4. get_content_length(environ)
  5. get_input_stream(environ, safe_fallback=True, max_content_length=None)
  6. get_input_stream(environ, safe_fallback=True, max_content_length=None) -> 从WSGI环境返回输入流,并以最明智的方式进行包装。 在大多数情况下,返回的流不是原始的WSGI流,但是在不考虑内容长度的情况下可以安全地读取流。(暂时不清楚)
  7. get_query_string(environ)
  8. get_path_info(environ, charset='utf-8', errors='replace') -> URL路径除了起始部分后的剩余部分,用于找到相应的应用程序对象,如果请求的路径就是根路径,这个值为空字符串 从WSGI环境返回“PATH_INFO”,并正确解码它。 这也关注了在Python 3环境下的WSGI解码舞蹈。 如果charset设置为None,则返回一个bytestring。(暂时不清楚)
  9. get_script_name(environ, charset='utf-8', errors='replace')
  10. pop_path_info(environ, charset='utf-8', errors='replace') -> 删除并返回“PATH_INFO”的下一个段,将其推送到“SCRIPT_NAME”上。 如果PATH_INFO上没有任何内容,则返回None。 如果charset设置为None,则返回一个bytestring。 如果有空段('/ foo // bar)这些被忽略,但被正确地推到“SCRIPT_NAME”
  11. peek_path_info(environ, charset='utf-8', errors='replace') -> 如果没有,返回“PATH_INFO”或“无”的下一个段。 工作原理如下:func:pop_path_info而不修改环境
  12. extract_path_info(environ_or_baseurl, path_or_url, charset='utf-8', errors='replace', collapse_http_schemes=True) -> 从给定的URL(或WSGI环境)和路径中提取路径信息。 返回的路径信息是一个unicode字符串,而不是一个适合WSGI环境的bytest。 这些URL也可能是IRI。 如果无法确定路径信息,则返回“None”
  13. class SharedDataMiddleware(object) -> 用于处理静态文件
  14. class DispatcherMiddleware(object) -> 组合多个app

    application = DispatcherMiddleware(firstapp, {'/secondapp': secondapp, '/thirdapp': thirdapp})
    run_simple('localhost', 5000, application, use_reloader=True, use_debugger=True)

wrappers模块

  1. BaseRequest对象

    • form属性,数据结构为werkzeug.datastructures.ImmutableMultiDict,可以通过parameter_storage_class来修改(如果表单顺序有要求,这可能是必需的)
    • valules属性,把form表单和get参数组合起来放在一个字典里面,数据结构为CombinedMultiDict。例如:

      CombinedMultiDict([ImmutableMultiDict([('name', 'xiaoming')]), ImmutableMultiDict([('age', '12')])])    取值:        name = values.get('name')        age = values.get('age)
    • files属性,包括所有的上传文件。数据结构werkzeug.datastructures.MultiDict,键名为input标签的name属性,每个value代表一个真实文件,数据结构为werkzeug.datastructures.FileStorage,就像一个标准的Python文件对象,不过有一个单独的方法.save()用来将文件存放在本地。注意客户端需使用POST/PUT/PATCH的其中一个方式,并且表单要设置enctype="multipart/form-data,否则会接收到空字典

    • cookies属性,一个字典,里面存放了cookies信息
    • headers属性,数据结构werkzeug.datastructures.EnvironHeaders,从WSGI环境里面读出来的头信息,dict形式
    • full_path属性,http://localhost:5000/form?namea=xiaoming -> /form?namea=xiaoming
    • script_root属性,The root path of the script without the trailing slash.
    • url属性,全路径
    • base_url,基本路径http://localhost:5000/form?namea=xiaoming -> http://localhost:5000/form
    • url_root -> 根路径 http://localhost:5000/form?namea=xiaoming -> http://localhost:5000/
    • host_url -> host+协议 http://localhost:5000/form?namea=xiaoming -> http://localhost:5000/
    • host -> localhost:5000
    • query_string get的查询数据 http://localhost:5000/form?namea=xiaoming ->b'namea=xiaoming'
    • method 请求方法GET/POST
    • access_route 如果转发的头部存在,这是从客户端ip到最后一个代理服务器的所有ip地址的列表
    • remote_addr -> 客户端IP
    • remote_user -> If the server supports user authentication, and the script is
      protected, this attribute contains the username the user has
      authenticated as.
    • scheme -> 协议 http/https
    • is_xhr属性,用X-Requested-With来判断的,所以不一定可信
    • environ一个wsgi和env对象,是一个字典
      • REQUEST_METHOD -> GET|POST
      • SCRIPT_NAME -> ''
      • PATH_INFO -> /form
      • QUERY_STRING -> namea=xiaoming
      • SERVER_NAME -> 127.0.0.1
      • SERVER_PORT -> 5000
      • HTTP_HOST -> localhost:5000
      • SERVER_PROTOCOL -> HTTP/1.1
      • wsgi.url_scheme -> http
      • CONTENT_TYPE -> ”
      • CONTENT_LENGTH -> 0
      • HTTP_CONNECTION -> close
      • HTTP_COOKIE
      • HTTP_ACCEPT_LANGUAGE -> zh-CN,zh;q=0.8
      • HTTP_ACCEPT_ENCODING -> gzip, deflate, br
      • HTTP_DNT -> 1
      • HTTP_ACCEPT -> text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
      • HTTP_USER_AGENT
      • HTTP_UPGRADE_INSECURE_REQUESTS
      • HTTP_CACHE_CONTROL -> max-age=0
      • HTTP_HOST -> localhost:5000
      • REMOTE_PORT -> 50556
      • REMOTE_ADDR -> 127.0.0.1
      • SERVER_SOFTWARE -> Werkzeug/0.12.2
      • wsgi.version -> 1,0
      • wsgi.url_scheme -> https
      • wsgi.input -> ''
      • wsgi.errors
      • wsgi.multithread -> False
      • wsgi.multiprocess -> False
      • wsgi.run_once -> False
  2. BaseResponse对象
    - set_cookie(self, key, value='', max_age=None, expires=None, path='/', domain=None, secure=False, httponly=False) -> 设置响应体的Cookies
    - delete_cookie(self, key, path='/', domain=None) -> 删除cookie

utils模块

  1. secure_filename(filename) -> 返回安全文件名
  2. escape(s,quote=None) -> 字符串转义
  3. unescape(s) -> 反转义
  4. redirect(location, code=302, Response=None) -> 重定向
  5. append_slash_redirect(environ, code=301) -> 给url的尾巴上添加/,以生成唯一的url。haonse.com -> haonse.com/

serving模块

security模块

  1. pbkdf2_hex(data, salt, iterations=DEFAULT_PBKDF2_ITERATIONS,keylen=None, hashfunc=None) -> 类似 :func:pbkdf2_bin, but 返回一个 hex-encoded 字符串
  2. pbkdf2_bin(data, salt, iterations=DEFAULT_PBKDF2_ITERATIONS, keylen=None, hashfunc=None) -> pbkdf2算法生成密钥
  3. safe_str_cmp(a, b)- > 两个字符串比较
  4. gen_salt(length) -> 生成一个盐(随机字符串)
  5. generate_password_hash(password, method='pbkdf2:sha256', salt_length=8) -> 用指定的方法和盐的长度生成密码
  6. check_password_hash(pwhash, password) -> 验证上面的
  7. safe_join(directory, *pathnames) -> 生成一个安全的path

local模块

formparser模块

http模块

  1. wsgi_to_bytes(data) -> 将data转成latin1
  2. bytes_to_wsgi -> 相反
  3. parse_cookie(header, charset='utf-8', errors='replace', cls=None): -> 把raw_cookies解析成一个字典
  4. dump_cookie(key, value='', max_age=None, expires=None, path='/', domain=None, secure=False, httponly=False, charset='utf-8', sync_expires=True, max_size=4093): -> 装成一个cookie

filesystem模块

  1. get_filesystem_encoding -> 返回系统编码。注意:这与Python对文件系统编码的理解不同,不建议将此值用于Python的unicode API。(不知道这个函数是用来干什么的,用到了再看)

datastructures模块

exceptions模块

posixemulation模块

test模块

原创粉丝点击