How to solve SSLCertVerificationError in Python - python

Whenever am calling an API from python am getting error :
'self._sslobj.do_handshake()
ss.SSLCertVerificationError:[SSL:CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get Local issuer certificate.
During handling of the above exception, another exception occurred:'
Here is the code:
import requests
url = 'some URL'
response = requests.request('GET', url, verify=False)
print(response.text.encode('utf8'))
The same I am doing from postman initially I got error as 'No required SSL Certificates was sent', later on I have added the certificate to Postman then am able to hit the API and successfully getting the response.
Could anyone please what I need to do to overcome the error.

Related

Certificates won't work in Python requests

I am trying to create a little Python script to send data to a server using the requests module in Python. To make it a bit more secure i want to use self signed certificates made in a program called XCA. When using the certificates in the browser everything works and is secure. When using Postman to send a request with the certificates it works as well. But when i created the Python script it seems to not work or can't get to the certificate.
I have tried to include the CA with the 'Verify' command as well as adding the certificates with the 'cert' command.
When using the 'Verify' command (as seen down below) with the CA i get the error message: Remote end closed connection without response' This message seems to appear everytime i add the CA to this script somehow.
When i use the cert command i get this error message: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)'))). I have searched for a sollution to this problem and it seemed that the CA should be include in the certifi 'cacert.pem' file. I have done this but then i also get the 'Remote disconect' message.
Code with 'verify':
import requests
import json
url = "https://ip/tapi"
payload = json.dumps ({"command" : "GetUserList"})
headers = {'content-type': 'application/json',
'X-TAPI': '',
'Authorization': 'Basic ',
'connection': 'keep-alive'}
r = requests.request("POST", url, headers=headers, data= payload, verify= 'Tbox_CA.crt')
print(r.text)
Code with 'cert':
import requests
import json
cert_file_path = 'HTTPS_client.crt'
key_file_path = 'HTTPS_client_key.pem'
url = "https://ip/tapi"
payload = json.dumps ({"command" : "GetUserList"})
headers = {'content-type': 'application/json',
'X-TAPI': '',
'Authorization': 'Basic ',
'connection': 'keep-alive'}
cert = cert_file_path, key_file_path
r = requests.request("POST", url, headers=headers, data= payload, cert=cert)
print(r.text)
When using the 'Verify' command (as seen down below) with the CA i get the error message: Remote end closed connection without response' This message seems to appear everytime i add the CA to this script somehow.
Which means that SSL works, the HTTP request was sent, but then the server closes the connection without sending a response. SSL is not the problem here and the certificate validation worked.
When i use the cert command i get this error message: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:997)'))).
The cert argument is not useful here since it is about providing a client certificate if the server requests it. Only, your server does not request a client certificate so providing this argument is basically the same as not providing it. Since no useful CA is given the certificate validation works. Since SSL thus already failed it cannot even send the real HTTP request inside the SSL connection, i.e. the error you've received when giving a CA is just masked because this error comes earlier.
Why the request failed from Python but not from the browser or Postman is unknown. There might be some server side bot protection implemented, but this is just a guess since there is nothing known about the actual URL you access.

Zeep - invalid security for wsse password type

We are using "BasicHTTPBinding WSS Type - PasswordText" authentication. I can successfully send the request manually using SoapUi tool, but in Zeep below code throws 500 internal server error and the response is
An error occurred when verifying security for the message.
headers = {'content-type': 'text/xml'}
url = r'https://xxxxxxxxxxxxxxxx/Services/LoansIntegrationService.svc?singleWsdl'
client = Client(url ,wsse=UsernameToken('vvasu', 'demo'))
body = client.transport.load(r'.\wsdl\LoanRequest.wsdl') #fetch the updated wsdl file and pass it to request
result = client.transport.post(message=body, address=url, headers=headers)
print(result.content) #An error occurred when verifying security for the message.
Response:
<s:Body>
<s:Fault>
<faultcode xmlns:a="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">a:InvalidSecurity</faultcode>
<faultstring xml:lang="en-US">An error occurred when verifying security for the message.</faultstring>
</s:Fault>
</s:Body>
</s:Envelope>```

SSL: CERTIFICATE_VERIFY_FAILED trying to validate a reCAPTCHA with django

I'm getting a
<urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:852)>
When I try to validate captchas in my django project.
This is how i do it:
recaptcha_response = request.POST.get('g-recaptcha-response')
print(recaptcha_response)
url = 'https://www.google.com/recaptcha/api/siteverify'
values = {
'secret': settings.CAPTCHA_SECRET_KEY,
'response': recaptcha_response
}
data = urllib.parse.urlencode(values).encode()
req = urllib.request.Request(url, data=data)
response = urllib.request.urlopen(req) # It fails here
result = json.loads(response.read().decode())
print(result)
The site has a valid certificate, and it works on local. In the log i'm getting this:
Request Method: POST
Request URL: http://prod.xxxx.com/evalua
Which is weird because the site works in https. Its on kubernetes, could that be the problem? I really have no idea what the problem IS? I have the captcha keys correctly set up in de recaptcha admin console. And the certificate are not autosign. I use lets encrypt
Check how you build the container image for your app and if it has very old CA certificates in it. You can use something like ADD https://curl.haxx.se/ca/cacert.pem /etc/ssl/certs/cacert.pem to ensure you have the latest standard bundle. You can also switch to Requests and Certifi instead of urllib, as that embeds a copy of the current cert bundle and ensures it is used.

Graphql : Getting HTTPError : 403 Client error

While parsing GraphQL URL " http://swapi.graphene-python.org/graphql" and trying to get attributes for "myFavouriteFilm", experiencing "requests.exceptions.HTTPError: 403 Client Error: Forbidden for url: http://swapi.graphene-python.org/graphql" error.
from gql import Client, gql
from gql.transport.requests import RequestsHTTPTransport
client = Client(
transport=RequestsHTTPTransport(url='http://swapi.graphene-python.org/graphql')
)
query = gql('''
{
myFavoriteFilm: film(id:"RmlsbToz") {
id
title
episodeId
}
}
''')
print(client.execute(query))
Sometimes, for some other GraphQL query, getting requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:726)
When I try to mention "SSL.verify=False", it throws "Syntax error"
So, not able to figure-out, is it something to do with my configuration or coding syntax issue.
Any help will be appreciated.

python SSL error when using request

I need to write a simple test script for rest get using python. What I have is:
import request
url = 'http://myurl......net'
headers ={'content-type':'application/xml'}
r= requests.get(url,headers=headers)
so this give me the following SSL error:
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed(_ssl.c:590)
So, i did some research and add " verify = False" to the end of my last line of code, but not I am stuck with: : InsecureRequestsWarning, Unverified request is been made. Addig certificate verification is strongly advised."
What to do to get rid of this message?

Categories