0

I am running python to extract value of keys from a long string returned from a http response header as below:

"csrftoken=##10882d8a32354e7e7e518beebf1d531d18788899; Secure; Path=/, session=75bcede9f0192ed2_619d5819.Qa4dv5iN0lHFkLWcdcDTzjvaabb; Secure; HttpOnly; Path=/"

I would like extract '##...8899' (value of csrftoken) and '75...aabb' (value of session). Is there any python method or smart way to easily extract those values?

  • Using RegEx lookaheads and lookbehinds might help. In this case, you would likely do one between `csrftoken=##` and the next `;` and then for the session, `session=` and `;`. While I am unable to implement this myself, https://www.regular-expressions.info/lookaround.html and https://stackoverflow.com/questions/1454913/regular-expression-to-find-a-string-included-between-two-characters-while-exclud might help – Larry the Llama Nov 23 '21 at 21:33
  • 2
    I would first split the string on `;`, then loop through that and split each on `=`. – Barmar Nov 23 '21 at 21:34
  • 1
    What is the source of this string? – juanpa.arrivillaga Nov 23 '21 at 21:54
  • looks like weirdly written cookies. with requests it's just `response.cookies`, but whatever library you're using you should have something more robust than parsing raw cookies – diggusbickus Nov 23 '21 at 22:17

1 Answers1

0
def my_parser(s):
    return { (z:=x.split("="))[0] : z[1] if len(z)>1 else "" for x in s.replace(" ", "").split(";") }

this function returns the dict as you want. deleting white spaces is probably not necessary, only if your real input actually has white spaces

You use it passing the string you want to parse.

output

{'csrftoken': '##10882d8a32354e7e7e518beebf1d531d18788899', 'Secure': '', 'Path': '/', 'HttpOnly': ''}
mnzbono
  • 131
  • 1
  • 6