I have the following dictionary:
{'https://www.youtube.com/sw.js_data': ['HTTP/2 200 OK',
'Content-Type: application/json; charset=utf-8',
'X-Content-Type-Options: nosniff',
'Cache-Control: no-cache, no-store, max-age=0, must-revalidate',
'Pragma: no-cache',
'Expires: Mon, 01 Jan 1990 00:00:00 GMT',
'Date: Mon, 14 Mar 2022 17:59:34 GMT',
'Content-Disposition: attachment; filename="response.bin"; filename*=UTF-8\'\'response.bin',
'Strict-Transport-Security: max-age=31536000',
'X-Frame-Options: SAMEORIGIN',
'Cross-Origin-Opener-Policy-Report-Only: same-origin; report-to="ATmXEA_XZXH6CdbrmjUzyTbVgxu22C8KYH7NsxKbRt94"',
'Permissions-Policy: ch-ua-arch=*, ch-ua-bitness=*, ch-ua-full-version=*, ch-ua-full-version-list=*, ch-ua-model=*, ch-ua-platform=*, ch-ua-platform-version=*',
'Accept-Ch: Sec-CH-UA-Arch, Sec-CH-UA-Bitness, Sec-CH-UA-Full-Version, Sec-CH-UA-Full-Version-List, Sec-CH-UA-Model, Sec-CH-UA-Platform, Sec-CH-UA-Platform-Version',
'Server: ESF',
'X-Xss-Protection: 0',
'Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"'],
'https://www.google.com/client_204?&atyp=i&biw=1440&bih=849&dpr=1.5&ei=Z4IvYpTtF5LU9AP1nIOICQ': ['HTTP/2 204 No Content',
'Content-Type: text/html; charset=UTF-8',
'Strict-Transport-Security: max-age=31536000',
"Content-Security-Policy: object-src 'none';base-uri 'self';script-src 'nonce-9KQUw4dRjvKnx/zTrOblTQ==' 'strict-dynamic' 'report-sample' 'unsafe-eval' 'unsafe-inline' https: http:;report-uri https://csp.withgoogle.com/csp/gws/cdt1",
'Bfcache-Opt-In: unload',
'Date: Mon, 14 Mar 2022 17:59:10 GMT',
'Server: gws',
'Content-Length: 0',
'X-Xss-Protection: 0',
'X-Frame-Options: SAMEORIGIN',
'Set-Cookie: 1P_JAR=2022-03-14-17; expires=Wed, 13-Apr-2022 17:59:10 GMT; path=/; domain=.google.com; Secure; SameSite=none',
'Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"']}
I am having a terribly difficult time trying to call and search these keys and values within this dictionary. I am attempting to write a for loop or list comprehension that will search through each value in the dictionary and look for a certain text ("Strict-Transport-Security", for example). After looping through, I would like for it to print the key and then the result of the search. So example desired output for this dictionary would be:
https://www.youtube.com/sw.js_data: Strict-Transport-Security missing
https://www.google.com/client_204?&atyp=i&biw=1440&bih=849&dpr=1.5&ei=Z4IvYpTtF5LU9AP1nIOICQ: Strict-Transport-Security present
Hopefully that makes sense. Assuming it is possible to do but I am having difficulty getting to that point. Thank you!
CodePudding user response:
header = 'Strict-Transport-Security'
for url in mydictionary:
if any(s.startswith(header) for s in mydictionary[url]):
print(f"{header} found for {url}")
else:
print(f"{header} missing for {url}")
CodePudding user response:
I would like to share with you another approach - you could convert your list of strings into dictionary
def parse_list(list_of_str):
res = {}
for element in list_of_str:
try:
x = element.split(":")
res[x[0]] = x[1].strip()
except IndexError:
continue
return res
parsed = {url: parse_list(l) for url, l in your_dict.items()}
Now you can use it as normal dictionary:
for url, subdict in parsed.items():
print(url, "present" if "Strict-Transport-Security" in subdict else "missing")