Django--httpResponse 头部的问题 - V2EX
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
ray1888
V2EX    Python

Django--httpResponse 头部的问题

  •  
  •   ray1888
    ray1888 2017-04-27 16:58:43 +08:00 3677 次点击
    这是一个创建于 3095 天前的主题,其中的信息可能已经有所发展或是发生改变。
    刚刚去看了 DJango http 的 Response 的源码,但是好像没看到头部内容组装的部分,那有大神能够解答它 Response 头部的参数是那里来的吗?感谢万分
    10 条回复    2017-04-28 20:28:18 +08:00
    blackeeper
        1
    blackeeper  
       2017-04-27 18:27:00 +08:00   1
    ![B31ED17C-3638-423C-B7E1-EFF88C08ED64.png]( https://ooo.0o0.ooo/2017/04/27/5901c75e7dfb5.png)
    eehello
        2
    eehello  
       2017-04-27 18:46:12 +08:00 via Android
    @blackeeper 请问用的什么软件?
    blackeeper
        3
    blackeeper  
       2017-04-27 20:41:51 +08:00
    @eehello dash
    eehello
        4
    eehello  
       2017-04-27 22:10:56 +08:00 via Android
    @blackeeper 谢谢。
    表示没有 mac,只能看着眼馋
    blackeeper
        5
    blackeeper  
       2017-04-27 22:24:18 +08:00
    @eehello windows 也有类似的,你可以试试这个 https://zealdocs.org/
    ray1888
        6
    ray1888  
    OP
       2017-04-27 22:43:02 +08:00
    @blackeeper 我的意思是你 Header 这里不是空的吗?但是最后 response 出来 header 有东西的,那些是在哪里填写的
    IanPeverell
        7
    IanPeverell  
       2017-04-28 10:26:27 +08:00   1
    @ray1888 这部分你可以看源码,是后面设置了__setitem__函数加上去的,参数包括 header,value,先把 header 和 value 用内建的函数重新编码,然后一次性加入_header 字典中,接着是 serialize_headers 函数获取 header 以 bytestring 的格式传给__bytes__或者__str__,然后通过 setdefault 函数设置默认 header 如果预先没有设置 header。

    以上就是我从 Django 的源码得到的部分,如果有误请指正
    eehello
        8
    eehello  
       2017-04-28 12:29:30 +08:00
    @blackeeper 非常感谢
    ray1888
        9
    ray1888  
    OP
       2017-04-28 17:38:30 +08:00
    @IanPeverell

    其实在这个地方还是没有写

    def _convert_to_charset(self, value, charset, mime_encode=False):
    """
    Convert headers key/value to ascii/latin-1 native strings.
    `charset` must be 'ascii' or 'latin-1'. If `mime_encode` is True and
    `value` can't be represented in the given charset, apply MIME-encoding.
    """
    if not isinstance(value, (bytes, str)):
    value = str(value)
    if ((isinstance(value, bytes) and (b'\n' in value or b'\r' in value)) or
    isinstance(value, str) and ('\n' in value or '\r' in value)):
    raise BadHeaderError("Header values can't contain newlines (got %r)" % value)
    try:
    if isinstance(value, str):
    # Ensure string is valid in given charset
    value.encode(charset)
    else:
    # Convert bytestring using given charset
    value = value.decode(charset)
    except UnicodeError as e:
    if mime_encode:
    value = Header(value, 'utf-8', maxlinelen=sys.maxsize).encode()
    else:
    e.reason += ', HTTP response headers must be in %s format' % charset
    raise
    return value

    def __setitem__(self, header, value):
    header = self._convert_to_charset(header, 'ascii')
    value = self._convert_to_charset(value, 'latin-1', mime_encode=True)
    self._headers[header.lower()] = (header, value)

    所以他默认 header 还是空的,只要你不创建 response 实例时候没有初始化 header,所以还是没有解答我的问题,因为一般都是使用 render_to_response 或者 response 直接创建相应的,但是 header 我们一般都没有填充,那到底是哪里填充的(就是在哪里实例化的时候框架帮我们填了 header 我还是没有找到)
    blackeeper
        10
    blackeeper  
       2017-04-28 20:28:18 +08:00   1
    这个是 Django 的中间件自动来帮我们填充的 header,比如说你在 settings 文件没有添加了 DEFAULT_CONTENT_TYPE,也没用在其它地方配置,那么中间件自动就填充。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     928 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 19:41 PVG 03:41 LAX 12:41 JFK 15:41
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86