2013-06-17 12 views
8

サイトにログインしてCSRFクッキーを設定する必要があるPythonプログラムのテストが終了しました。私はpy2exeを使ってexeとしてパッケージングしようとしましたが、ソケットエラーが発生しました。私はPyInstallerで試してみると同じ問題があります。私は同じ問題を抱える他の人たちをいくつか見つけたので、問題はSLL証明書の所在と関係があることを知っています。py2exe(またはPyInstaller)でコンパイルされたexeでのSSL証明書エラーの修正

これは、ログ呼び出しを含む私のsite_agentクラスです。

class site_agent: 
     self.get_params() 
     URL = root_url + '/accounts/login/'   
     # Retrieve the CSRF token first 
     self.agent = requests.session() 
     self.agent.get(URL) # retrieves the cookie # This line throws the error 
     self.csrftoken = self.agent.cookies['csrftoken']  
     # Set up login data including the CSRF cookie 
     login_data = {'username': self.username, 
         'password': self.password, 
         'csrfmiddlewaretoken' : self.csrftoken} 
     # Log in 
     logging.info('Logging in') 
     response = self.agent.post(URL, data=login_data, headers=hdr) 

エラーがself.agent.get(URL)ラインに来て、トレースバックを示しています

Traceback (most recent call last): 
    File "<string>", line 223, in <module> 
    File "<string>", line 198, in main 
    File "<string>", line 49, in __init__ 
    File "C:\pyinstaller-2.0\pyinstaller-2.0\autoresponder\b 
uild\pyi.win32\autoresponder\out00-PYZ.pyz\requests.sessions", line 350, in get 
    File "C:\pyinstaller-2.0\pyinstaller-2.0\autoresponder\b 
uild\pyi.win32\autoresponder\out00-PYZ.pyz\requests.sessions", line 338, in requ 
est 
    File "C:\pyinstaller-2.0\pyinstaller-2.0\autoresponder\b 
uild\pyi.win32\autoresponder\out00-PYZ.pyz\requests.sessions", line 441, in send 

    File "C:\pyinstaller-2.0\pyinstaller-2.0\autoresponder\b 
uild\pyi.win32\autoresponder\out00-PYZ.pyz\requests.adapters", line 331, in send 

requests.exceptions.SSLError: [Errno 185090050] _ssl.c:336: error:0B084002:x509 
certificate routines:X509_load_cert_crl_file:system lib 

この問題はrequests.adaptersであることを意味するのでしょうか?もしそうなら

、私は、どこかcacert.pemの探しpy2exeまたはPyInstallerと私のexeファイルを再構築し、その後のPythonの私のインストールされたバージョンに戻ってそれを変更することが私のインストールPythonパッケージでそれを編集することができますか?私は今PyInstallerでコンパイルすると、すべてのrequests.get()requests.post()呼び出しでverify=Falseを設定した後、プログラムを実行している

EDIT。しかし、SSLがある理由のために、私は本当に誰もがこのツールを使用する前にこのエラーを修正できるようにしたいと思います。

+2

http://stackoverflow.com/a/21206079/538284 –

答えて

4

pyInstallerのを使用している場合... frmdstryrによって与えられた答えに加えて

from hookutils import collect_data_files 

# Get the cacert.pem 
datas = collect_data_files('requests') 
+1

pyinstallerにはデフォルトでこのフックが含まれるようになりました – gdw2

3

を含むlibの要求のためのPyInstaller\hooks\hook-requests.pyファイルを作成し、私はcacert.pemがどこにあるrequestsを教えていました。私は右の私の輸入後に以下の行を追加しました:

# Get the base directory 
if getattr(sys , 'frozen' , None): # keyword 'frozen' is for setting basedir while in onefile mode in pyinstaller 
    basedir = sys._MEIPASS 
else: 
    basedir = os.path.dirname(__file__) 
    basedir = os.path.normpath(basedir) 

# Locate the SSL certificate for requests 
os.environ['REQUESTS_CA_BUNDLE'] = os.path.join(basedir , 'requests', 'cacert.pem') 

このソリューションでは、このリンクから派生している:あなたは「リクエスト」モジュールを使用している場合はhttp://kittyandbear.net/python/pyinstaller-request-sslerror-manual-cacert-solution.txt

1

これは簡単に解決することができます。

1)、exeファイルが存在している、あなたの配布フォルダ内)「要求」モジュールは

os.environ['REQUESTS_CA_BUNDLE'] = "certifi/cacert.pem" 

2を使用しているメインのpythonファイルに以下のコードを配置し、「認証取得」というフォルダを作成し、配置しますその中に "cacert.pem"ファイルがあります。

3)今、あなたの配布は、SSL呼び出しを検証するために必要な証明書が含まれて...

pip install certifi 

import certifi 
certifi.where() 

恐ろしいことで「このcacert.pem」ファイルを見つけることができます。