requests: AttributeError: 'X509' object has no attribute '_x509'

While trying to use Slack API using slacker module that is based on Requests, I receive this SSL-related error:

Python 2.7.12 (default, Oct 11 2016, 05:24:00)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from slacker import Slacker
>>> s=Slacker('<SLACK BOT API KEY>')
>>> s.chat.post_message('#general', 'test')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/slacker/__init__.py", line 287, in post_message
    'icon_emoji': icon_emoji
  File "/usr/local/lib/python2.7/site-packages/slacker/__init__.py", line 71, in post
    return self._request(requests.post, api, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/slacker/__init__.py", line 57, in _request
    **kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 110, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/requests/adapters.py", line 423, in send
    timeout=timeout
  File "/usr/local/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 594, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 350, in _make_request
    self._validate_conn(conn)
  File "/usr/local/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 835, in _validate_conn
    conn.connect()
  File "/usr/local/lib/python2.7/site-packages/requests/packages/urllib3/connection.py", line 330, in connect
    cert = self.sock.getpeercert()
  File "/usr/local/lib/python2.7/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 324, in getpeercert
    'subjectAltName': get_subj_alt_name(x509)
  File "/usr/local/lib/python2.7/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 166, in get_subj_alt_name
    cert = _Certificate(openssl_backend, peer_cert._x509)
AttributeError: 'X509' object has no attribute '_x509'

I see that other users experience this too: http://stackoverflow.com/questions/40628315/python-requests-and-streaming-attributeerror-x509-object-has-no-attribute

Downgrading from 2.12.1 to 2.11.1 removes the issue.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 1
  • Comments: 37 (15 by maintainers)

Most upvoted comments

same thing

pip show requests
Name: requests
Version: 2.12.1
pip show pyopenssl
Name: pyOpenSSL
Version: 16.2.0
pip show cryptography
Name: cryptography
Version: 1.5.3
import pdb
import requests
pdb.run('rs = requests.get( "https://www.mail.ru" )')
> <string>(1)<module>()
(Pdb) continue
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.7/pdb.py", line 1238, in run
    Pdb().run(statement, globals, locals)
  File "/usr/lib64/python2.7/bdb.py", line 400, in run
    exec cmd in globals, locals
  File "<string>", line 1, in <module>
  File "/usr/lib/python2.7/site-packages/requests/api.py", line 70, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python2.7/site-packages/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python2.7/site-packages/requests/adapters.py", line 423, in send
    timeout=timeout
  File "/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 594, in urlopen
    chunked=chunked)
  File "/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 350, in _make_request
    self._validate_conn(conn)
  File "/usr/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 835, in _validate_conn
    conn.connect()
  File "/usr/lib/python2.7/site-packages/requests/packages/urllib3/connection.py", line 330, in connect
    cert = self.sock.getpeercert()
  File "/usr/lib/python2.7/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 324, in getpeercert
    'subjectAltName': get_subj_alt_name(x509)
  File "/usr/lib/python2.7/site-packages/requests/packages/urllib3/contrib/pyopenssl.py", line 166, in get_subj_alt_name
    cert = _Certificate(openssl_backend, peer_cert._x509)
AttributeError: 'X509' object has no attribute '_x509'

Downgrade to 2.11.1 is a fix …

@skob Please please please read the other comments in this thread before posting. I have asked all people reporting to do an investigation and so far no-one has.

Let me be clear: any furthe “me too” posts that don’t acknowledge the investigation I have asked people to do will be summarily deleted.

@larryhq Can you please do the investigation I outlined above?

To anyone else who hits this issue, please don’t just say “me too”. Please investigate this issue as outlined above, because right now none of the maintainers can reproduce the bug.

I have the same problem (even after reinstalling pyopenssl and cryptography).

The problem goes away when I rollback requests to 2.11 and comes back when I upgrade to 2.12 (or the current dev version)

Reinstalling pyopenssl does not help

So it is definitely part of requests (or it dependency description).

I’ve just closed this because it’s now clear that all of these issues stem from having an out-of-date PyOpenSSL in your Python environment. If you encounter this issue, please update your PyOpenSSL to at least 0.14.

I had this issue, and resolved it by doing this. After verifying a clean virtualenv works with only a ‘pip install requests’

sudo yum uninstall python-requests sudo pip uninstall pyopenssl cryptography requests sudo pip install requests