Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python dictionary key error cannot resolve

I am performing status check across a huge list of links and my snippet is as follows:

link = 'http://xyz'
proxyDict = { "http" : "ip:80", "https" : "https://ip:443"}
r = requests.get(link, allow_redirects=False, verify=False)
http_status = r.status_code
print (r.headers)

# check the status and react accordingly

if http_status == 200 and r.headers['content-length'] == "0":
   print ('Link Alive - NO content'+';'+str(http_status)+';'+link, file = log)
elif http_status == 200 and "text/html" in r.headers['content-type']:
   print ('External- direct HTML link'+';'+str(http_status)+';'+link, file = log)  
elif http_status == 200 and "application" in r.headers['content-type']:
   print ('External- direct HTML link'+';'+str(http_status)+';'+link, file = log)

When I execute the code I get the following error:

    return self._store[key.lower()][1]
    KeyError: 'content-length'

The header output is as follows:

CaseInsensitiveDict({'status': '200', path=/; HttpOnly, shpuvid=rBBcnFJUTliSHV+hA5lLAg==; expires=Thu, 08-Oct-15 18:26:32 GMT;'connection': 'keep-alive', 'cache-control': 'max-age=0, private, must-revalidate', 'date': 'Tue, 08 Oct 2013 18:26:32 GMT', 'content-type': 'text/html; charset=utf-8', 'x-rack-cache': 'miss'})

I know that the error exists because the header output has no key "content-length", but when if condition does not satisfy it has to jump to next elif condition which does not happen, rather stops the code execution throwing the above error.

Any suggestions? Might be a silly question but a good thing for a beginner to learn.

like image 720
Sangamesh Hs Avatar asked Dec 12 '22 10:12

Sangamesh Hs


2 Answers

Instead of using the bracket notation, use r.headers.get('content-length') from the dictionary which will not throw the key error but simply return None.

Its nice that you can use either notation to retrieve values from a dictionary. Many times you want that key error to be thrown as not to let a problem go unnoticed. In this case, it appears that dictionary.get() is what you want.

like image 53
bcollins Avatar answered Dec 24 '22 03:12

bcollins


Key error generally means the key doesn't exist.

I'm gessing that self._store[key.lower()][1] is not valid (doesn't exist)

From official python docs:

exception KeyError

Raised when a mapping (dictionary) key is not found in the set of existing keys.

like image 30
Kobi K Avatar answered Dec 24 '22 05:12

Kobi K