LoginSignup
47
50

More than 5 years have passed since last update.

Python実行時にInsecurePlatformWarningが出る場合の対応方法

Last updated at Posted at 2015-03-21

2016/01/12更新

  • 'requests[security]'について追記。
  • Software Collectionsについて追記。

はじめに

Pythonを新規導入したりパッケージを更新した後に、Python実行時に以下のような警告が出るようになってしまって、あれれーって思うことはないでしょうか?(私はSoftLayerのPythonパッケージを更新して久しぶりにコマンドを実行しようとするとこのような警告が出てびっくりしてしまいました)。この記事では、このInsecurePlatformWarningという警告文が出ないようにする方法を紹介します。

# python test1.py
/usr/lib/python2.6/site-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
(以下略)

# sl vs list
/usr/lib/python2.6/site-packages/requests/packages/urllib3/util/ssl_.py:79: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning
(以下略)

解決方法1. requestsパッケージを2.5.3以前に戻す。

下記リンクから分かるとおり、requests-2.6.0以降ではこの警告文が出るように実装されています。よってシンプルな解決策としては、pipでrequestsをバージョン指定で2.5.3に戻すというのが考えられます。
http://fossies.org/diffs/requests/2.5.3_vs_2.6.0/requests/packages/urllib3/util/ssl_.py-diff.html

# pip list | grep requests
requests (2.6.2)
# pip install requests==2.5.3
# pip list | grep requests
requests (2.5.3)

ちなみに、パッケージを新規に導入したりupgradeをしようとすると、せっかく据え置きしていたrequestsも同時に更新されてしまうことがあります(また後でdowngradeすればいいんですけどね。)もしrequests 2.5.3で据え置きにしたまま新規導入、パッケージ更新を一発で仕上げたい場合は、以下のように実施すればよいでしょう。

SoftLayerのパッケージを新規に導入する際の例
# pip install SoftLayer requests==2.5.3
SoftLayerのパッケージを更新する際の例
# pip install --upgrade SoftLayer requests==2.5.3

解決方法2. requests[security]を導入する。

requests[security]の導入
# yum install -y openssl-devel python-devel libffi-devel
# pip install 'requests[security]'

解決方法3. Pythonのバージョンを2.7.9以降にする。

Pythonのバージョンを2.7.9以降にすれば、この警告は出なくなります。ただし、OSによってはシステムレベルでPythonが使われており、例えばCentOSやRHELのyumなどではPythonで実装されています。そのため、システムレベルでPythonのバージョンを変更することはあまり望ましくないと個人的には思います。

新しめのSoftware Packageと共存させるために、CentOSやRHEL系ではSoftware Collectionsというコンポーネントが提供されています。こちらを使ってPython 2.7やPython 3.3を導入してもよいでしょう。
http://qiita.com/murachi1208/items/202bff84964188619f57
http://www.idcf.jp/blog/cloud/software-collections-for-centos-6/

現実的には、利用者レベルで適宜バージョンを切り替えることができるpyenvやvirtualenvなどを利用するのが良い気がします。個人的には非常に重宝しています。Pythonを個別にインストール+コンパイルするのは結構面倒ですし、色々なバージョンで試したいこともあるので。使い方は・・・Qiita含めてググればいくらでも資料があるようなので今回は省略。
https://github.com/yyuu/pyenv-virtualenv
http://qiita.com/yuta_h3/items/2988c4d0811bf8c344c0
http://qiita.com/search?page=2&q=pyenv&sort=rel&utf8=%E2%9C%93

参考リンク

https://urllib3.readthedocs.org/en/latest/security.html?highlight=insecureplatformwarning
http://stackoverflow.com/questions/29134512/insecureplatformwarning-a-true-sslcontext-object-is-not-available-this-prevent

47
50
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
47
50