tags:

views:

52

answers:

1

For the openers, opener = urllib2.build_opener(), if I try to add an header:

request.add_header('if-modified-since',request.headers.get('last-nodified'))

I get the error code:

Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    feeddata = opener.open(request)
  File "C:\Python27\lib\urllib2.py", line 391, in open
    response = self._open(req, data)
  File "C:\Python27\lib\urllib2.py", line 409, in _open
    '_open', req)
  File "C:\Python27\lib\urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 1173, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "C:\Python27\lib\urllib2.py", line 1142, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "C:\Python27\lib\httplib.py", line 946, in request
    self._send_request(method, url, body, headers)
  File "C:\Python27\lib\httplib.py", line 986, in _send_request
    self.putheader(hdr, value)
  File "C:\Python27\lib\httplib.py", line 924, in putheader
    str = '%s: %s' % (header, '\r\n\t'.join(values))
TypeError: sequence item 0: expected string, NoneType found

How do you get around this?

I tried building a class from urllib2.BaseHandler and it doesn't work.

A: 

Your traceback says: expected string, NoneType found from which I deduce that you've stored a None value as a header. Did you really write 'last-nodified'? The header you mean was probably 'last-modified', but even then you should check that it existed and not re-use it as a header if request.headers.get() returns None.

Duncan