SSL23_GET_SERVER_HELLO: tlsv1 alert internal error (SNI) in Python 2.7.6

SSL23_GET_SERVER_HELLO: tlsv1 alert internal error (SNI) in Python 2.7.6

會發生在 Python 2.7.6 連 CloudFlare HTTPS 的時候
似乎是因為 2.7.6 不支援 Server-Name-Indication (SNI)
Python 2.7.9 之後才支援

相關錯誤:

SSL routines: SSL23_GET_SERVER_HELLO: tlsv1 alert internal error
SSLError: bad handshake: SysCallError(32, 'EPIPE')

解決辦法:

$ sudo apt-get install libffi-dev libssl-dev
$ pip install -U urllib3[secure] ndg-httpsclient pyasn1

ref:
https://stackoverflow.com/questions/18578439/using-requests-with-tls-doesnt-give-sni-support/
http://docs.python-requests.org/en/master/community/faq/
https://github.com/kennethreitz/requests/issues/749
https://github.com/kennethreitz/requests/pull/1347

Setup locale in Ubuntu

$ locale-gen en_US.UTF-8

# displaying the available locales
$ locale -a

$ locale

ref:
https://www.thomas-krenn.com/en/wiki/Configure_Locales_in_Ubuntu

解法:

$ update-locale LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 LC_ALL=en_US.UTF-8

or

in /etc/default/locale

LANG=en_US.UTF-8
LC_CTYPE=en_US.UTF-8
LC_ALL=en_US.UTF-8

or

in ~/.bashrc

export LANG=en_US.UTF-8
export LANGUAGE=
export LC_CTYPE=en_US.UTF-8
export LC_NUMERIC=en_US.UTF-8
export LC_TIME=en_US.UTF-8
export LC_COLLATE=en_US.UTF-8
export LC_MONETARY=en_US.UTF-8
export LC_MESSAGES=en_US.UTF-8
export LC_PAPER=en_US.UTF-8
export LC_NAME=en_US.UTF-8
export LC_ADDRESS=en_US.UTF-8
export LC_TELEPHONE=en_US.UTF-8
export LC_MEASUREMENT=en_US.UTF-8
export LC_IDENTIFICATION=en_US.UTF-8
export LC_ALL=

如果你使用 upstart 來啟動你的服務
記得也要在 config 裡用 env 指定 locale 參數