SSLError in Requests when packaging as OS X .app

后端 未结 4 1449
傲寒
傲寒 2021-02-06 08:44

I\'m developing an application for OS X. The application involves communicating with a server through python-requests, using a secure connection.

I am able to run the p

4条回答
  •  一整个雨季
    2021-02-06 09:24

    I ran into the same problem and had to distribute my application to users who may not have Python or the certifi package installed on their Mac. Drawing on inspirations from the answers here, I came up with the following solution.

    Step 1: Download a OpenSSL package from https://www.openssl.org/source/. Find /openssl-1.0.2n/certs/demo/ca-cert.pem and place it under the same directory as your Python program (for example main.py).

    Step 2: Create a setup.py as usual, but include ca-cert.pem in the DATA_FILES list. So your setup.py should look something like this:

    from setuptools import setup
    
    APP = ['main.py']
    DATA_FILES = ['ca-cert.pem']
    OPTIONS = {'argv_emulation': False}
    
    setup(
        app=APP,
        data_files=DATA_FILES,
        options={'py2app': OPTIONS},
        setup_requires=['py2app'],
    )
    

    Step 3: Use the verify parameter so requests will use the certificate file you provide.

    import requests
    requests.get("https://httpbin.org/", verify="ca-cert.pem")
    

    Alternatively, you can also create a session so that you don't have to specify verify every time.

    import requests
    s = requests.Session()
    s.verify = "ca-cert.pem"
    s.get("https://httpbin.org")
    

    Step 4: Package the application using py2app as usual. The resulting app should be able to run normally.

    python setup.py py2app
    

提交回复
热议问题