Nltk’s word splitter Punkt: SSL problem, unable to download

This article gives you a comprehensive understanding of Java object serialization and deserialization>>>

report errors:

LookupError:
**********************************************************************
  Resource punkt not found.
  Please use the NLTK Downloader to obtain the resource:

  >>> import nltk
  >>> nltk.download('punkt')

  Attempted to load tokenizers/punkt/english.pickle

  Searched in:
    - '/home/a/nltk_data'
    - '/home/a/anaconda3/envs/py2/nltk_data'
    - '/home/a/anaconda3/envs/py2/share/nltk_data'
    - '/home/a/anaconda3/envs/py2/lib/nltk_data'
    - '/usr/share/nltk_data'
    - '/usr/local/share/nltk_data'
    - '/usr/lib/nltk_data'
    - '/usr/local/lib/nltk_data'
    - u''
**********************************************************************

  

Solution:

import nltk
import ssl

try:
  _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
  pass
else:
  ssl._create_default_https_context = _create_unverified_https_context

nltk.download("punkt")

  


Supplementary materials:

SSLError: [SSL: CERTIFICATE_ VERIFY_ FAILED] certificate verify failed (_ ssl.c:581)

Today, I’d like to try Baidu’s speech recognition API with the following steps:

1. First go to Baidu open cloud platform to register and become a developer. It may take time to audit. I applied last year and now my account is still in use

2. Then create an application

3. Add services to the created application, including speech recognition and speech generation

4. So I have an access to call his voice recognition interface_ The token, because I use the API for rest, I need to take the API_ Key and secret_ The key is obtained through an HTTP request, and that’s the problem

I use request to post and get according to the appearance of his document, and report an error of verification failure

requests.post(‘ https://openapi.baidu.com/oauth/2.0/token?grant_ type=client_ credentials& client_ id=xxxxxxx& client_ secret=xxxxxxx’).content

requests.get(‘ https://openapi.baidu.com/oauth/2.0/token?grant_ type=client_ credentials& client_ id=xxxxxxx& client_ secret=xxxxxxx’).content

He told me:

SSLError: [SSL: CERTIFICATE_ VERIFY_ FAILED] certificate verify failed (_ ssl.c:581)

Some people say that the reason is as follows:

After Python 2.7.9, a new feature is introduced
when you use urllib.urlopen as an HTTPS, you will verify the SSL certificate once
when the target uses a self signed certificate, a
urllib2.urlerror: & lt; urlopen error [SSL: CERTIFICATE_ VERIFY_ FAILED] certificate verify failed (_ ssl.c:581)> Error message for

It’s true that I tried with urllib, and the results are the same. Requests are the same as urllib

So to solve this problem, pep-0476 says

For users who wish to opt out of certificate verification on a single connection, they can achieve this by providing thecontextargument tourllib.urlopen:

import ssl

# This restores the same behavior as before.
context = ssl._ create_ unverified_ context()
urllib.urlopen(” https://no-valid-cert “, context=context)
It is also possible,though highly discouraged, to globally disable verification by monkeypatching thesslmodule in versions of Python that implement this PEP:

import ssl

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
that is to say, you can disable this certificate request. For urllib, there are two ways. One is that urllib. Urlopen() has a parameter context, which is set to SSL_ create_ unverified_ Context or modify the current global default value

_ create_ unverified_ https_ Context
or

ssl._ create_ default_ https_ Context
is

ssl._ create_ unverified_ context

After a test, it’s true that several tokens are returned. Does the global variable have to be set for requests. In fact, both post and get of request have a parameter called verify. Just set it to false

print requests.get(‘ https://openapi.baidu.com/oauth/2.0/token?grant_ type=client_ credentials& client_ id=xxxxx& client_ secret=xxxxxxxx’, verify=False).content

———————
Author: nankaizhl
source: CSDN
original text: https://blog.csdn.net/xiaopangxia/article/details/49908889
Copyright notice: This article is the original article of the blogger, please attach the blog link if you reprint it

Similar Posts: