为什么在未发送请求的情况下,wsgiref.simple_server将请求的内容类型报告为“文本/纯文本”? [英] Why does wsgiref.simple_server report content-type of request as 'text/plain' while none was sent?

查看:210
本文介绍了为什么在未发送请求的情况下,wsgiref.simple_server将请求的内容类型报告为“文本/纯文本”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

client.py的输出为 text / plain ,尽管没有内容类型头发送到服务器。

为什么?

Output from client.py is text/plain although no content-type header was sent to the server.
Why?

# ---------------------------------------------- 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()


推荐答案

text / plain 是MIME的默认值。 rel = nofollow> 节5.2内容类型默认值

The type text/plain is the default for MIME, in section 5.2 Content Type Defaults.

WSGI simple_server 使用 BaseHTTPRequestHandler 依次使用 mimetools.Message 类来解析客户端发送的标头-在此处设置默认值:

The WSGI simple_server uses BaseHTTPRequestHandler which in turn uses a mimetools.Message class to parse the headers sent by the client -- here is where it sets the default:

# mimetools.py

class Message(rfc822.Message):

    def parsetype(self):
        str = self.typeheader
        if str is None:
            str = 'text/plain'

这篇关于为什么在未发送请求的情况下,wsgiref.simple_server将请求的内容类型报告为“文本/纯文本”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆