Parse an HTTP request Authorization header with Python

前端 未结 10 570
情书的邮戳
情书的邮戳 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:46

    A little regex:

    import re
    reg=re.compile('(\w+)[:=] ?"?(\w+)"?')
    
    >>>dict(reg.findall(headers))
    
    {'username': 'Foobear', 'realm': 'testrealm', 'qop': 'chap', 'cnonce': '5ccc069c403ebaf9f0171e9517f40e41', 'response': '6629fae49393a05397450978507c4ef1', 'Authorization': 'Digest'}
    

提交回复
热议问题