我使用Python 3.6.8(适用于Android)编写的应用程序,当从urllib.request(Python3)访问网页上托管的URL的JSON文件时,该错误会从移动设备返回此错误。
这是错误:
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed:
unable to get local issuer certificate (_ssl.c: 1051)
在Windows(W7)和Linux(ubuntu)上从桌面运行脚本,我没有此错误,并且可以完美地访问它。我一直在网上寻找可能的解决方案,但现在什么也没有。有人知道如何解决吗?
我不明白为什么我可以在Windows和Linux上而不是在移动设备(Android)上运行脚本。我需要在脚本中添加一些代码吗?谢谢
main.py
import socket
import ssl
import urllib.request # Para Python3
import urllib.error
from urllib.request import urlopen
class Api(Screen):
data_json = StringProperty("")
def data_api(self):
try:
url = "https://...../data.json"
request_json = urllib.request.Request(
url,
data=None,
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
}
)
context = ssl.SSLContext()
response = urllib.request.urlopen(request_json, context=context)
if response.getcode() == 401:
print("Acces:", str(response.getcode()))
if response.getcode() == 403:
print("Acces:", str(response.getcode()))
if response.getcode() == 200:
print("Access:", str(response.getcode()))
data = json.loads(response.read())
self.data_json = str(data)
print(data)
except (urllib.error.HTTPError) as e:
print("Error: ", e)