0

I'm relatively new to Python. Trying to write a function in Python that will calculate the distance between locations using the Google Distance Matrix API. I actually have used this code successfully in the past, but now I am getting the following error and can't figure out what is going on.

IOError: [Errno socket error] [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)

I know the issue isn't with the URL because I can type it in a browser, using my API key, and it gives me the result I'm looking for (i.e., distance info from Google Maps Distance Matrix API). It seems to be an issue with the urllib.urlopen function call. I assume I'm calling it incorrectly somehow, but I can't figure out how. Any ideas?

I'm using Python version 2.7.10.

import urllib
import re

def getMiles(origin,destination):
    #APIkey = "someAPIkey"  #Update to prompt user for API key
    query = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&origins=" + origin + "&destinations=" + destination + "&key=" + APIkey
    test = 0

fhand = urllib.urlopen(query)  #CODE ERRORS OUT HERE
#Error handling to ensure query works
try:
    for line in fhand:
        line = str(line.strip())
        #print line
        if test == 1:
            line = line.replace('"text" : "',"")
            line = line.replace('",',"")
            result = [origin,'"'+destination+'"'] + line.split()
            break
        if line.startswith('"distance"'):
            test = 1
    return result
except:
    result = [origin,'"'+destination+'"',"ERROR","Query could not be opened"]
    return result
todd w
  • 529
  • 5
  • 4
  • See here: http://stackoverflow.com/questions/19268548/python-ignore-certicate-validation-urllib2 – Alex L Feb 05 '17 at 02:45
  • The answer says it's evil and not to run in a production environment. What does this mean? I'm just doing this for an analysis and not putting any of this into any software... I assume I'm good to use this code? – todd w Feb 05 '17 at 02:55
  • It seems to work for me (ie when I remove the try/except lines): { "destination_addresses" : [], "error_message" : "The provided API key is invalid.", "origin_addresses" : [], "rows" : [], "status" : "REQUEST_DENIED" } coming from google – Amorpheuses Feb 05 '17 at 03:37
  • I added the code block starting with ctx = ssl.create_default_context(), and also updated my code to use urllib2 instead, but now I am getting a HTTPError: HTTP Error 400: Bad Request I am not sure what this means because if I print the URL out before the error is thrown, I can copy and paste it into a browser and it resolves. What am I missing? – todd w Feb 05 '17 at 03:40
  • A common cause of a URL working in the browser but not in the script is that the browser is using either a proxy to connect to the target and the script not or that there is legal SSL interception going on using a corporate firewall or desktop AV/firewall and that the browser has included the needed proxy CA in the trust store while the script has not since the browser and Python use different trust stores. But there are not enough information in your question to know which if any of these is the case. – Steffen Ullrich Feb 05 '17 at 05:42

1 Answers1

0

Here is the solution that worked for me, add the following piece of code in your program and it should fix your error, mate.

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

bsikriwal
  • 178
  • 3
  • 15