【问题标题】:geocoder Service error in pythonpython中的地理编码器服务错误
【发布时间】:2023-03-18 20:16:01
【问题描述】:

我在尝试从 geopy 运行地理编码器以在 python 中获取位置坐标时遇到服务错误。我已经设置了代理配置,给它一个 api_key。 我不确定为什么会收到此错误。从我对此进行的搜索来看,这似乎是一个代理问题,但我已经设置好了。 这可能是什么问题?

这是我的代码:

from geopy import geocoders

proxies={'http': 'http://location:port', 'https': 'http://localhost:port'}
api_key = '.......'

g = geocoders.GoogleV3(api_key=api_key,proxies=proxies, timeout=10)

location = 'Mountain View, CA'
try:
    place, (lat, lng) = g.geocode(location)
except ValueError as error_message:
    print("Error: geocode failed on input %s with message %s" % (location, error_message))

这是我的错误输出:

Traceback (most recent call last):
  File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 1254, in do_open
    h.request(req.get_method(), req.selector, req.data, headers)
  File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 1106, in request
    self._send_request(method, url, body, headers)
  File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 1151, in _send_request
    self.endheaders(body)
  File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 1102, in endheaders
    self._send_output(message_body)
  File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 934, in _send_output
    self.send(msg)
  File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 877, in send
    self.connect()
  File "/Users/aqm1152/anaconda/lib/python3.5/http/client.py", line 1260, in connect
    server_hostname=server_hostname)
  File "/Users/aqm1152/anaconda/lib/python3.5/ssl.py", line 377, in wrap_socket
    _context=self)
  File "/Users/aqm1152/anaconda/lib/python3.5/ssl.py", line 752, in __init__
    self.do_handshake()
  File "/Users/aqm1152/anaconda/lib/python3.5/ssl.py", line 988, in do_handshake
    self._sslobj.do_handshake()
  File "/Users/aqm1152/anaconda/lib/python3.5/ssl.py", line 633, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:645)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/aqm1152/anaconda/lib/python3.5/site-packages/geopy/geocoders/base.py", line 143, in _call_geocoder
    page = requester(req, timeout=(timeout or self.timeout), **kwargs)
  File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 163, in urlopen
    return opener.open(url, data, timeout)
  File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 466, in open
    response = self._open(req, data)
  File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 484, in _open
    '_open', req)
  File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 444, in _call_chain
    result = func(*args)
  File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 1297, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/Users/aqm1152/anaconda/lib/python3.5/urllib/request.py", line 1256, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error EOF occurred in violation of protocol (_ssl.c:645)>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/aqm1152/Documents/TestingCode/ACERT/test_1.py", line 12, in <module>
    place, (lat, lng) = g.geocode(location)
  File "/Users/aqm1152/anaconda/lib/python3.5/site-packages/geopy/geocoders/googlev3.py", line 217, in geocode
    self._call_geocoder(url, timeout=timeout), exactly_one
  File "/Users/aqm1152/anaconda/lib/python3.5/site-packages/geopy/geocoders/base.py", line 171, in _call_geocoder
    raise GeocoderServiceError(message)
geopy.exc.GeocoderServiceError: EOF occurred in violation of protocol (_ssl.c:645)

【问题讨论】:

    标签: proxy python-3.5 google-geocoder geopy


    【解决方案1】:

    我修改了您的代码如下,因为我不使用代理并应用 API,它在我的机器上工作。

    from geopy import geocoders
    
    #proxies={'http': 'http://location:port', 'https': 'http://localhost:port'}
    #api_key = '.......'
    
    g = geocoders.GoogleV3()
    
    location = 'Mountain View, CA'
    try:
        place, (lat, lng) = g.geocode(location)
    except ValueError as error_message:
        print("Error: geocode failed on input %s with message %s" % (location, error_message))
    
    print (place, lat, lng)
    

    这是结果。

    Mountain View, CA, USA 37.3860517 -122.0838511
    

    从 trackback 看来,所有错误都与 SSL 有关。您可以尝试在开头添加以下代码以禁用 SSL 认证验证。

    import ssl 
    
    # Disable SSL certificate verification
    try:
        _create_unverified_https_context = ssl._create_unverified_context
    except AttributeError:
        # Legacy Python that doesn't verify HTTPS certificates by default
        pass
    else:
        # Handle target environment that doesn't support HTTPS verification
        ssl._create_default_https_context = _create_unverified_https_context
    

    我在运行 Python 3.6 的 env 中运行 Python 2.7 的 Anaconda 中遇到了类似的问题,而 root 运行 Python 3.6。在 Anaconda 中没有遇到此问题,rootenv 在另一台机器上运行 Python 2.7。

    另外,this SO post关于使用os模块为geopy设置代理服务器可能是很好的参考。

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 2011-07-20
      • 2011-05-19
      • 2013-08-17
      • 1970-01-01
      • 2011-10-08
      相关资源
      最近更新 更多