Parse raw HTTP Headers

前端 未结 7 1025

I have a string of raw HTTP and I would like to represent the fields in an object. Is there any way to parse the individual headers from an HTTP string?

\'GE         


        
7条回答
  •  一个人的身影
    2020-11-27 05:02

    mimetools has been deprecated since Python 2.3 and totally removed from Python 3 (link).

    Here is how you should do in Python 3:

    import email
    import io
    import pprint
    
    # […]
    
    request_line, headers_alone = request_text.split('\r\n', 1)
    message = email.message_from_file(io.StringIO(headers_alone))
    headers = dict(message.items())
    pprint.pprint(headers, width=160)
    

提交回复
热议问题