Parse an HTTP request Authorization header with Python

前端 未结 10 547
情书的邮戳
情书的邮戳 2020-12-30 06:47

I need to take a header like this:

 Authorization: Digest qop=\"chap\",
     realm=\"testrealm@host.com\",
     username=\"Foobear\",
     response=\"6629fae         


        
10条回答
  •  感情败类
    2020-12-30 07:23

    You can also use urllib2 as CheryPy does.

    here is the snippet:

    input= """
     Authorization: Digest qop="chap",
         realm="testrealm@host.com",
         username="Foobear",
         response="6629fae49393a05397450978507c4ef1",
         cnonce="5ccc069c403ebaf9f0171e9517f40e41"
    """
    import urllib2
    field, sep, value = input.partition("Authorization: Digest ")
    if value:
        items = urllib2.parse_http_list(value)
        opts = urllib2.parse_keqv_list(items)
        opts['protocol'] = 'Digest'
        print opts
    

    it outputs:

    {'username': 'Foobear', 'protocol': 'Digest', 'qop': 'chap', 'cnonce': '5ccc069c403ebaf9f0171e9517f40e41', 'realm': 'testrealm@host.com', 'response': '6629fae49393a05397450978507c4ef1'}
    

提交回复
热议问题