I have a script which “appeared” to work in our test environment, as there was lack of visibility. However we are now testing in our pre-prod environment we have noticed the following issue…
The basic function of the script is to perform HTTP/URL requests on the host web front end and return the status code and also the content size (i.e. content-length). I am having trouble working out, how to do a check for the content-lenght before assigning it to a variable. At the moment I have the following:
#... Code ...
try:
conn = urllib2.urlopen(line, timeout=1)
contentLen = conn.headers['content-length']
#... Code ...
However for URLs that lack this content-length it simply breaks the script if content length is missing. Rather than raising an exception I would like to simply check for it’s existance before assigning contentLen and then if it exists assign the actual content-length value, if it doesn’t exist then assign the value “NULL”. So this can then be printed as “Null“.
Thanks in advance.
conn.headersis ahttplib.HTTPMessage, which is a subclass ofrfc822.Messageand thus has a (limited)dict-like interface. You can use the 2-argument call togetto supply a default value.