LoginSignup
0
0

More than 5 years have passed since last update.

PythonのrequestsライブラリでWarningが出る場合の強引な対応方法

Posted at

Pythonのrequestsのライブラリは非常に便利なのでよく使うのだが、
実行時に何かとwarningがでてウザい。

例えばInsecurePlatformWarningなんかが出る。
ググッてこちらの方を参考に対応しようと思ったが、パッケージを入れたりダウングレードするのはどうにも面倒。

なので、強引にソースコードを書き換えることにした。

書き換え方は簡単。

私の場合はこんなワーニングが出たが、

/usr/lib/python2.6/site-packages/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#insecureplatformwarning.
  InsecurePlatformWarning

ご丁寧にソースコードのpathが書いてあるので、それをvimか何かで開いて、

vim /usr/lib/python2.6/site-packages/requests/packages/urllib3/util/ssl_.py

以下のようにwarnings.warnもろともコメントアウトしてあげればOK

        def wrap_socket(self, socket, server_hostname=None, server_side=False):
            #warnings.warn(
            #    'A true SSLContext object is not available. This prevents '
            #    'urllib3 from configuring SSL appropriately and may cause '
            #    'certain SSL connections to fail. You can upgrade to a newer '
            #    'version of Python to solve this. For more information, see '
            #    'https://urllib3.readthedocs.io/en/latest/security.html'
            #    '#insecureplatformwarning.',
            #    InsecurePlatformWarning
            #)
            kwargs = {
                'keyfile': self.keyfile,
                'certfile': self.certfile,
                'ca_certs': self.ca_certs,
                'cert_reqs': self.verify_mode,
                'ssl_version': self.protocol,
                'server_side': server_side,
            }

これで十分じゃね!?w

0
0
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
0
0