---
slug: "Curl,Python,PHPでHTTPS接続する際固まるサイトがあるので、TLS1.2を使わないようにする"
title: "Curl, Python, PHP で HTTPS 接続する際固まるサイトがあるので、TLS1.2 を使わないようにする"
description: "\n\n\nこの記事 http://b.ytyng.com/a-61/ で書きましたが、OpenSSL 1.0.1f で https 接続した場合、TLS1.2 で 接続しようとして hello を発行した時、特定のサイトが応答で固まることがあります。"
url: "https://www.ytyng.com/blog/Curl,Python,PHPでHTTPS接続する際固まるサイトがあるので、TLS1.2を使わないようにする"
publish_date: "2015-06-11T06:28:25Z"
created: "2015-06-11T06:28:25Z"
updated: "2026-02-27T10:42:57.347Z"
categories: ["Linux"]
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20230812/4e34fb5a2245414fad95d1a2ca59654e.png.webp?width=768"
has_video: false
has_music: false
video_urls: []
music_urls: []
lang: "ja"
---

# Curl, Python, PHP で HTTPS 接続する際固まるサイトがあるので、TLS1.2 を使わないようにする

<div class="document">


<p>この記事 <a class="reference external" href="http://b.ytyng.com/a-61/">http://b.ytyng.com/a-61/</a> で書きましたが、OpenSSL 1.0.1f で https 接続した場合、TLS1.2 で 接続しようとして hello を発行した時、特定のサイトが応答で固まることがあります。</p>
<p>(OpenSSL 1.0.1f は、ubuntu 14.04, 15.04 などの最新のデフォルトバージョンです。1.0.1g を使っていれば解決されているかもしれません)</p>
<p>その場合、TLS1.2 を使わないように接続することで、回避することができます。</p>
<p>curl, Python, PHP で、TLS1.2 を使わない (TLS1.0を強制する) 方法です。</p>
<div class="section" id="curl">
<h3>curl</h3>
<pre class="literal-block">$ curl -vv "https://hoge.example.com/"  # URLは仮
* Hostname was NOT found in DNS cache
*   Trying xxx.xxx.xxx.xxx...
* Connected to hoge.example.com (xxx.xxx.xxx.xxx) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSLv3, TLS handshake, Client hello (1):

ここで固まる
</pre>
<p>↓</p>
<p>TLS1.2 を使わない (TLS1.0を強制)</p>
<pre class="literal-block">$ curl -vv "https://hoge.example.com/" --tlsv1.0

固まらない!
</pre>
</div>
<div class="section" id="python3-4-requests">
<h3>python3.4 + requests</h3>
<pre class="literal-block"># import requests
r = requests.get('https://hoge.example.com/')
固まる!
</pre>
<p>↓ TLS1.2 を使わない (TLS1.0を強制)</p>
<pre class="literal-block">import requests

from requests.adapters import HTTPAdapter
from requests.packages.urllib3.poolmanager import PoolManager
import ssl

class MyAdapter(HTTPAdapter):
    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = PoolManager(
            num_pools=connections,
            maxsize=maxsize,
            block=block,
            ssl_version=ssl.PROTOCOL_TLSv1)

s = requests.Session()
s.mount('https://', MyAdapter())

r = s.get("https://hoge.example.com/")
固まらない!
</pre>
<p>参考:</p>
<p>Choosing The SSL Version In Python Requests • Lukasa's Echochamber
<a class="reference external" href="https://lukasa.co.uk/2013/01/Choosing_SSL_Version_In_Requests/">https://lukasa.co.uk/2013/01/Choosing_SSL_Version_In_Requests/</a></p>
</div>
<div class="section" id="php5-6">
<h3>PHP5.6</h3>
<pre class="literal-block">$html = file_get_contents('https://hoge.example.com/');
固まる!
</pre>
<p>↓ TLS1.2 を使わない (TLS1.0を強制)</p>
<pre class="literal-block">$ctx = stream_context_create([
    'ssl' =&gt; [
        'crypto_method' =&gt; STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT,
    ],
]);
$html = file_get_contents('https://hoge.example.com/', false, $ctx);
固まらない!
</pre>
<p>参考:</p>
<p>PHP: PHP 5.6.x における OpenSSL 関連の変更 - Manual
<a class="reference external" href="http://php.net/manual/ja/migration56.openssl.php">http://php.net/manual/ja/migration56.openssl.php</a></p>
</div>
</div>
