PythonCharmers / python-future

Easy, clean, reliable Python 2/3 compatibility
http://python-future.org
MIT License
1.17k stars 291 forks source link

Using build_opener with cookiejar thows AttributeError: 'HTTPMessage' object has no attribute 'getheaders' #546

Open bonham opened 4 years ago

bonham commented 4 years ago
#!/usr/bin/env python

from future import standard_library
standard_library.install_aliases()
import urllib.request
import http.cookiejar

def main():

    cj = http.cookiejar.CookieJar()
    opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))

    url = "https://www.google.com"
    opener.open(url)

main()

Stack Trace:

Traceback (most recent call last):
  File "cookie.py", line 18, in <module>
    main()
  File "cookie.py", line 15, in main
    opener.open(url)
  File "/usr/local/lib/python2.7/dist-packages/future/backports/urllib/request.py", line 506, in open
    response = meth(req, response)
  File "/usr/local/lib/python2.7/dist-packages/future/backports/urllib/request.py", line 1350, in http_response
    self.cookiejar.extract_cookies(response, request)
  File "/usr/lib/python2.7/cookielib.py", line 1667, in extract_cookies
    for cookie in self.make_cookies(response, request):
  File "/usr/lib/python2.7/cookielib.py", line 1584, in make_cookies
    rfc2965_hdrs = headers.getheaders("Set-Cookie2")
AttributeError: 'HTTPMessage' object has no attribute 'getheaders'

Tested on debian 10.2 in docker

bhenry commented 2 years ago

did you find a workaround? yes i see this is old. if i should be somewhere else, then set me in the right direction.

keren-meron commented 2 years ago

You need to use cookiejar from future instead, like this: import future.backports.http.cookiejar as http_cookiejar

I got this AttributeError as well, and I realized the error was that I was instantiating my HTTPCookieProcessor with a http.cookiejar.CookieJar object (the default is future.backports.http.cookiejar). To fix it, I switched from http.cookiejar to future.backports.http.cookiejar.

i.e. old code:

from future import standard_library
standard_library.install_aliases()
import urllib.request
import http.cookiejar

http_cookie_processor = urllib.request.HTTPCookieProcessor(http.cookiejar.CookieJar())
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))

new code:

from future import standard_library
standard_library.install_aliases()
import urllib.request
import future.backports.http.cookiejar as http_cookiejar

http_cookie_processor = urllib.request.HTTPCookieProcessor(http_cookiejar.CookieJar())
opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))