Importing gTTs into Python issue - python

When I enter the code in the Python Shell
I get this error.
Traceback (most recent call last):
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen
chunked=chunked)
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 343, in _make_request
self._validate_conn(conn)
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 839, in _validate_conn
conn.connect()
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connection.py", line 344, in connect
ssl_context=context)
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\util\ssl_.py", line 344, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 412, in wrap_socket
session=session
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 853, in _create
self.do_handshake()
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\ssl.py", line 1117, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1051)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\adapters.py", line 449, in send
timeout=timeout
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen
_stacktrace=sys.exc_info()[2])
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\site-packages\urllib3\util\retry.py", line 398, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='translate.google.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1051)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\site-packages\gtts\tts.py", line 189, in write_to_fp
part_tk = self.token.calculate_token(part)
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\site-packages\gtts_token\gtts_token.py", line 28, in calculate_token
seed = self._get_token_key()
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\site-packages\gtts_token\gtts_token.py", line 55, in _get_token_key
response = requests.get("https://translate.google.com/")
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\site-packages\requests\adapters.py", line 514, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='translate.google.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1051)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
tts.write_to_fp(mp3_fp)
File "C:\Users\Raav\AppData\Local\Programs\Python\Python37\lib\site-packages\gtts\tts.py", line 194, in write_to_fp
str(e))
gtts.tts.gTTSError: Connection error during token calculation: HTTPSConnectionPool(host='translate.google.com', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1051)')))
Because I have correctly installed gTTS I am pretty sure.
I used the example that was given at https://pypi.org/project/gTTS/
The code that I used was
From gtts import gTTs
TTS = gTTS(‘hello’)
TTS.save(‘hello’)
I did not create a separate program I just entered each line separately in the IDLE.

Related

Pandas_datareader issue

When I try to read data from stooq through pandas_datareader.data this error keeps coming through:
Traceback (most recent call last):
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/urllib3/connectionpool.py", line 700, in urlopen
self._prepare_proxy(conn)
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/urllib3/connectionpool.py", line 996, in _prepare_proxy
conn.connect()
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/urllib3/connection.py", line 414, in connect
self.sock = ssl_wrap_socket(
^^^^^^^^^^^^^^^^
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/urllib3/util/ssl_.py", line 449, in ssl_wrap_socket
ssl_sock = _ssl_wrap_socket_impl(
^^^^^^^^^^^^^^^^^^^^^^
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/urllib3/util/ssl_.py", line 493, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ssl.py", line 517, in wrap_socket
return self.sslsocket_class._create(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ssl.py", line 1075, in _create
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/ssl.py", line 1346, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:992)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/requests/adapters.py", line 489, in send
resp = conn.urlopen(
^^^^^^^^^^^^^
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/urllib3/connectionpool.py", line 787, in urlopen
retries = retries.increment(
^^^^^^^^^^^^^^^^^^
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/urllib3/util/retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='stooq.com', port=443): Max retries exceeded with url: /q/d/l/?s=%5EDJI&i=d&d1=20180216&d2=20230215 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:992)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/matteo/PycharmProjects/pythonProject/stock_data.py", line 3, in <module>
f = web.DataReader('^DJI', 'stooq')
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/pandas/util/_decorators.py", line 211, in wrapper
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/pandas_datareader/data.py", line 432, in DataReader
).read()
^^^^^^
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/pandas_datareader/base.py", line 253, in read
df = self._read_one_data(self.url, params=self._get_params(self.symbols))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/pandas_datareader/base.py", line 108, in _read_one_data
out = self._read_url_as_StringIO(url, params=params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/pandas_datareader/base.py", line 119, in _read_url_as_StringIO
response = self._get_response(url, params=params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/pandas_datareader/base.py", line 155, in _get_response
response = self.session.get(
^^^^^^^^^^^^^^^^^
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/requests/sessions.py", line 600, in get
return self.request("GET", url, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/requests/sessions.py", line 587, in request
resp = self.send(prep, **send_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/requests/sessions.py", line 701, in send
r = adapter.send(request, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/matteo/PycharmProjects/pythonProject/venv/lib/python3.11/site-packages/requests/adapters.py", line 563, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='stooq.com', port=443): Max retries exceeded with url: /q/d/l/?s=%5EDJI&i=d&d1=20180216&d2=20230215 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:992)')))
I tried to run the same code on jupiter lab and the error is the same, contrary in google colab I get the correct result. Here is the code from google colab:
Code:
import pandas_datareader.data as web
f = web.DataReader('^DJI', 'stooq')
f[:10]

Issues connecting to sharepoint 2019 with the code below. How do I disable ssl?

Issues connecting to sharepoint 2019 with the code below. How do I disable ssl? When i try the same code with sharepoint online, everything works fine but not sharepoint 2019. Any help will be really appreciated.
Issues connecting to sharepoint 2019 with the code below. How do I disable ssl? When i try the same code with sharepoint online, everything works fine but not sharepoint 2019. Any help will be really appreciated.
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
import os
import requests
def disable_ssl(request):
request.verify = False
app_settings = {
'url': 'https://mysp.com/sites/'+'sitenameGG'+'/',
'client_id': 'xxxx',
'client_secret': 'xxxx',
}
context_auth = AuthenticationContext(app_settings['url'])
context_auth.acquire_token_for_app(client_id=app_settings['client_id'], client_secret=app_settings['client_secret'])
ctx = ClientContext(app_settings['url'], context_auth)
web = ctx.web
ctx.load(web)
ctx.pending_request().beforeExecute += disable_ssl
ctx.execute_query()
response = File.open_binary(ctx, "/sites/sitenameGG/"+('some%20test%20lib/hello.csv'))
with open(r'C:\Users\somepath\'+filename, 'wb') as local_file:
local_file.write(response.content)
Error Results/Traceback of below:
Traceback (most recent call last):
File "C:\Python310\lib\urllib3\connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "C:\Python310\lib\urllib3\connectionpool.py", line 386, in _make_request
self._validate_conn(conn)
File "C:\Python310\lib\urllib3\connectionpool.py", line 1042, in _validate_conn
conn.connect()
File "C:\Python310\lib\urllib3\connection.py", line 414, in connect
self.sock = ssl_wrap_socket(
File "C:\Python310\lib\urllib3\util\ssl_.py", line 449, in ssl_wrap_socket
ssl_sock = _ssl_wrap_socket_impl(
File "C:\Python310\lib\urllib3\util\ssl_.py", line 493, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
File "C:\Python310\lib\ssl.py", line 512, in wrap_socket
return self.sslsocket_class._create(
File "C:\Python310\lib\ssl.py", line 1070, in _create
self.do_handshake()
File "C:\Python310\lib\ssl.py", line 1341, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python310\lib\requests\adapters.py", line 440, in send
resp = conn.urlopen(
File "C:\Python310\lib\urllib3\connectionpool.py", line 786, in urlopen
retries = retries.increment(
File "C:\Python310\lib\urllib3\util\retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='mysp.somesiteGG.com', port=443): Max retries exceeded with url: /sites/somesiteGG/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python310\lib\office365\runtime\auth\providers\acs_token_provider.py", line 41, in get_app_only_access_token
realm = self._get_realm_from_target_url()
File "C:\Python310\lib\office365\runtime\auth\providers\acs_token_provider.py", line 70, in _get_realm_from_target_url
response = requests.head(url=self.url, headers={'Authorization': 'Bearer'})
File "C:\Python310\lib\requests\api.py", line 102, in head
return request('head', url, **kwargs)
File "C:\Python310\lib\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python310\lib\requests\sessions.py", line 529, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python310\lib\requests\sessions.py", line 645, in send
r = adapter.send(request, **kwargs)
File "C:\Python310\lib\requests\adapters.py", line 518, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='mysp.somesiteGG.com', port=443): Max retries exceeded with url: /sites/somesiteGG/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\G14826\PycharmProjects\pythonProject\access_rest.py", line 22, in <module>
context_auth.acquire_token_for_app(client_id=app_settings['client_id'], client_secret=app_settings['client_secret'])
File "C:\Python310\lib\office365\runtime\auth\authentication_context.py", line 82, in acquire_token_for_app
return self._provider.ensure_app_only_access_token()
File "C:\Python310\lib\office365\runtime\auth\providers\acs_token_provider.py", line 36, in ensure_app_only_access_token
self._cached_token = self.get_app_only_access_token()
File "C:\Python310\lib\office365\runtime\auth\providers\acs_token_provider.py", line 45, in get_app_only_access_token
self.error = e.response.text
AttributeError: 'NoneType' object has no attribute 'text'
Process finished with exit code 1

Getting error: "certificate verify failed: unable to get local issuer certificate" trying to webscrape pdfs in PyCharm

Here is the code that I have been trying to fix:
import requests
from urllib.parse import urljoin
from bs4 import BeautifulSoup
url = "https://www.pbs.gov.pk/node/3391/?name=001"
#If there is no such folder, the script will create one automatically
folder_location = r'infoFA'
if not os.path.exists(folder_location):os.mkdir(folder_location)
response = requests.get(url)
soup= BeautifulSoup(response.text, "html.parser")
for link in soup.select("a[href$='.pdf']"):
#Name the pdf files using the last portion of each link which are unique in this case
filename = os.path.join(folder_location,link['href'].split('/')[-1])
with open(filename, 'wb') as f:
f.write(requests.get(urljoin(url,link['href'])).content)
From this, I get the following error in PyCharm:
Traceback (most recent call last):
File "/Users/justinlesko/PycharmProjects/pythonProject8/venv/lib/python3.9/site-packages/urllib3/connectionpool.py", line 699, in urlopen
httplib_response = self._make_request(
File "/Users/justinlesko/PycharmProjects/pythonProject8/venv/lib/python3.9/site-packages/urllib3/connectionpool.py", line 382, in _make_request
self._validate_conn(conn)
File "/Users/justinlesko/PycharmProjects/pythonProject8/venv/lib/python3.9/site-packages/urllib3/connectionpool.py", line 1010, in _validate_conn
conn.connect()
File "/Users/justinlesko/PycharmProjects/pythonProject8/venv/lib/python3.9/site-packages/urllib3/connection.py", line 411, in connect
self.sock = ssl_wrap_socket(
File "/Users/justinlesko/PycharmProjects/pythonProject8/venv/lib/python3.9/site-packages/urllib3/util/ssl_.py", line 449, in ssl_wrap_socket
ssl_sock = _ssl_wrap_socket_impl(
File "/Users/justinlesko/PycharmProjects/pythonProject8/venv/lib/python3.9/site-packages/urllib3/util/ssl_.py", line 493, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock, server_hostname=server_hostname)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1040, in _create
self.do_handshake()
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/justinlesko/PycharmProjects/pythonProject8/venv/lib/python3.9/site-packages/requests/adapters.py", line 439, in send
resp = conn.urlopen(
File "/Users/justinlesko/PycharmProjects/pythonProject8/venv/lib/python3.9/site-packages/urllib3/connectionpool.py", line 755, in urlopen
retries = retries.increment(
File "/Users/justinlesko/PycharmProjects/pythonProject8/venv/lib/python3.9/site-packages/urllib3/util/retry.py", line 574, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.pbs.gov.pk', port=443): Max retries exceeded with url: /node/3391/?name=001 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/Users/justinlesko/PycharmProjects/pythonProject8/main.py", line 10, in <module>
response = requests.get(url)
File "/Users/justinlesko/PycharmProjects/pythonProject8/venv/lib/python3.9/site-packages/requests/api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "/Users/justinlesko/PycharmProjects/pythonProject8/venv/lib/python3.9/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/Users/justinlesko/PycharmProjects/pythonProject8/venv/lib/python3.9/site-packages/requests/sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "/Users/justinlesko/PycharmProjects/pythonProject8/venv/lib/python3.9/site-packages/requests/sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "/Users/justinlesko/PycharmProjects/pythonProject8/venv/lib/python3.9/site-packages/requests/adapters.py", line 514, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='www.pbs.gov.pk', port=443): Max retries exceeded with url: /node/3391/?name=001 (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1129)')))
Process finished with exit code 1
I have tried the following to fix this ssl error:
-setting verify to "False" in requests.get(url)
-installing and reinstalling certificates from the python 3.9 folder I have
-setting unknown certificates to "trust" in preferences
Rather than turn it off you could add the proper cert to your cacert.pem file. You can append it to the end of the file.
For pycharm it will be beneath the PycharmProjects file when using a python SDK you downloaded with PyCharm. For example:
./PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/certifi/cacert.pem

How to use cert and key file with Python Requests on client side?

I have a homework assignment, where I have to communicate with a server. I'm at the last task, but I can't get it to work. The task is the following:
Connect to http://[Server IP] and download cert.php and key.php files.
After that, use these files to GET https://[Server IP].
If the HTTPS request is successful, the server will reply with the unique flag. (hw's solution)
I can download the files no problem, but when I try to use them, I get errors:
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen
httplib_response = self._make_request(
File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py", line 382, in _make_request
self._validate_conn(conn)
File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py", line 1010, in _validate_conn
conn.connect()
File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connection.py", line 411, in connect
self.sock = ssl_wrap_socket(
File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\util\ssl_.py", line 432, in ssl_wrap_socket
ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls)
File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\util\ssl_.py", line 474, in _ssl_wrap_socket_impl
return ssl_context.wrap_socket(sock)
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1040, in _create
self.do_handshake()
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1122)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen
retries = retries.increment(
File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\util\retry.py", line 574, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='152.66.249.144', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1122)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\pythonProject\HF\hf_script.py", line 195, in <module>
final_conn = requests.get(url_https,
File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\requests\api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\requests\api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\requests\sessions.py", line 542, in request
resp = self.send(prep, **send_kwargs)
File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\requests\sessions.py", line 655, in send
r = adapter.send(request, **kwargs)
File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\requests\adapters.py", line 514, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='152.66.249.144', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1122)')))
Process finished with exit code 1
I have also tried saving the files with .pem extension, I don't even know if that makes sense, but didn't work. Furthermore I have tried to use ssl and creating an unverified context, but it says Cannot find reference '_create_unverified_context' in 'ssl.pyi'
Here's the code I work with:
# Creating request with session cookie
s = requests.session()
r = s.post(url, data=data)
# Downloading the cert
cert = s.get(cert_url)
open('cert.php', 'wb').write(cert.content)
# Downloading the key
key = s.get(key_url)
open('key.php', 'wb').write(key.content)
final_conn = requests.get(url_https,
cert=('C:/Users/user/PycharmProjects/pythonProject/HF/cert.php',
'C:/Users/user/PycharmProjects/pythonProject/HF/key.php'))
print(final_conn.text)
Okay, I got it. I doubt anyone will use this, but the cert and key I used was fine, the pbroblem was, the server itself was using a self-signed certificate, and I had to establish the connection anyway. The solution that worked for me:
final_conn = requests.get(url_https,
cert=('C:/Users/user/PycharmProjects/pythonProject/HF/cert.php',
'C:/Users/user/PycharmProjects/pythonProject/HF/key.php'),
verify=False)

Passing security certificates using requests

I need to access a site, but I am getting an error:
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)
Code:
import requests
BASE_URL = "https://www.portfolio123.com/"
r = requests.get(BASE_URL + 'server_status.jsp')
Full Error:
Traceback (most recent call last):
File "C:\Python38-32\lib\site-packages\urllib3\connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(
File "C:\Python38-32\lib\site-packages\urllib3\connectionpool.py", line 376, in _make_request
self._validate_conn(conn)
File "C:\Python38-32\lib\site-packages\urllib3\connectionpool.py", line 994, in _validate_conn
conn.connect()
File "C:\Python38-32\lib\site-packages\urllib3\connection.py", line 352, in connect
self.sock = ssl_wrap_socket(
File "C:\Python38-32\lib\site-packages\urllib3\util\ssl_.py", line 370, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "C:\Python38-32\lib\ssl.py", line 500, in wrap_socket
return self.sslsocket_class._create(
File "C:\Python38-32\lib\ssl.py", line 1040, in _create
self.do_handshake()
File "C:\Python38-32\lib\ssl.py", line 1309, in do_handshake
self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python38-32\lib\site-packages\requests\adapters.py", line 439, in send
resp = conn.urlopen(
File "C:\Python38-32\lib\site-packages\urllib3\connectionpool.py", line 719, in urlopen
retries = retries.increment(
File "C:\Python38-32\lib\site-packages\urllib3\util\retry.py", line 436, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.portfolio123.com', port=443): Max retries exceeded with url: /server_status.jsp (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)')))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Lakewealth/.PyCharmCE2019.2/config/scratches/scratch_4.py", line 5, in <module>
r = requests.get(BASE_URL + 'server_status.jsp')
File "C:\Python38-32\lib\site-packages\requests\api.py", line 75, in get
return request('get', url, params=params, **kwargs)
File "C:\Python38-32\lib\site-packages\requests\api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python38-32\lib\site-packages\requests\sessions.py", line 533, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python38-32\lib\site-packages\requests\sessions.py", line 646, in send
r = adapter.send(request, **kwargs)
File "C:\Python38-32\lib\site-packages\requests\adapters.py", line 514, in send
raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='www.portfolio123.com', port=443): Max retries exceeded with url: /server_status.jsp (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1108)')))
There are a lot of questions on this subject such as here, here, and here. I scanned through them, but didn't find the solution yet.
import requests
BASE_URL = "https://www.portfolio123.com/"
CERT = 'c:\data\P123.CER'
r = requests.get(BASE_URL + 'server_status.jsp', verify=CERT)

Categories

Resources