0
0 комментариев

Сохраняет ли http.client.HTTPMessage и его аналог в python 2 порядок ключей? Вот такой код (ideone):

from http.server import BaseHTTPRequestHandler
from io import BytesIO
 
class HTTPRequest(BaseHTTPRequestHandler):
    def __init__(self, request_text):
        self.rfile = BytesIO(request_text)
        self.raw_requestline = self.rfile.readline()
        self.error_code = self.error_message = None
        self.parse_request()
 
    def send_error(self, code, message):
        self.error_code = code
        self.error_message = message
 
 
request_text = (
    b'GET /who/ken/trust.html HTTP/1.1\r\n'
    b'Host: cm.bell-labs.com\r\n'
    b'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3\r\n'
    b'Accept: text/html;q=0.9,text/plain\r\n'
    b'\r\n'
    )
 
request = HTTPRequest(request_text)
 
print( "Error code:", request.error_code )      # None  (check this first)
print( "request.command", request.command )         # "GET"
print( "request.path", request.path )            # "/who/ken/trust.html"
print( "request.request_version", request.request_version ) # "HTTP/1.1"
print( "len(request.headers)", len(request.headers) )    # 3
print( "request.headers.keys()",request.headers.keys() )  # ['accept-charset', 'host', 'accept']
print( "request.headers['host']", request.headers['host'] ) # "cm.bell-labs.com"
print( "type(request.headers)", type(request.headers) )


Добавить комментарий