Question

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.

Was it helpful?

Solution

conn.headers is a httplib.HTTPMessage, which is a subclass of rfc822.Message and thus has a (limited) dict-like interface. You can use the 2-argument call to get to supply a default value.

contentLen = conn.headers.get('content-length','NULL')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top