Why does wsgiref.simple_server report the content type of the request as "text / plain" until no one was sent?

The client.py output file is equal text/plain, although a content type header was not 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()
0
source share
1 answer

The type text/plainis the default for MIME, in section 5.2 The default content type .

WSGI simple_serveruses it BaseHTTPRequestHandler, which in turn uses the class mimetools.Messageto parse the headers sent by the client - here it sets the default value:

# mimetools.py

class Message(rfc822.Message):

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

Source: https://habr.com/ru/post/1704582/


All Articles