{"id":91,"date":"2016-11-05T14:32:13","date_gmt":"2016-11-05T14:32:13","guid":{"rendered":"http:\/\/www.luo666.com\/?p=91"},"modified":"2019-01-19T04:48:54","modified_gmt":"2019-01-19T04:48:54","slug":"requests-exceptions-sslerror-when-running-a-py2exe-generated-exe-file","status":"publish","type":"post","link":"http:\/\/www.luo666.com\/?p=91","title":{"rendered":"requests.exceptions.SSLError when running a py2exe generated EXE file"},"content":{"rendered":"<p>\u6700\u8fd1\u505a\u4e2a\u5174\u8da3\u5c0f\u9879\u76ee\uff0c\u9700\u8981\u7528python\u6765get\/post https\u8bf7\u6c42\uff0c\u7528\u5230\u4e86requests\u6a21\u5757\uff0c\u56e0\u4e3a\u8981\u5728windows\u4e0b\u4f7f\u7528\uff0c\u8003\u8651\u5230\u6613\u7528\u6027\u548c\u5c0f\u767d\u73a9\u5bb6\u7684\u9700\u6c42\uff0c\u4f7f\u7528py2exe\u751f\u6210exe\u3002<a href=\"http:\/\/www.jb51.net\/article\/57652.htm\">\u53c2\u8003\u94fe\u63a5<\/a>\u770b\u8d77\u6765\u5f88\u7b80\u5355\uff0c\u7edf\u5171\u5206\u4e09\u6b65\uff1a1\uff0c\u5b89\u88c5py2exe\uff1b2\uff0c\u51c6\u5907\u4e00\u4e2a\u914d\u7f6e\u811a\u672c\uff0c\u6211\u4eec\u53eb\u4ed6build_exe.py\uff1b3\uff0c\u6267\u884c\u4e00\u6761\u547d\u4ee4python build_exe.py py2exe<\/p>\n<p>\u5148\u628a\u8fd9\u4e2a\u7b80\u5355\u7684\u521d\u7ea7\u7248\u672c\u653e\u8fd9\u91cc\uff1a<\/p>\n<p style=\"padding-left: 30px;\">#usage: python build_exe.py py2exe<\/p>\n<p style=\"padding-left: 30px;\">from distutils.core import setup<br \/>\nimport py2exe<\/p>\n<p style=\"padding-left: 30px;\">setup(console=[&#8216;myqqbot.py&#8217;])<\/p>\n<p>\u4f46\u662f\u8fd9\u91cc\u5176\u5b9e\u8fd8\u662f\u6709\u5751\u7684\uff0c\u5b89\u88c5py2exe\u8981\u6ce8\u610f\uff0c\u5e76\u4e0d\u662f\u76f4\u63a5pip install py2exe\u5c31\u53ef\u4ee5\u4e86\uff0cpy2exe\u662f\u4eba\u5bb6python3\u4e13\u7528\u7684\uff0cpython2\u7684\u662fpy2exe_py2\uff0c\u60b2\u5267\u5427\uff0c\u6240\u4ee5\u88c5\u5305\u524d\u6700\u597d\u8fd8\u662fpip search\u4e0b\uff0c\u514d\u5f97\u6d6a\u8d39\u65f6\u95f4\u8fd8\u4e0d\u77e5\u9053\u9519\u5728\u54ea\u3002\u914d\u7f6e\u811a\u672c\u8c8c\u4f3c\u7b80\u5355\uff0c\u5927\u5bb6\u5e94\u8be5\u80fd\u60f3\u5230\u8fd9\u91cc\u9762\u5176\u5b9e\u662f\u6709\u5f88\u591a\u73a9\u6cd5\u7684\uff0c\u4ee5\u81f3\u4e8e\u9700\u8981\u4e00\u4e2a\u811a\u672c\u800c\u4e0d\u662f\u76f4\u63a5\u4f20\u53c2\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u751f\u6210\u8fc7\u7a0b\u5f88\u987a\u5229\uff0c\u4e0d\u8fc7\u51e0\u79d2exe\u6587\u4ef6\u5df2\u7ecf\u8eba\u5728dist\u76ee\u5f55\u4e0b\u4e86\u3002\u4f46\u662f\u53cc\u51fb\u4ed6\uff0cconsole\u4e00\u95ea\u800c\u8fc7\uff0c\u90fd\u4e0d\u77e5\u9053\u53d1\u751f\u4e86\u4ec0\u4e48\u3002Windows\u5c31\u662f\u8fd9\u4e48\u53d8\u6001\uff0c\u53cd\u6b63\u628a\u4f60\u5f53\u5c0f\u767d\uff0c\u4e0d\u7ed9\u4f60\u4efb\u4f55\u9519\u8bef\u4fe1\u606f\u3002\u5728\u8d85\u7ea7\u96be\u7528\u7684cmd\u91cc\u9762\u7528\u547d\u4ee4\u884c\u8c03\u7528exe\u540e\u53d1\u73b0\u4e86\u95ee\u9898\uff1a<\/p>\n<p style=\"padding-left: 30px;\"><strong>requests.exceptions.SSLError: [Errno 2] No such file or directory<\/strong><\/p>\n<p>\u4e07\u80fd\u7684stackoverflow\u544a\u8bc9\u6211\u4eec\uff0c\u4e0d\u8981\u614c\u6211\u6709\u836f\uff0c<a href=\"https:\/\/github.com\/kennethreitz\/requests\/issues\/557\">\u836f\u5728\u8fd9<\/a>\u3002so\u5927\u592b\u544a\u8bc9\u6211\u4eec\u5728exe\u6587\u4ef6\u6267\u884c\u7684\u65f6\u5019\u73af\u5883\u53d8\u91cf\u53d1\u751f\u4e86\u6539\u53d8\uff0c\u627e\u4e0d\u5230\u8fd9\u4e2apem\u6587\u4ef6\u4e86\uff0c\u6211\u4eec\u9700\u8981\u7ed9\u4e00\u4e2a\u9759\u6001\u7684pem\u6587\u4ef6\u8def\u5f84\uff0c\u6216\u8005\u8bbe\u7f6e\u4e00\u4e2aREQUESTS_CA_BUNDLE\u73af\u5883\u53d8\u91cf\uff0c\u56e0\u4e3a\u4e0d\u77e5\u9053\u8fd9\u4e2aexe\u6587\u4ef6\u4f1a\u5728\u4ec0\u4e48\u73af\u5883\u4e0b\u6267\u884c\uff0c\u4e0d\u80fd\u4fdd\u8bc1\u6240\u5728\u7684\u73af\u5883\u4e2d\u6709\u8fd9\u4e2apem\u6587\u4ef6\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u5728\u6253\u5305\u7684\u65f6\u5019\u6253\u4e00\u4e2apem\u6587\u4ef6\u8fdb\u53bb\u3002\u5230\u6b64\u4ea7\u751f\u4e24\u4e2a\u65b0\u95ee\u9898\uff1a1\uff0c\u5982\u4f55\u83b7\u53d6cacert.pem\u6587\u4ef6\uff1b2\uff0c\u600e\u4e48\u7528py2exe\u6253\u5305\uff0c\u5f53\u7136\u4f60\u80af\u5b9a\u80fd\u60f3\u5230\u76f4\u63a5\u653e\u5230\u751f\u6210\u7684dist\u76ee\u5f55\u4e0b\u9762\u5c31\u53ef\u4ee5\u4e86\uff0c\u4f46\u662f\u8fd9\u6837\u662f\u4e0d\u662f\u592alow\u4e86\uff0c\u6211\u4eec\u8981\u6709\u66f4elegant\u7684\u65b9\u6cd5\u3002<!--more--><\/p>\n<p>\u83b7\u53d6pem\u6587\u4ef6\uff0cso\u5927\u795e\u7ed9\u51fa\u4e86\u7b80\u7ea6\u800c\u4e0d\u7b80\u5355\u7684\u5b9e\u73b0\uff1a<\/p>\n<p style=\"padding-left: 30px;\">import certifi<br \/>\nprint certifi.where()<\/p>\n<p>\u4ec5\u6b64\u4e24\u884c\uff0c\u5b8c\u6574\u7684pem\u6587\u4ef6\u8def\u5f84\u5c31\u51fa\u7089\u4e86\u3002<\/p>\n<p>\u6253\u5305pem\u6587\u4ef6\uff0c\u8fd9\u5c31\u8981\u6539\u4e00\u4e0b\u4e0a\u9762\u7684build_exe.py\u4e86\uff0c\u65b0\u73a9\u6cd5\u4e0d\u5f97\u4e0dget\u4e00\u4e0b\u4e86\uff1a<\/p>\n<p style=\"padding-left: 30px;\">#usage: python build_exe.py py2exe<\/p>\n<p style=\"padding-left: 30px;\">from distutils.core import setup<br \/>\nimport py2exe<\/p>\n<p style=\"padding-left: 30px;\">setup(console=[&#8216;myqqbot.py&#8217;],<br \/>\ndata_files=[(&#8220;certifi&#8221;, [&#8220;cacert.pem&#8221;])] #1<br \/>\n#,(&#8220;fonts&#8221;,\u00a0glob.glob(&#8220;fonts\\\\*.fnt&#8221;))], #2<br \/>\n)<\/p>\n<p>\u4e0a\u9762\u4ee3\u7801\u8bf4\u660e\u4e00\u4e0b\uff1a#1\uff0c\u8fd9\u4e2a\u62ec\u53f7\u91cc\u9762\u4e24\u4e2a\u5b57\u6bb5\u7684\u65b9\u6cd5\u5176\u5b9e\u662f\u7ed9\u4e86\u4e00\u4e2a\u5b50\u76ee\u5f55\u540d\u201ccertifi\u201d\uff0c \u8fd9\u91cc\u4e00\u5b9a\u8981\u628a\u6587\u4ef6\u540d\u653e\u5728[]\u91cc\u9762\uff0c\u56e0\u4e3a\u9ed8\u8ba4\u4e00\u4e2a\u76ee\u5f55\u4e0b\u53ef\u4ee5\u540c\u65f6\u6536\u5f55\u591a\u4e2a\u6587\u4ef6\u7684\u3002\u5f53\u7136\u53ef\u4ee5\u4e0d\u8981\u8fd9\u4e2a\u5b50\u76ee\u5f55\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199data_files=[&#8220;cacert.pem&#8221;]\uff0c\u76f4\u63a5\u5199\u6587\u4ef6\u540d\u7684\u65b9\u6cd5\u5c31\u662f\u9ed8\u8ba4\u8fd9\u4e2a\u6587\u4ef6\u5728\u5de5\u7a0b\u6839\u76ee\u5f55\u4e0b\uff0c\u4e5f\u5c31\u662f\u548cbuild_exe.py\u5728\u4e00\u4e2a\u76ee\u5f55\u3002\u6240\u4ee5\u8fd9\u91cc\u5176\u5b9e\u53ef\u4ee5\u5199\u7edd\u5bf9\u8def\u5f84\u76f4\u63a5\u6307\u5411python\u5b89\u88c5\u76ee\u5f55\u4e0b\u7684\u90a3\u4e2apem\u6587\u4ef6\u3002#2\uff0c\u53ef\u4ee5\u540c\u65f6\u6307\u5b9a\u53e6\u5916\u7684\u5b50\u76ee\u5f55\u548c\u9700\u8981\u6253\u5305\u7684\u6587\u4ef6\uff0c\u8fd8\u53ef\u4ee5\u7528glob\u8fd9\u4e2a\u65b9\u6cd5\u6765\u505a\u6a21\u7cca\u5339\u914d\u3002<\/p>\n<p>\u7ed3\u5408\u8def\u5f84\u67e5\u627e\u548c\u6253\u5305\u811a\u672c\uff0c\u4e00\u4e2a\u4f18\u7f8e\u7684\u5b9e\u73b0\u5982\u4e0b\uff1a<\/p>\n<p style=\"padding-left: 30px;\"><code><\/code>#usage: python build_exe.py py2exe<\/p>\n<p style=\"padding-left: 30px;\">from distutils.core import setup<br \/>\nimport py2exe<br \/>\nimport certifi<\/p>\n<p style=\"padding-left: 30px;\">setup(console=[&#8216;myqqbot.py&#8217;],<br \/>\ndata_files=[certifi.where()])<\/p>\n<p>\u56e0\u4e3a\u8003\u8651\u5230exe\u6267\u884c\u73af\u5883\u95ee\u9898\uff0c\u6240\u4ee5\u6211\u4eec\u6253\u5305\u4e86\u4e2apem\u6587\u4ef6\u5230exe\u6587\u4ef6\u7684\u6267\u884c\u76ee\u5f55\u4e0b\uff0c\u8fd9\u6837\u6211\u4eec\u53ea\u80fd\u5c06exe\u6267\u884c\u65f6\u7684REQUESTS_CA_BUNDLE\u73af\u5883\u53d8\u91cf\u8bbe\u7f6e\u4e3a\u5f53\u65f6\u6267\u884c\u76ee\u5f55\u7684\u7edd\u5bf9\u8def\u5f84+pem\u6587\u4ef6\u76f8\u5bf9\u6267\u884c\u76ee\u5f55\u7684\u76f8\u5bf9\u8def\u5f84\u3002<\/p>\n<p>\u6bd4\u5982\uff1aos.environ[&#8216;REQUESTS_CA_BUNDLE&#8217;] =\u00a0os.path.join(os.path.abspath(&#8216;.&#8217;), &#8216;certifi\\cacert.pem&#8217;)<\/p>\n<p>\u8fd9\u91cc\u53c8\u6316\u4e86\u65b0\u5751\uff0c\u5982\u679c\u5728\u5de5\u7a0b\u76ee\u5f55\u76f4\u63a5\u6267\u884cpy\u811a\u672c\u800c\u4e0d\u662f\u5728dist\u76ee\u5f55\u6267\u884cexe\u6587\u4ef6\uff0c\u90a3\u4e48\u5f53\u524d\u76ee\u5f55\u4e0b\u6839\u672c\u6ca1\u6709pem\u6587\u4ef6\uff0c\u90a3\u4e48\u7a0b\u5e8f\u5c31\u76f4\u63a5byebye\u4e86\uff0c\u8fd9\u4e0d\u662f\u6211\u4eec\u8ffd\u6c42\u5b8c\u7f8e\u7684\u7a0b\u5e8f\u5458\u80fd\u63a5\u53d7\u7684\uff0c\u6211\u4eec\u8fd8\u8981\u8c03\u8bd5\u7a0b\u5e8f\u5462\uff0c\u6bcf\u6b21\u7f16\u8bd1\u6210exe\u518d\u6267\u884c\uff0c\u5f00\u73a9\u7b11\u5462\uff0c\u9c7c\u548c\u718a\u638c\u6211\u4eec\u90fd\u8981\uff1a<\/p>\n<p style=\"padding-left: 30px;\">cacert_path = os.path.join(os.path.abspath(&#8216;.&#8217;), &#8216;certifi\\cacert.pem&#8217;)<br \/>\nif os.path.exists(cacert_path):<br \/>\nos.environ[&#8216;REQUESTS_CA_BUNDLE&#8217;] = cacert_path<\/p>\n<p>\u4e8e\u662f\uff0c\u6211\u4eecpy\u548cexe\u90fd\u53ef\u4ee5\u6109\u5feb\u7684\u6267\u884c\u4e86~~<\/p>\n<p>\u81f3\u4e8ecacert.pem\u5230\u5e95\u662f\u4ec0\u4e48\u9b3c\uff0c\u8fd9\u4e2a\u63a5\u4e0b\u6765\u518d\u7814\u7a76\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u505a\u4e2a\u5174\u8da3\u5c0f\u9879\u76ee\uff0c\u9700\u8981\u7528python\u6765get\/post https\u8bf7\u6c42\uff0c\u7528\u5230\u4e86requests\u6a21\u5757\uff0c\u56e0\u4e3a\u8981\u5728windows\u4e0b\u4f7f\u7528\uff0c\u8003\u8651\u5230\u6613\u7528\u6027\u548c\u5c0f\u767d\u73a9\u5bb6\u7684\u9700\u6c42\uff0c\u4f7f\u7528py2exe\u751f\u6210exe\u3002\u53c2\u8003\u94fe\u63a5\u770b\u8d77\u6765\u5f88\u7b80\u5355\uff0c\u7edf\u5171\u5206\u4e09\u6b65\uff1a1\uff0c\u5b89\u88c5py2exe\uff1b2\uff0c\u51c6\u5907\u4e00\u4e2a\u914d\u7f6e\u811a\u672c\uff0c\u6211\u4eec\u53eb\u4ed6build_exe.py\uff1b3\uff0c\u6267\u884c\u4e00\u6761\u547d\u4ee4python build_exe.py py2exe \u5148\u628a\u8fd9\u4e2a\u7b80\u5355\u7684\u521d\u7ea7\u7248\u672c\u653e\u8fd9\u91cc\uff1a #usage: python build_exe.py py2exe from distutils.core import setup import py2exe setup(console=[&#8216;myqqbot.py&#8217;]) \u4f46\u662f\u8fd9\u91cc\u5176\u5b9e\u8fd8\u662f\u6709\u5751\u7684\uff0c\u5b89\u88c5py2exe\u8981\u6ce8\u610f\uff0c\u5e76\u4e0d\u662f\u76f4\u63a5pip install py2exe\u5c31\u53ef\u4ee5\u4e86\uff0cpy2exe\u662f\u4eba\u5bb6python3\u4e13\u7528\u7684\uff0cpython2\u7684\u662fpy2exe_py2\uff0c\u60b2\u5267\u5427\uff0c\u6240\u4ee5\u88c5\u5305\u524d\u6700\u597d\u8fd8\u662fpip search\u4e0b\uff0c\u514d\u5f97\u6d6a\u8d39\u65f6\u95f4\u8fd8\u4e0d\u77e5\u9053\u9519\u5728\u54ea\u3002\u914d\u7f6e\u811a\u672c\u8c8c\u4f3c\u7b80\u5355\uff0c\u5927\u5bb6\u5e94\u8be5\u80fd\u60f3\u5230\u8fd9\u91cc\u9762\u5176\u5b9e\u662f\u6709\u5f88\u591a\u73a9\u6cd5\u7684\uff0c\u4ee5\u81f3\u4e8e\u9700\u8981\u4e00\u4e2a\u811a\u672c\u800c\u4e0d\u662f\u76f4\u63a5\u4f20\u53c2\u3002 &nbsp; \u751f\u6210\u8fc7\u7a0b\u5f88\u987a\u5229\uff0c\u4e0d\u8fc7\u51e0\u79d2exe\u6587\u4ef6\u5df2\u7ecf\u8eba\u5728dist\u76ee\u5f55\u4e0b\u4e86\u3002\u4f46\u662f\u53cc\u51fb\u4ed6\uff0cconsole\u4e00\u95ea\u800c\u8fc7\uff0c\u90fd\u4e0d\u77e5\u9053\u53d1\u751f\u4e86\u4ec0\u4e48\u3002Windows\u5c31\u662f\u8fd9\u4e48\u53d8\u6001\uff0c\u53cd\u6b63\u628a\u4f60\u5f53\u5c0f\u767d\uff0c\u4e0d\u7ed9\u4f60\u4efb\u4f55\u9519\u8bef\u4fe1\u606f\u3002\u5728\u8d85\u7ea7\u96be\u7528\u7684cmd\u91cc\u9762\u7528\u547d\u4ee4\u884c\u8c03\u7528exe\u540e\u53d1\u73b0\u4e86\u95ee\u9898\uff1a requests.exceptions.SSLError: [Errno 2] No such file or directory \u4e07\u80fd\u7684stackoverflow\u544a\u8bc9\u6211\u4eec\uff0c\u4e0d\u8981\u614c\u6211\u6709\u836f\uff0c\u836f\u5728\u8fd9\u3002so\u5927\u592b\u544a\u8bc9\u6211\u4eec\u5728exe\u6587\u4ef6\u6267\u884c\u7684\u65f6\u5019\u73af\u5883\u53d8\u91cf\u53d1\u751f\u4e86\u6539\u53d8\uff0c\u627e\u4e0d\u5230\u8fd9\u4e2apem\u6587\u4ef6\u4e86\uff0c\u6211\u4eec\u9700\u8981\u7ed9\u4e00\u4e2a\u9759\u6001\u7684pem\u6587\u4ef6\u8def\u5f84\uff0c\u6216\u8005\u8bbe\u7f6e\u4e00\u4e2aREQUESTS_CA_BUNDLE\u73af\u5883\u53d8\u91cf\uff0c\u56e0\u4e3a\u4e0d\u77e5\u9053\u8fd9\u4e2aexe\u6587\u4ef6\u4f1a\u5728\u4ec0\u4e48\u73af\u5883\u4e0b\u6267\u884c\uff0c\u4e0d\u80fd\u4fdd\u8bc1\u6240\u5728\u7684\u73af\u5883\u4e2d\u6709\u8fd9\u4e2apem\u6587\u4ef6\uff0c\u6240\u4ee5\u6211\u4eec\u5fc5\u987b\u5728\u6253\u5305\u7684\u65f6\u5019\u6253\u4e00\u4e2apem\u6587\u4ef6\u8fdb\u53bb\u3002\u5230\u6b64\u4ea7\u751f\u4e24\u4e2a\u65b0\u95ee\u9898\uff1a1\uff0c\u5982\u4f55\u83b7\u53d6cacert.pem\u6587\u4ef6\uff1b2\uff0c\u600e\u4e48\u7528py2exe\u6253\u5305\uff0c\u5f53\u7136\u4f60\u80af\u5b9a\u80fd\u60f3\u5230\u76f4\u63a5\u653e\u5230\u751f\u6210\u7684dist\u76ee\u5f55\u4e0b\u9762\u5c31\u53ef\u4ee5\u4e86\uff0c\u4f46\u662f\u8fd9\u6837\u662f\u4e0d\u662f\u592alow\u4e86\uff0c\u6211\u4eec\u8981\u6709\u66f4elegant\u7684\u65b9\u6cd5\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false,"jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[4],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p7Dhki-1t","jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/posts\/91"}],"collection":[{"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.luo666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=91"}],"version-history":[{"count":6,"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/posts\/91\/revisions"}],"predecessor-version":[{"id":144,"href":"http:\/\/www.luo666.com\/index.php?rest_route=\/wp\/v2\/posts\/91\/revisions\/144"}],"wp:attachment":[{"href":"http:\/\/www.luo666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=91"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.luo666.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=91"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.luo666.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=91"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}