V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
miniyao
V2EX  ›  Python

from urllib import request 和 from flask import request, 两个 request 库是同一个吗?

  •  
  •   miniyao · 2018-03-09 11:20:41 +08:00 · 2228 次点击
    这是一个创建于 2237 天前的主题,其中的信息可能已经有所发展或是发生改变。

    两个库名字都叫 request,在 flask 里有这样一行:

    request = LocalProxy(partial(_lookup_req_object, 'request'))
    

    两个 request 库是相同的功能吗?

    要是功能有不同,要用 urllib 的 request 就要用别名,或者全路径了吗:

    import urllib.request
    from urllib import request as request-a
    from flask import request
    
    5 条回复    2018-03-09 18:00:32 +08:00
    awanabe
        1
    awanabe  
       2018-03-09 11:24:47 +08:00   ❤️ 1
    不是
    flask 下面有个 request.py 是用来处理 flask 中 request 中的数据分析
    xpresslink
        2
    xpresslink  
       2018-03-09 11:55:46 +08:00   ❤️ 1
    其实真是一个东西
    A 是个客户端,
    import urllib.request
    把这个 request 给 web 服务器 B 发送过来

    在 B 服务器上
    from flask import request
    之后, 这个 request 的内容就是 A 发过来的 request 内容了。

    明白了么?
    在客户端是 sent request
    在服务端是 received request
    woshilibai
        3
    woshilibai  
       2018-03-09 12:15:21 +08:00 via Android
    哈哈哈哈哈哈
    yangzhezjgs
        4
    yangzhezjgs  
       2018-03-09 13:29:31 +08:00
    简单说 from flask import request 是 WSGI 里 environ 的封装
    再说细一点,from flask import request 里的 requests 是 flask/ctx.py 里 RequestContext 的 request,这个 requests 有是基于 werkzeug.wrappers 的 Request,下面是他的__init__函数,可以看到输入的就是 environ
    ···
    def __init__(self, environ, populate_request=True, shallow=False):
    self.environ = environ
    if populate_request and not shallow:
    self.environ['werkzeug.request'] = self
    self.shallow = shallow
    ···
    lolizeppelin
        5
    lolizeppelin  
       2018-03-09 18:00:32 +08:00 via Android
    help 一下就知道了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5482 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 08:01 · PVG 16:01 · LAX 01:01 · JFK 04:01
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.