Search code examples
pythonsslpippraw

I am having multiple SSL error's with different python applications


First, I cannot pip install pretty much anything without adding in --trusted-host pypi.python.org. If I do not include it, i get an error reading

Could not fetch URL https://pypi.python.org/simple/beautifulsoup/: There was a problem confirming the ssl certificate: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:748) - skipping`

BeautifulSoup was just one example, it happens all across the board.

Additionally, if I run this reddit bot code:

from bs4 import BeautifulSoup
from urllib.parse import urlparse

import requests
import praw
import time
import re
import bs4
import ssl

USERAGENT = 'web:PeskyTrollBot:v0.1 (by /u/ThePeskyWabbit)'
auth = True

def authenticate():
    print("Authenticating...")
    reddit = praw.Reddit('bot1', user_agent=USERAGENT)
    print("Authenticated as {}\n" .format(reddit.user.me()))
    return reddit

authenticate()
#while auth:
#    try:
#        authenticate()
#        auth = False
#    except:
#        print('authentication failed. Retrying in 1 minute.\n')
#        time.sleep(60) 

I get the error output:

C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/JoshLaptop/PycharmProjects/practice/TestBot.py
Authenticating...
Traceback (most recent call last):
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\contrib\pyopenssl.py", line 441, in wrap_socket
    cnx.do_handshake()
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenSSL\SSL.py", line 1716, in do_handshake
    self._raise_ssl_error(self._ssl, result)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenSSL\SSL.py", line 1456, in _raise_ssl_error
    _raise_current_error()
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenSSL\_util.py", line 54, in exception_from_error_queue
    raise exception_type(errors)
OpenSSL.SSL.Error: [('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 595, in urlopen
    self._prepare_proxy(conn)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 816, in _prepare_proxy
    conn.connect()
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connection.py", line 326, in connect
    ssl_context=context)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\util\ssl_.py", line 329, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\contrib\pyopenssl.py", line 448, in wrap_socket
    raise ssl.SSLError('bad handshake: %r' % e)
ssl.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\adapters.py", line 440, in send
    timeout=timeout
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\util\retry.py", line 388, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.reddit.com', port=443): Max retries exceeded with url: /api/v1/access_token (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",),))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\requestor.py", line 47, in request
    return self._http.request(*args, timeout=TIMEOUT, **kwargs)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\adapters.py", line 506, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='www.reddit.com', port=443): Max retries exceeded with url: /api/v1/access_token (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",),))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/JoshLaptop/PycharmProjects/practice/TestBot.py", line 20, in <module>
    authenticate()
  File "C:/Users/JoshLaptop/PycharmProjects/practice/TestBot.py", line 17, in authenticate
    print("Authenticated as {}\n" .format(reddit.user.me()))
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\praw\models\user.py", line 60, in me
    user_data = self._reddit.get(API_PATH['me'])
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\praw\reddit.py", line 367, in get
    data = self.request('GET', path, params=params)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\praw\reddit.py", line 472, in request
    params=params)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\sessions.py", line 175, in request
    params=params, url=url)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\sessions.py", line 121, in _request_with_retries
    retries, saved_exception, url)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\sessions.py", line 87, in _do_retry
    params=params, url=url, retries=retries - 1)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\sessions.py", line 121, in _request_with_retries
    retries, saved_exception, url)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\sessions.py", line 87, in _do_retry
    params=params, url=url, retries=retries - 1)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\sessions.py", line 109, in _request_with_retries
    data, files, json, method, params, retries, url)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\sessions.py", line 94, in _make_request
    params=params)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\rate_limit.py", line 32, in call
    kwargs['headers'] = set_header_callback()
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\sessions.py", line 135, in _set_header_callback
    self._authorizer.refresh()
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\auth.py", line 328, in refresh
    password=self._password)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\auth.py", line 138, in _request_token
    response = self._authenticator._post(url, **data)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\auth.py", line 29, in _post
    data=sorted(data.items()))
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\requestor.py", line 49, in request
    raise RequestException(exc, args, kwargs)
prawcore.exceptions.RequestException: error with request HTTPSConnectionPool(host='www.reddit.com', port=443): Max retries exceeded with url: /api/v1/access_token (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",),))

Process finished with exit code 1

and finally if I execute this code exactly: http://praw.readthedocs.io/en/latest/tutorials/refresh_token.html

I get the error output:

Traceback (most recent call last):
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\contrib\pyopenssl.py", line 441, in wrap_socket
    cnx.do_handshake()
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenSSL\SSL.py", line 1716, in do_handshake
    self._raise_ssl_error(self._ssl, result)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenSSL\SSL.py", line 1456, in _raise_ssl_error
    _raise_current_error()
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\OpenSSL\_util.py", line 54, in exception_from_error_queue
    raise exception_type(errors)
OpenSSL.SSL.Error: [('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 595, in urlopen
    self._prepare_proxy(conn)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 816, in _prepare_proxy
    conn.connect()
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connection.py", line 326, in connect
    ssl_context=context)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\util\ssl_.py", line 329, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\contrib\pyopenssl.py", line 448, in wrap_socket
    raise ssl.SSLError('bad handshake: %r' % e)
ssl.SSLError: ("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\adapters.py", line 440, in send
    timeout=timeout
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\urllib3\util\retry.py", line 388, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.reddit.com', port=443): Max retries exceeded with url: /api/v1/access_token (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",),))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\requestor.py", line 47, in request
    return self._http.request(*args, timeout=TIMEOUT, **kwargs)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\sessions.py", line 508, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\sessions.py", line 618, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\requests\adapters.py", line 506, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='www.reddit.com', port=443): Max retries exceeded with url: /api/v1/access_token (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",),))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/JoshLaptop/PycharmProjects/practice/RefreshToken.py", line 95, in <module>
    sys.exit(main())
  File "C:/Users/JoshLaptop/PycharmProjects/practice/RefreshToken.py", line 89, in main
    refresh_token = reddit.auth.authorize(params['code'])
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\praw\models\auth.py", line 52, in authorize
    authorizer.authorize(code)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\auth.py", line 202, in authorize
    redirect_uri=self._authenticator.redirect_uri)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\auth.py", line 138, in _request_token
    response = self._authenticator._post(url, **data)
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\auth.py", line 29, in _post
    data=sorted(data.items()))
  File "C:\Users\JoshLaptop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\prawcore\requestor.py", line 49, in request
    raise RequestException(exc, args, kwargs)
prawcore.exceptions.RequestException: error with request HTTPSConnectionPool(host='www.reddit.com', port=443): Max retries exceeded with url: /api/v1/access_token (Caused by SSLError(SSLError("bad handshake: Error([('SSL routines', 'tls_process_server_certificate', 'certificate verify failed')],)",),))

Process finished with exit code 1

I have updated pyopenssl. I have reinstalled python. I have rebooted. I have tried authenticating in a try/except block. I have updated certificates. They all seem related to a root cause such as a missing certificate but I can't find documentation on how to get the correct certs other than by updating pyopenssl. I don't think this programming related issue because of the problems I have with cmd when pip installing things. Please help!!


Solution

  • This problem was fixed by installing python directly to the C:\ directory, rather than within my user profile.