2011-05-07 29 views
0

client.py的輸出是text/plain,但沒有將內容類型標頭髮送到服務器。
爲什麼?爲什麼wsgiref.simple_server在沒有發送任何內容時將內容類型的請求報告爲「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() 

回答

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' 
相關問題