如何使用Googlemaps Python处理SSL握手问题

时间:2018-09-18 14:21:41

标签: python-3.x google-maps ssl

我正在尝试使用Python和googlemaps api /库对大约5k地址进行地址解析和提取路由时间/距离。我已经对此进行了详尽的研究,无法弄清楚为什么在几次成功的循环之后,循环却总是随机地中断。这是代码:

import googlemaps, numpy as np, pandas as pd, pylab as pl, os, time, requests
from datetime import datetime

def findlat(address_lat):

    gmaps = googlemaps.Client(key='XXXXXX')
    coord_result = gmaps.geocode(address_lat)
    for item in coord_result:
        lat = item['geometry']['location']
        return lat['lat']

def findlng(address_lng):
    gmaps = googlemaps.Client(key='XXXXXX')
    coord_result = gmaps.geocode(address_lng)
    for item in coord_result:
        lng = item['geometry']['location']
        return lng['lng']


address_list_run["lat"] = 0.00
address_list_run["lng"] = 0.00
for i in range(3526,5751): #range set from last break point
    try:
        source = address_list_run['Full_Address'][i]
        destination = address_list_run['dest_dest'][i]
        address_list_run['lat'][i] = findlat(source)
        address_list_run['lng'][i] = findlng(source)
        time.sleep(1)
    except (requests.exceptions.SSLError) as e: #this isn't doing anything
        time.sleep(1)
        continue

该代码将运行几行(少至5条,最多800条),然后中断下面的完整回溯。我已经阅读了几篇文章,但不知道具体为何会中断-我研究了加密技术,请求,OpenSSL,但不确定如何使用googlemaps库来忽略验证。我已经检查了herehere,但似乎无法实现有效的解决方案。我正在Windows 10上运行Python 3.6。

如何处理SSL握手错误?感谢您的帮助。

  

-------------------------------------------------- ----------------------------错误回溯(最近的呼叫   持续)   c:\ users \ User \ Appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ urllib3 \ contrib \ pyopenssl.py   在wrap_socket(自我,袜子,server_side,do_handshake_on_connect,   prevent_ragged_eofs,服务器主机名)       443尝试:   -> 444 cnx.do_handshake()       445,除了OpenSSL.SSL.WantReadError:

     

c:\ users \ User \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ OpenSSL \ SSL.py   在do_handshake()中1906结果=   _lib.SSL_do_handshake(self._ssl)   -> 1907 self._raise_ssl_error(self._ssl,结果)1908

     

c:\ users \ User \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ OpenSSL \ SSL.py   在_raise_ssl_error(self,ssl,result)1638中:   -> 1639 _raise_current_error()1640

     

c:\ users \ User \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ OpenSSL_util.py   在exception_from_error_queue(exception_type)中        53   ---> 54引发exception_type(错误)        55

     

错误:[('SSL例程','tls_process_server_certificate',   “证书验证失败”)]

     

在处理上述异常期间,发生了另一个异常:

     

SSLError跟踪(最近的呼叫)   持续)   c:\ users \ User \ Appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ urllib3 \ connectionpool.py   在urlopen(self,method,url,body,标头,重试,重定向,   assert_same_host,超时,pool_timeout,release_conn,分块,   body_pos,** response_kw)       599正文=正文,标题=标题,   -> 600分块=分块)       601

     

c:\ users \ User \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ urllib3 \ connectionpool.py   在_make_request中(自我,conn,方法,url,超时,分块,   ** httplib_request_kw)       342尝试:   -> 343 self._validate_conn(conn)       344除了(SocketTimeout,BaseSSLError)为e:

     

c:\ users \ User \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ urllib3 \ connectionpool.py   在_validate_conn(self,conn)中       848如果不是getattr(conn,'sock',None):#AppEngine可能没有.sock   -> 849 conn.connect()       850

     

c:\ users \ User \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ urllib3 \ connection.py   在连接中(个体经营)       355 server_hostname =主机名,   -> 356 ssl_context = context)       357

     

c:\ users \ User \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ urllib3 \ util \ ssl_.py   在ssl_wrap_socket中(袜子,密钥文件,certfile,cert_reqs,ca_certs,   server_hostname,ssl_version,密码,ssl_context,ca_cert_dir)       358如果HAS_SNI和server_hostname不为None:   -> 359返回context.wrap_socket(sock,server_hostname = server_hostname)       360

     

c:\ users \ User \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ urllib3 \ contrib \ pyopenssl.py   在wrap_socket(自我,袜子,server_side,do_handshake_on_connect,   prevent_ragged_eofs,服务器主机名)       449,除了OpenSSL.SSL.Error为e:   -> 450提高ssl.SSLError('握手不好:%r'%e)       451中断

     

SSLError:(“”握手不当:Error([('SSL例程',   'tls_process_server_certificate','证书验证失败')],)“,)

     

在处理上述异常期间,发生了另一个异常:

     

MaxRetryError Traceback(最近的呼叫   持续)   c:\ users \ User \ Appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ requests \ adapters.py   发送中(自己,请求,流,超时,验证,证书,代理)       444次重试= self.max_retries   -> 445超时=超时       446)

     

c:\ users \ User \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ urllib3 \ connectionpool.py   在urlopen(self,method,url,body,标头,重试,重定向,   assert_same_host,超时,pool_timeout,release_conn,分块,   body_pos,** response_kw)       637次重试= retries.increment(方法,url,错误= e,_pool =自我,   -> 638 _stacktrace = sys.exc_info()2)       639重试.sleep()

     

c:\ users \ User \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ urllib3 \ util \ retry.py   以增量(自我,方法,URL,响应,错误,_pool,_stacktrace)       397如果new_retry.is_exhausted():   -> 398引发MaxRetryError(_pool,url,error或ResponseError(cause))       399

     

MaxRetryError:HTTPSConnectionPool(host ='maps.googleapis.com',   port = 443):网址超过了最大重试次数:   / maps / api / geocode / json?address = XXXXXXXXXXXXX(由   SSLError(SSLError(“握手不好:Error([('SSL例程',   'tls_process_server_certificate','证书验证   失败')],)“,),))

     

在处理上述异常期间,发生了另一个异常:

     

SSLError跟踪(最近的呼叫)   持续)   c:\ users \ User \ Appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ googlemaps \ client.py   在_request中(自我,网址,参数,first_request_time,retry_counter,   base_url,accepts_clientid,extract_body,requests_kwargs,post_json)       235 =请求方法(base_url + authed_url,   -> 236 ** final_requests_kwargs)       第237章:requests.exceptions.Timeout:

     

c:\ users \ User \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ requests \ sessions.py   在get(self,url,** kwargs)中       第524章   -> 525 return self.request('GET',url,** kwargs)       526

     

c:\ users \ User \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ requests \ sessions.py   在请求中(自身,方法,URL,参数,数据,标头,Cookie,文件,   身份验证,超时,allow_redirects,代理,挂钩,流,验证,证书,   json)       511 send_kwargs.update(设置)   -> 512 resp = self.send(prep,** send_kwargs)       513

     

c:\ users \ User \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ requests \ sessions.py   在send(self,request,** kwargs)中       621#发送请求   -> 622 r = adapter.send(请求,** kwargs)       623

     

c:\ users \ User \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ requests \ adapters.py   发送中(自己,请求,流,超时,验证,证书,代理)       510#该分支适用于urllib3 v1.22及更高版本。   -> 511引发SSLError(e,request = request)       512

     

SSLError:HTTPSConnectionPool(host ='maps.googleapis.com',端口= 443):   url超过了最大重试次数:   / maps / api / geocode / json?address = XXXXXXXXXXXXX(由   SSLError(SSLError(“握手不好:Error([('SSL例程',   'tls_process_server_certificate','证书验证   失败')],)“,),))

     

在处理上述异常期间,发生了另一个异常:

     

TransportError跟踪(最近的呼叫   最后)在()   ---> 11 address_list_run ['lat'] [i] = findlat(源)        12 address_list_run ['lng'] [i] = findlng(源代码)        13次.sleep(1)

     在findlat(address_lat)中的

       35 def findlat(address_lat):        36个gmaps = googlemaps.Client(key = XXXXXXX)   ---> 37 coord_result = gmaps.geocode(address_lat)        对于coord_result中的项目为38:        39 lat = item ['geometry'] ['location']

     

c:\ users \ User \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ googlemaps \ client.py   在包装器中(* args,** kwargs)       363 def包装器(* args,** kwargs):       364个args [0] ._ extra_params = kwargs.pop(“ extra_params”,None)   -> 365结果= func(* args,** kwargs)       366尝试:       367 del args [0] ._ extra_params

     

c:\ users \ User \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ googlemaps \ geocoding.py   用地理编码(客户,地址,组件,范围,区域,语言)        66个params [“ language”] =语言        67   ---> 68返回client._request(“ / maps / api / geocode / json”,params).get(“ results”,[])        69        70

     

c:\ users \ User \ appdata \ local \ programs \ python \ python36 \ lib \ site-packages \ googlemaps \ client.py   在_request中(自我,网址,参数,first_request_time,retry_counter,   base_url,accepts_clientid,extract_body,requests_kwargs,post_json)       238提高googlemaps.exceptions.Timeout()       239例外,例如e:   -> 240引发googlemaps.exceptions.TransportError(e)       241       242 if _RETRIABLE_STATUSES中的response.status_code:

     

TransportError:HTTPSConnectionPool(host ='maps.googleapis.com',   port = 443):网址超过了最大重试次数:   / maps / api / geocode / json?address = XXXXXXXXXXXXXXXXXX(由   SSLError(SSLError(“握手不好:Error([('SSL例程',   'tls_process_server_certificate','证书验证   失败')],)“,),))

0 个答案:

没有答案