gpt4 book ai didi

python - 使用 python 3.5 和 urllib 禁用 SSL 身份验证

转载 作者:太空宇宙 更新时间:2023-11-03 14:42:58 25 4
gpt4 key购买 nike

我正在尝试从此服务器下载天气数据: https://goldsmr4.sci.gsfc.nasa.gov/opendap/MERRA2/M2T1NXSLV.5.12.4/

我想使用这里发布的脚本: https://github.com/Open-Power-System-Data/weather_data/blob/master/opendap_download/multi_processing_download.py

服务器的SSL证书好像有问题:

CertificateError: hostname 'goldsmr4.sci.gsfc.nasa.gov' doesn't match either of
'*.gesdisc.eosdis.nasa.gov', 'gesdisc.eosdis.nasa.gov'

如何禁用 SSL 验证?

我想我必须在这里更改一些参数:

def __create_authenticated_sesseion(self):
s = requests.Session()
s.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.85 Safari/537.36'}
s.auth = (self.__username, self.__password)
s.cookies = self.__authorize_cookies_with_urllib()

或在这里:

def __authorize_cookies_with_urllib(self):
username = self.__username
password = self.__password
top_level_url = "https://urs.earthdata.nasa.gov"


# create an authorization handler
p = urllib.request.HTTPPasswordMgrWithDefaultRealm()
p.add_password(None, top_level_url, username, password);

auth_handler = urllib.request.HTTPBasicAuthHandler(p)
auth_cookie_jar = cookiejar.CookieJar()
cookie_jar = urllib.request.HTTPCookieProcessor(auth_cookie_jar)
opener = urllib.request.build_opener(auth_handler, cookie_jar)

urllib.request.install_opener(opener)

或在这里:

def __download_and_save_file(self, url, file_path):
r = self._authenticated_session.get(url, stream=True)
with open(file_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
return r.status_code

非常欢迎任何帮助!谢谢。

最佳答案

我结合这两个答案解决了这个问题: https://stackoverflow.com/a/33770290/7105351禁用 SSL 身份验证和 https://stackoverflow.com/a/26236748/7105351把它放进开瓶器。

现在我的代码看起来像这样并且可以正常工作:

def __authorize_cookies_with_urllib(self):
username = self.__username
password = self.__password
top_level_url = "https://urs.earthdata.nasa.gov"

#Create new ssl context
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
httpsHandler = urllib.request.HTTPSHandler(context = ctx)

# create an authorization handler
p = urllib.request.HTTPPasswordMgrWithDefaultRealm()
p.add_password(None, top_level_url, username, password);

auth_handler = urllib.request.HTTPBasicAuthHandler(p)
auth_cookie_jar = cookiejar.CookieJar()
cookie_jar = urllib.request.HTTPCookieProcessor(auth_cookie_jar)
opener = urllib.request.build_opener(auth_handler, cookie_jar, httpsHandler)

urllib.request.install_opener(opener)

def __download_and_save_file(self, url, file_path):
r = self._authenticated_session.get(url, stream=True, verify = False)
with open(file_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
return r.status_code

关于python - 使用 python 3.5 和 urllib 禁用 SSL 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41700105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com