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.

What version of PyOpenSSL do you have installed?


Also could you include the version of cryptography you have installed?


```
Name: pyOpenSSL
Version: 16.2.0
Summary: Python wrapper module around the OpenSSL library
Home-page: https://pyopenssl.readthedocs.io/
Author: Hynek Schlawack
Author-email: hs@ox.cx
License: Apache License, Version 2.0
Location: /usr/local/lib/python2.7/site-packages
Requires: six, cryptography
---
Name: cryptography
Version: 1.5.3
Summary: cryptography is a package which provides cryptographic recipes and primitives to Python developers.
Home-page: https://github.com/pyca/cryptography
Author: The cryptography developers
Author-email: cryptography-dev@python.org
License: BSD or Apache License, Version 2.0
Location: /usr/local/lib/python2.7/site-packages
Requires: cffi, six, pyasn1, idna, enum34, setuptools, ipaddress
```


Hrm, I have both of those modules at those versions as well but cannot reproduce this error. I don't see how the object can have _no attribute_ `_x509`: there is no codepath where `_x509` doesn't get patched onto the object that I can see, and that code has remained unchanged since 2013.


Can you try reinstalling pyopenssl and cryptography for me please?


I did it in the morning :)

Reinstalled those and seen the same error after upgrading Requests.


@sapran Ok, in that case I'll need you to try to run this under pdb and investigate why there appears to be no `_x509` attribute on that X509 object. In particular it would be good to check it on line `310` of `requests.packages.urllib3.contrib.pyopenssl`, to see what the return value of `get_peer_certificate` was and to investigate it there.


When I update requests to version 2.12.1, I has the same issue also.


@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.


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.


@Lukasa I had the same error, read your comments, reinstalled both libraries, pyopenssl and cryptography, and it worked. no more error. Thanks for your help. :) 


No problem. =) I'd like to make @reaperhulk aware of this issue at this time, as this seems to be related to pyopenssl and cryptography in a way I don't fully understand yet.


I'm wondering if somehow Python is loading the wrong version of pyOpenSSL for people who are affected by this. the `X509` object most definitely has `_x509` (https://github.com/pyca/pyopenssl/blob/master/src/OpenSSL/crypto.py#L997-L1004) and has had it since 0.14 was released almost 3 years ago (https://github.com/pyca/pyopenssl/blob/0146d447e63a737b4f05a1164d5950eff68543e7/OpenSSL/crypto.py#L748-L752).

I had the same issue on a new MBP with a 'vanilla' macOS Sierra.  @Lukasa I uninstalled and re-installed pyOpenSSL and Cryptography and it all worked fine.  Thanks.

I had the same problem. And upgrading pyOpenSSL by 'pip install --upgrade pyOpenSSL' didn't solve the problem.

I suspect that the reason is that there is another pyOpenSSL installation in the system, for my case, I have pyOpenSSL-0.13.1-3.el7.x86_64 installed by 'yum'. So I think if you use a CentOS 7 system and install pyOpenSSL-0.13.1-3.el7.x86_64 by yum, then install requests 2.12.1 by pip, then you might probably reproduce the problem.

the conflict between yum and pip is annoying.

So can people also let us know the result of running `python -c 'import OpenSSL; print(OpenSSL.__version__)'` I suspect this is exactly what @reaperhulk has suggested and what @zhang-jingwang seems to have confirmed.

On my OS X machine:
```
$ python -c 'import OpenSSL; print(OpenSSL.__version__)'
0.13.1
$ python --version
Python 2.7.10
$ sw_vers
ProductName:	Mac OS X
ProductVersion:	10.11.5
BuildVersion:	15F34
```

Yup, that's too old. Our setup.py file requires at least 0.14

Hey guys I had today the same error, using RHEL 7.3 and pyOpenSSL 0.13.1-3.el7. Uninstalling and then installing again pyOpenSSL and cryptography via pip works for me as well. Thanks for the info!

Same error.
requests==2.12.1
cryptography==1.5
pyOpenSSL not installed

@jachymb PyOpenSSL *cannot* be not installed. Please open your Python shell and type `import OpenSSL`.

I am sorry. It is pyOpenSSL=0.13.1

Ok, that's too old. Our setup.py file requires at least 0.14.

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.

```
$ python -c 'import OpenSSL; print(OpenSSL.__version__)'
16.2.0
```
Still broken…

@sapran Please reinstall PyOpenSSL and its dependencies, and you will find it is not, in fact, broken.

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).

@rjungbeck I cannot stress this enough: this problem is *not* part of Requests.

Try a brand new virtual environment. Please also verify in your current environment that PyOpenSSL is the version you think it is. We do not support PyOpenSSL 0.13 any longer.

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