【问题标题】:Why does wsgiref.simple_server report content-type of request as 'text/plain' while none was sent?为什么 wsgiref.simple_server 将请求的内容类型报告为“文本/纯文本”,而没有发送?
【发布时间】:2011-08-20 22:00:54
【问题描述】:

client.py 的输出是 text/plain,尽管没有向服务器发送内容类型标头。
为什么?

# ---------------------------------------------- server.py
from wsgiref.simple_server import make_server

def simple_app(environ, start_response):
    print environ.get('CONTENT_TYPE')
    start_response('200 OK', [])
    return []

make_server('localhost', 88, simple_app).serve_forever()

# ---------------------------------------------- client.py
import subprocess
import urllib2

p = subprocess.Popen(['python', '-u', 'server.py'])
req = urllib2.Request('http://localhost:88', headers={})
assert not req.has_header('content-type')
urllib2.urlopen(req).read()
p.terminate()

【问题讨论】:

    标签: python content-type wsgi wsgiref


    【解决方案1】:

    text/plain 类型是 MIME 的默认类型,在 section 5.2 Content Type Defaults 中。

    WSGI simple_server 使用 BaseHTTPRequestHandler 反过来使用 mimetools.Message 类来解析客户端发送的标头——这里是它设置默认值的地方:

    # mimetools.py
    
    class Message(rfc822.Message):
    
        def parsetype(self):
            str = self.typeheader
            if str is None:
                str = 'text/plain'
    

    【讨论】:

      猜你喜欢
      • 2018-01-13
      • 1970-01-01
      • 1970-01-01
      • 2016-09-27
      • 2018-11-27
      • 2018-05-11
      • 2011-12-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多