3

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.

singhabsk
  • 31
  • 3

1 Answers1

0

I also met this problem,My solution is copying from Google browser to in the network of the request headers, headers = { 'Accept': 'application/json, text/javascript, /; q=0.01', 'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'zh-CN,zh;q=0.8', 'Connection': 'keep-alive', 'Host': 'stockpage.10jqka.com.cn', 'Referer': 'http://stockpage.10jqka.com.cn/000001/bonus/', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36', 'X-Requested-With': 'XMLHttpRequest' } Method of use, request = urllib2.Request(url, headers=headers) response = urllib2.urlopen(request)