Как обработать SSLError в API геокодирования? - PullRequest
0 голосов
/ 25 октября 2019

Я использую платный API геокодирования GCP, чтобы получить широту и долготу около 1 миллиона адресов. Хотя это очень медленно, но я могу подождать. Проблема в том, что я получаю SSLError, иногда после 15000 запросов, а иногда после 6000 запросов или около того. Я использую Python 2.7, и я пытался поймать ошибку, но она не устранена.

Ошибка:

SSLErrorTraceback (most recent call last)
<ipython-input-2-7dd6c7aa3195> in <module>()
     14         count=0
     15     try:
---> 16         location2 = geocoder2.geocode(row,timeout=1,components={"country": "PK","locality":"Sindh"})
     17         if location2:
     18             print(count2,location2.latitude,location2.longitude)

/home/prassani/prassani/local/lib/python2.7/site-packages/geopy/geocoders/googlev3.pyc in geocode(self, query, exactly_one, timeout, bounds, region, components, place_id, language, sensor)
    271         logger.debug("%s.geocode: %s", self.__class__.__name__, url)
    272         return self._parse_json(
--> 273             self._call_geocoder(url, timeout=timeout), exactly_one
    274         )
    275 

/home/prassani/prassani/local/lib/python2.7/site-packages/geopy/geocoders/base.pyc in _call_geocoder(self, url, timeout, raw, requester, deserializer, **kwargs)
    398             return page
    399 
--> 400         page = decode_page(page)
    401 
    402         if deserializer is not None:

/home/prassani/prassani/local/lib/python2.7/site-packages/geopy/util.pyc in decode_page(page)
     50         else:
     51             encoding = page.headers.getparam("charset") or "utf-8"
---> 52         return text_type(page.read(), encoding=encoding)
     53     else:  # requests?
     54         encoding = page.headers.get("charset") or "utf-8"

/usr/lib/python2.7/socket.pyc in read(self, size)
    353             while True:
    354                 try:
--> 355                     data = self._sock.recv(rbufsize)
    356                 except error, e:
    357                     if e.args[0] == EINTR:

/usr/lib/python2.7/httplib.pyc in read(self, amt)
    605         # connection, and the user is reading more bytes than will be provided
    606         # (for example, reading in 1k chunks)
--> 607         s = self.fp.read(amt)
    608         if not s and amt:
    609             # Ideally, we would raise IncompleteRead if the content-length

/usr/lib/python2.7/socket.pyc in read(self, size)
    382                 # fragmentation issues on many platforms.
    383                 try:
--> 384                     data = self._sock.recv(left)
    385                 except error, e:
    386                     if e.args[0] == EINTR:

/usr/lib/python2.7/ssl.pyc in recv(self, buflen, flags)
    770                     "non-zero flags not allowed in calls to recv() on %s" %
    771                     self.__class__)
--> 772             return self.read(buflen)
    773         else:
    774             return self._sock.recv(buflen, flags)

/usr/lib/python2.7/ssl.pyc in read(self, len, buffer)
    657                 v = self._sslobj.read(len, buffer)
    658             else:
--> 659                 v = self._sslobj.read(len)
    660             return v
    661         except SSLError as x:

SSLError: ('The read operation timed out',)

Код, который я использую:

from geopy.geocoders import GoogleV3
from geopy.exc import GeocoderServiceError, GeocoderTimedOut
from requests.exceptions import SSLError
import time
import pandas as pd

df=pd.read_csv("./sat-data.csv")
df['Address']=''
df['Location']=''

geocoder2 = GoogleV3(api_key="somekey")
count=0
count2=0

for index, row in df.School_Address.iteritems():
    count+=1
    count2+=1
    if count ==4900:
        time.sleep(100)
        count=0
    try:
        location2 = geocoder2.geocode(row,timeout=1,components={"country": "PK","locality":"Sindh"})
        if location2:
            print(count2,location2.latitude,location2.longitude)
            df.Address.loc[index]=location2.address
            df.Location.loc[index]=(location2.latitude, location2.longitude)
        else:
            df.Address.loc[index]="None"
            df.Location.loc[index]="None"
    except (GeocoderServiceError, GeocoderTimedOut, SSLError), e:
        print("Error: geocode failed on input")

df.to_csv('./sat-data-new.csv', index=False)

Любая помощь будет высоко оценена.

...