I was trying to implement handling Last-Modified feature of HTTP using python urllib2 library, so that if server's GET response is not modified since last time it should throw "urllib2.HTTPError: HTTP Error 304: Not Modified". I have implemented the same using below code , but not sure why I am not getting the 304 response.
Also I did research on urllib2.py but not found any detail of status code 304 implementation in this module.
Below is my code and result:
import httplib
import urllib2
httplib.HTTPConnection.debuglevel = 1
request = urllib2.Request('http://www.iitg.ac.in/groff/projects.html')
opener = urllib2.build_opener()
firstdatastream = opener.open(request)
print firstdatastream.headers.dict
request.add_header("If−Modified−Since", firstdatastream.headers.dict['last- modified'])
print "Value of last modified time \n"
var = firstdatastream.headers.dict['last-modified']
print var
seconddatastream = opener.open(request)
print seconddatastream.headers.dict
The second last line of code should raise "urllib2.HTTPError: HTTP Error 304: Not Modified", because of (this line of code ): request.add_header("If−Modified−Since", firstdatastream.headers.dict['last- modified'])
but I am unable to see it on my console result. Below is my result:
{'content-length': '6706', 'accept-ranges': 'none', 'server': 'Apache/2.2.15 (Red Hat)', 'last-modified': 'Thu, 07 May 2015 09:27:08 GMT', 'connection': 'close', 'etag': '"8a0ce0-1a32-5157a83ffe2b7"', 'date': 'Fri, 14 Aug 2015 06:22:02 GMT', 'content-type': 'text/html; charset=UTF-8'} Value of last modified time
Thu, 07 May 2015 09:27:08 GMT
{'content-length': '6706', 'accept-ranges': 'none', 'server': 'Apache/2.2.15 (Red Hat)', 'last-modified': 'Thu, 07 May 2015 09:27:08 GMT', 'connection': 'close', 'etag': '"8a0ce0-1a32-5157a83ffe2b7"', 'date': 'Fri, 14 Aug 2015 06:22:02 GMT', 'content-type': 'text/html; charset=UTF-8'}
Any help will be appreciated. Thanks a lot.