I have written some Python code for an azure function and everything works fine when I execute it locally in VS code through Azure Functions Core Tools. The code calls a REST API.
When I deploy it to azure it fails with the following error, any idea on how to debug this?
Result: Failure Exception: SSLError: HTTPSConnectionPool(host='api.myurl.net', port=443): Max retries exceeded with url: /payments/123456 (Caused by SSLError(SSLError(1, '[SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:1125)'))) Stack: File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 355, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.8/concurrent/futures/thread.py", line 57, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.8/LINUX/X64/azure_functions_worker/dispatcher.py", line 542, in __run_sync_func return func(**params) File "/home/site/wwwroot/HttpTrigger1/__init__.py", line 15, in main status, body, headers = client.get('/payments/234368493',raw=True) File "/home/site/wwwroot/.python_packages/lib/site-packages/quickpay_api_client/api.py", line 80, in perform response = self.fulfill(method, url, File "/home/site/wwwroot/.python_packages/lib/site-packages/quickpay_api_client/api.py", line 44, in fulfill return getattr(self.session, method)(*args, **kwargs) File "/home/site/wwwroot/.python_packages/lib/site-packages/requests/sessions.py", line 555, in get return self.request('GET', url, **kwargs) File "/home/site/wwwroot/.python_packages/lib/site-packages/requests/sessions.py", line 542, in request resp = self.send(prep, **send_kwargs) File "/home/site/wwwroot/.python_packages/lib/site-packages/requests/sessions.py", line 655, in send r = adapter.send(request, **kwargs) File "/home/site/wwwroot/.python_packages/lib/site-packages/requests/adapters.py", line 514, in send raise SSLError(e, request=request)
The problem was in the code of one of the installed python modules. The module used the poolmanager and I suspect the problem was in that. I rewrote the code and now it's working.
Your problem is caused by your certificate is not trusted, you can try to use the following ways to solve it.
1. You can refer to SSLError (bad handshake) when using Azure CLI to get the trusted certificate, you can get the trusted certificate by the URL mentioned by the error message in a browser.
2. You can try to disable certificate verification, please refer to SSL handshake error with some Azure CLI commands:
set ADAL_PYTHON_SSL_NO_VERIFY=1
set AZURE_CLI_DISABLE_CONNECTION_VERIFICATION=1
Related
I have successfully been using the gTTS module in order to get audio from Google Translate for a while. I use it quite sparsely (I must have made 25 requests in total), and don't believe I could have hit any kind of limit that would cause my address to be blocked from using the service.
However, today, after trying to use it (I haven't used it in 1-2 months), I got the following program:
from gtts import gTTS
tts = gTTS('hallo', 'de')
tts.save('hallo.mp3')
To cause an error. I tracked down the problem, and I managed to see that even this simple program:
import requests
response = requests.get("https://translate.google.com/")
Causes the following error:
Traceback (most recent call last):
File "C:\...\lib\site-packages\urllib3\connectionpool.py", line 601, in urlopen
chunked=chunked)
File "C:\...\lib\site-packages\urllib3\connectionpool.py", line 346, in _make_request
self._validate_conn(conn)
File "C:\...\lib\site-packages\urllib3\connectionpool.py", line 850, in _validate_conn
conn.connect()
File "C:\...\lib\site-packages\urllib3\connection.py", line 326, in connect
ssl_context=context)
File "C:\...\lib\site-packages\urllib3\util\ssl_.py", line 329, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "C:\...\lib\ssl.py", line 407, in wrap_socket
_context=self, _session=session)
File "C:\...\lib\ssl.py", line 814, in __init__
self.do_handshake()
File "C:\...\lib\ssl.py", line 1068, in do_handshake
self._sslobj.do_handshake()
File "C:\...\lib\ssl.py", line 689, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:777)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\...\lib\site-packages\requests\adapters.py", line 440, in send
timeout=timeout
File "C:\...\lib\site-packages\urllib3\connectionpool.py", line 639, in urlopen
_stacktrace=sys.exc_info()[2])
File "C:\...\lib\site-packages\urllib3\util\retry.py", line 388, 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(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:777)'),))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main2.py", line 2, in <module>
response = requests.get("https://translate.google.com/")
File "C:\...\lib\site-packages\requests\api.py", line 72, in get
return request('get', url, params=params, **kwargs)
File "C:\...\lib\site-packages\requests\api.py", line 58, in request
return session.request(method=method, url=url, **kwargs)
File "C:\...\lib\site-packages\requests\sessions.py", line 508, in request
resp = self.send(prep, **send_kwargs)
File "C:\...\lib\site-packages\requests\sessions.py", line 618, in send
r = adapter.send(request, **kwargs)
File "C:\...\lib\site-packages\requests\adapters.py", line 506, 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(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:777)'),))
I would like to know if anyone has an idea what the issue could be. I can get on the Google Translate website without any problems from my browser, and have no issues using the audio either.
Accepted answer did not work for me since the code has changed, the way i got it to work was to add verify=False in gtts_token.py instead
response = requests.get("https://translate.google.com/", verify=False)
This looks like an error related to your proxy setting, especially if you are using your work PC. I have got the same issue, but different error message, for example:
gTTSError: Connection error during token calculation:
HTTPSConnectionPool(host='translate.google.com', port=443): Max
retries exceeded with url: / (Caused by SSLError(SSLError("bad
handshake: Error([('SSL routines', 'ssl3_get_server_certificate',
'certificate verify failed')],)",),))
To further investigate the issue, you can debug it in the command line.
(base) c:\gtts-cli "sample text to debug" --debug --output test.mp3
you should see results as below;
ProxyError('Cannot connect to proxy.', OSError('Tunnel connection failed: 407 Proxy Authentication Required',)))
Solution:
I have checked the gTTs documentation, there is no way to pass your proxy setting to the api. so the work around is ignore the ssl verification, which in not available also in gTTs. so the only way to do it is to change the following gtts files:
tts.py, in line 208 chage the request function to add verifiy=false
r = requests.get(self.GOOGLE_TTS_URL,
params=payload,
headers=self.GOOGLE_TTS_HEADERS,
proxies=urllib.request.getproxies(),
verify=False)
file lang.py, line 56
page = requests.get(URL_BASE, verify=False)
Then, try again the debug command line. you should be able to get the file recorded now
(base) c:\gtts-cli "sample text to debug" --debug --output test.mp3
gtts.tts - DEBUG - status-0: 200
gtts.tts - DEBUG - part-0 written to <_io.BufferedWriter name=test.mp3'>
I downloaded Google Cloud SDK by following https://cloud.google.com/sdk/docs/quickstart-debian-ubuntu .
And I downloaded Google Speech API by following
https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/speech/cloud-client .
When I run python transcribe.py resources/audio.raw,
the error happens.
I got an error,TransportError: HTTPSConnectionPool(host='accounts.google.com', port=443): Max retries exceeded with url: /o/oauth2/token (Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)'),))
)>)
Traceback is
E1019 18:46:20.868555489 3813 plugin_credentials.c:74] Getting metadata from plugin failed with error: Traceback (most recent call last):
File "src/python/grpcio/grpc/_cython/_cygrpc/credentials.pyx.pxi", line 154, in grpc._cython.cygrpc.plugin_get_metadata (src/python/grpcio/grpc/_cython/cygrpc.c:7054)
File "/home/pi/py2env/local/lib/python2.7/site-packages/grpc/_plugin_wrapping.py", line 106, in __call__
AuthMetadataPluginCallback(wrapped_cygrpc_callback))
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/auth/transport/grpc.py", line 73, in __call__
callback(self._get_authorization_headers(context), None)
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/auth/transport/grpc.py", line 61, in _get_authorization_headers
headers)
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/auth/credentials.py", line 121, in before_request
self.refresh(request)
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/oauth2/credentials.py", line 117, in refresh
self._client_secret))
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/oauth2/_client.py", line 189, in refresh_grant
response_data = _token_endpoint_request(request, token_uri, body)
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/oauth2/_client.py", line 104, in _token_endpoint_request
method='POST', url=token_uri, headers=headers, body=body)
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/auth/transport/requests.py", line 115, in __call__
raise exceptions.TransportError(exc)
TransportError: HTTPSConnectionPool(host='accounts.google.com', port=443): Max retries exceeded with url: /o/oauth2/token (Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)'),))
Traceback (most recent call last):
File "transcribe.py", line 94, in <module>
transcribe_file(args.path)
File "transcribe.py", line 52, in transcribe_file
response = client.recognize(config, audio)
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/cloud/gapic/speech/v1/speech_client.py", line 201, in recognize
return self._recognize(request, options)
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/gax/api_callable.py", line 452, in inner
return api_caller(api_call, this_settings, request)
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/gax/api_callable.py", line 438, in base_caller
return api_call(*args)
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/gax/api_callable.py", line 376, in inner
return a_func(*args, **kwargs)
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/gax/retry.py", line 127, in inner
' classified as transient', exception)
google.gax.errors.RetryError: RetryError(Exception occurred in retry method that was not classified as transient, caused by <_Rendezvous of RPC that terminated with (StatusCode.UNAUTHENTICATED, Traceback (most recent call last):
File "src/python/grpcio/grpc/_cython/_cygrpc/credentials.pyx.pxi", line 154, in grpc._cython.cygrpc.plugin_get_metadata (src/python/grpcio/grpc/_cython/cygrpc.c:7054)
File "/home/pi/py2env/local/lib/python2.7/site-packages/grpc/_plugin_wrapping.py", line 106, in __call__
AuthMetadataPluginCallback(wrapped_cygrpc_callback))
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/auth/transport/grpc.py", line 73, in __call__
callback(self._get_authorization_headers(context), None)
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/auth/transport/grpc.py", line 61, in _get_authorization_headers
headers)
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/auth/credentials.py", line 121, in before_request
self.refresh(request)
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/oauth2/credentials.py", line 117, in refresh
self._client_secret))
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/oauth2/_client.py", line 189, in refresh_grant
response_data = _token_endpoint_request(request, token_uri, body)
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/oauth2/_client.py", line 104, in _token_endpoint_request
method='POST', url=token_uri, headers=headers, body=body)
File "/home/pi/py2env/local/lib/python2.7/site-packages/google/auth/transport/requests.py", line 115, in __call__
raise exceptions.TransportError(exc)
TransportError: HTTPSConnectionPool(host='accounts.google.com', port=443): Max retries exceeded with url: /o/oauth2/token (Caused by SSLError(SSLError(1, u'[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)'),))
)>)
I installed module can be used https to ubuntu,so I really cannnot understand why this error happens.Authentication of Google Cloud API is ok,so I do not know this is for what kind of error.How should I fix this?
It looks a grpcio issue that's causing the error. I suggest installing the latest version the Cloud Speech API client library (which specifies its required version of grpcio) and try again. Rather than downloading it from GH, I suggest using pip install instead.
"Hello World" s2t-demo.py example (Py2&3 compatible):
from __future__ import print_function
from google.cloud import speech
# Py2 vs. 3 library compatibility
recogcfg = speech.RecognitionConfig if hasattr(
speech, 'RecognitionConfig') else speech.enums.RecognitionConfig
# build request metadata
FILE = 'gs://YOUR_BUCKET/AUDIO_FILE.wav'
body = {
'audio': {'uri': FILE},
'config': {
'language_code': 'en-US',
'encoding': recogcfg.AudioEncoding.LINEAR16,
},
}
# call Speech-to-Text API to transcribe text from specified audio file
S2T = speech.SpeechClient()
rsp = S2T.recognize(**body).results[0].alternatives[0]
print('** %.2f%% confident of this transcript of %r:\n\t%r' % (
rsp.confidence*100., FILE, rsp.transcript))
Recommended steps (recommend using a virtualenv):
gcloud auth application-default login - setup credentials
pip install -U pip google-cloud-speech - update pip and install client library
pip list - confirm google-cloud-speech v1.3.4+ and grpcio 1.41.1+
gsutil cp AUDIO_FILE.wav gs://YOUR_BUCKET - copy audio file to Cloud Storage
python s2t-demo.py - run app and see audio file transcription
Example output:
$ python s2t-demo.py
** 93.05% confident of this transcript of 'gs://bucket/text.wav':
'Google headquarters in Mountain View unveiled the new Android phone at the Consumer Electronics Show Sundar pichai said in his keynote that users love their new Android phones'
The code sample works for both Python 2 and 3 in case you want to upgrade sooner than later.
I am getting an SSL "bad handshake" error when using a python script with requests to log in to https://selfserve.publicmobile.ca/Overview/
Here's the error:
Traceback (most recent call last):
File "/home/pi/Documents/repos/private-repos/public-mobile-usage-scraping/pm_usage_scraping_to_db.py", line 55, in <module>
r = s.get(URL)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 526, in get
return self.request('GET', url, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 513, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 623, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 514, in send
raise SSLError(e, request=request)
SSLError: ("bad handshake: Error([('SSL routines', 'SSL3_GET_SERVER_CERTIFICATE', 'certificate verify failed')],)",)
I know that a workaround is setting verify=False but will leave me vulnerable, especially since I want to send my username and password over this.
From my research, it seems like the website is missing an intermediate certificate (see https://www.ssllabs.com/ssltest/analyze.html?d=selfserve.publicmobile.ca).
Is that true? Where/how do I find the intermediate certificate to add it to my code?
Alternatively, how bad is it if I make a non-SSL request to that site with my account credentials, considering I am only doing that from my home network?
I am new to python and web scraping, and have only a rudimentary of SSL and web security. Thank you so much for help!
s.get(***, verify=False) you try, cancal verify
I'm trying to do a simple post request, I'm using a list because I want to send all my post request at the same time using thread. Here is an example of an url :
s = "https://emoncms.org/input/post.json?node="+str(test)+"&json={test_stack_overflow:0}&apikey="+str(apikey)
list.append(threading.Thread(target=requests.post, args=([s, ])))
I was using this code maybe 3 months ago and it worked perfectly.
I wanted to get back on this project this week and I realized that I got some errors, this one particularly :
Exception in thread Thread-14:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 763, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 94, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 49, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 457, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 569, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 420, in send
raise SSLError(e, request=request)
SSLError: <unprintable SSLError object>
I got an other error, ConnectionError but I think it's due to the network or because the website can't follow it's activity or is down. I leave you the traceback if you want :
ConnectionError: ('Connection aborted.', error(101, 'Network is unreachable'))
This code is only a part of my project, the code is running every minutes and I don't know why but this issue (SSLError) comes only maybe 10 times a day. I got this script running on different Raspberry Pi and some have the same problem but not the same frequency, others don't have it at all.
Any ideas on what is going ?
Thanks in advance !
Use verify=False in the requests method like this
import requests
url="https://emoncms.org/input/post.json?node="+str(test)+"&json={test_stack_overflow:0}&apikey="+str(apikey)
requests.post(url,verify=False)
If you are using with threads then it will be like
list.append(threading.Thread(target=requests.post, args=(url,),kwargs={"verify":False})) #**kwargs should be passed seperately.
You are getting this error because python requests tries to verify certificate for https connections so you have to override it by passing verify=False or you can also provide certificate in verify like this requests.get(url,verify="/path/to/certificate.ext")
Also I doubt that this should be a get request because query parameters won't come in post request as of my knowledge. So if you use GET method same verify applies there too.
I'm trying to setup a WebDAV connection using easywebdav in Python. (Using 2.7.8 for now)
import csv, easywebdav
webdav=easywebdav.connect('https://sakai.rutgers.edu/dav/restoftheurl,username="",password="")
print webdav.ls()
Though when I run this I get the following error message. My guess is that it possibly has something to do with the URL using HTTPS?
Traceback (most recent call last):
File "/home/willkara/Development/SakaiStuff/WorkProjects/sakai-manager/file.py", line 4, in <module>
print webdav.ls()
File "build/bdist.linux-x86_64/egg/easywebdav/client.py", line 176, in ls
File "build/bdist.linux-x86_64/egg/easywebdav/client.py", line 97, in _send
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 456, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 559, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 375, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='https', port=80): Max retries exceeded with url: //sakai.rutgers.edu/dav/url:80/. (Caused by <class 'socket.gaierror'>: [Errno -2] Name or service not known)
[Finished in 0.1s with exit code 1]
I find it strange that you combine HTTPS protocol and port 80. HTTPS uses port 443.
Though the error message "Name or service not known" would rather indicate that the hostname sakai.rutgers.edu is not recognized on your system. Try to ping the host.
I noticed that you shouldn't have http:// or https:// in the beginning of your adress, only the host name. You select protocol with protocol='https'. Also, I couln't get it to work if I added the path the url, I had to use it as argument to the operations like easywebdav.ls('/dav/restoftheurl') or easywebdav.cd('/dav/restoftheurl').