反向地理编码 - geopy.Nominatim 模块抛出 urlopen 错误 [SSL: UNKNOWN PROTOCOL]

Reverse gecoding - geopy.Nominatim module throws urlopen error [SSL: UNKNOWN PROTOCOL]

我正在尝试使用 geopy.Nominatim 模块从纬度和经度获取地址详细信息。我收到“<'urlopen 错误 [SSL: UNKNOWN_PROTOCOL] 未知协议 (_ssl.c:727)>”错误。

Version Details :
Python version : 2.7
geopy version : 1.23.0
geographiclib : 1.50  (Dependency with geopy)
requests : 2.25.1
chardet : 3.0.4 (Dependency with requests)
urllib3 : 1.25.10 (Dependency with requests)
idna : 2.10 (Dependency with requests)
certifi : 2020.6.20 (Dependency with requests)
Code:
=====
from geopy.geocoders.osm import Nominatim
from geopy.exc import GeocoderServiceError
 

def reverse(lat,long):

    app = Nominatim(user_agent='reverse-geocoding')
    coordinates = "{},{}".format(lat,long) # not giving the actual co-ordinates
    try:
        address_details = app.reverse(coordinates,language="en").raw
        return address_details
    except GeocoderServiceError as e1:
        print (str(e1))

result = reverse(lat,long)
print(result)

================ 我使用了以下变通方法和相同的 script.Am 得到“<'urlopen error [SSL: UNKNOWN_PROTOCOL] unknown protocol (_ssl.c:727)>” error

解决方法 1:使用请求库使用的 CA 包:

from geopy.geocoders.osm import Nominatim
from geopy.geocoders import options
from geopy.exc import GeocoderServiceError
import ssl
import certifi

def reverse(lat,long):
    ctx = ssl.create_default_context(cafile=certifi.where())
    options.default_ssl_context = ctx
    app = Nominatim(user_agent='reverse-geocoding')
    coordinates = "{},{}".format(lat,long) # not giving the actual co-ordinates
    try:
        address_details = app.reverse(coordinates,language="en").raw
        return address_details
    except GeocoderServiceError as e1:
        print (str(e1))

result = reverse(lat,long)
print(result)

解决方法 2:完全禁用 TLS 证书验证:

from geopy.geocoders.osm import Nominatim
from geopy.geocoders import options
from geopy.exc import GeocoderServiceError
import ssl


def reverse(lat,long):
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    options.default_ssl_context = ctx
    app = Nominatim(user_agent='reverse-geocoding')
    coordinates = "{},{}".format(lat,long) # not giving the actual co-ordinates
    try:
        address_details = app.reverse(coordinates,language="en").raw
        return address_details
    except GeocoderServiceError as e1:
        print (str(e1))

result = reverse(lat,long)
print(result)

谁能帮我解决这个问题。

错误 UNKNOWN PROTOCOL 很可能是由于您的请求是通过代理进行的。

我查看了您在 Workaround 2 中提到的代码。请在您的代码中明确提及代理。尝试使用以下代码行:

ctx = ssl.create_default.context()
ctx.check_hostname = True
ctx.verify_mode = ssl.CERT_REQUIRED
options.default_ssl_context = ctx
proxies = {'https':'https://your-proxy-server.com:port'}
app = Nominatim(user_agent="your-agent", proxies=proxies, timeout=10)

这也将帮助您不要完全忽略 SSL 验证。