chrismattmann / tika-python

Tika-Python is a Python binding to the Apache Tika™ REST services allowing Tika to be called natively in the Python community.
Apache License 2.0
1.51k stars 234 forks source link

Python 2.7.9 on windows fails to download Tika server #54

Closed chrismattmann closed 9 years ago

chrismattmann commented 9 years ago

Running on latest Python 2.7.9 on windows Vista and I get this:

>>> parser.from_buffer(str)
tika.py: Retrieving http://search.maven.org/remotecontent?filepath=org/apache/tika/tika-server/1.9/tika-server-1.9.jar to c:\users\chrisa~1\appdata\local\temp\tika-server.jar.

Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    parser.from_buffer(str)
  File "C:\Python27\lib\site-packages\tika\parser.py", line 29, in from_buffer
    {'Accept': 'application/json'}, False)
  File "C:\Python27\lib\site-packages\tika\tika.py", line 239, in callServer
    serverEndpoint = checkTikaServer(serverHost, port, tikaServerJar)
  File "C:\Python27\lib\site-packages\tika\tika.py", line 266, in checkTikaServer
    tikaServerJar = getRemoteJar(tikaServerJar, jarPath)
  File "C:\Python27\lib\site-packages\tika\tika.py", line 300, in getRemoteJar
    urlretrieve(urlOrPath, destPath)
  File "C:\Python27\lib\urllib.py", line 98, in urlretrieve
    return opener.retrieve(url, filename, reporthook, data)
  File "C:\Python27\lib\urllib.py", line 245, in retrieve
    fp = self.open(url, data)
  File "C:\Python27\lib\urllib.py", line 213, in open
    return getattr(self, name)(url)
  File "C:\Python27\lib\urllib.py", line 364, in open_http
    return self.http_error(url, fp, errcode, errmsg, headers)
  File "C:\Python27\lib\urllib.py", line 377, in http_error
    result = method(url, fp, errcode, errmsg, headers)
  File "C:\Python27\lib\urllib.py", line 641, in http_error_302
    data)
  File "C:\Python27\lib\urllib.py", line 667, in redirect_internal
    return self.open(newurl)
  File "C:\Python27\lib\urllib.py", line 213, in open
    return getattr(self, name)(url)
  File "C:\Python27\lib\urllib.py", line 443, in open_https
    h.endheaders(data)
  File "C:\Python27\lib\httplib.py", line 1049, in endheaders
    self._send_output(message_body)
  File "C:\Python27\lib\httplib.py", line 893, in _send_output
    self.send(msg)
  File "C:\Python27\lib\httplib.py", line 855, in send
    self.connect()
  File "C:\Python27\lib\httplib.py", line 1274, in connect
    server_hostname=server_hostname)
  File "C:\Python27\lib\ssl.py", line 352, in wrap_socket
    _context=self)
  File "C:\Python27\lib\ssl.py", line 579, in __init__
    self.do_handshake()
  File "C:\Python27\lib\ssl.py", line 808, in do_handshake
    self._sslobj.do_handshake()
IOError: [Errno socket error] [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)
>>> 

I've tried monkey patching as suggested in:

http://stackoverflow.com/questions/27835619/ssl-certificate-verify-failed-error

Still get same error.

And also read:

http://bugs.python.org/issue23052

chrismattmann commented 9 years ago

A workaround on this is to download and install Python 2.7.8. I was able to get Tika working using 2.7.8. After installing, I pip install --upgrade --force tika. This should install Tika 1.9.3. After doing so, double check to make sure there aren't any temporary instances of tika server that are corrupt from previous downloads using HTTPS where it messed up because of 2.7.9. The way I checked was checking:

c:\users\chrisa~1\appdata\local\temp\

which is where tika-server is downloaded to. Check the jar file it may likely be corrupt. Delete it if necessary (which will cause Python to redownload it for you).

Then, try e.g., parser.from_file again:

>>> parser.from_file('C:\Users\Chris A Mattmann\Desktop\BalanceSheet.pdf')
tika.py: Retrieving http://search.maven.org/remotecontent?filepath=org/apache/tika/tika-server/1.9/tika-server-1.9.jar to c:\users\chrisa~1\appdata\local\temp\tika-server.jar.
{'content': None, 'metadata': {u'access_permission:can_modify': [u'true'], u'access_permission:extract_content': [u'true'], u'access_permission:assemble_document': [u'true'], u'access_permission:extract_for_accessibility': [u'true'], u'access_permission:fill_in_form': [u'true'], u'pdf:encrypted': [u'false'], u'access_permission:can_print': [u'true'], u'dc:format': [u'application/pdf; version=1.3'], u'access_permission:can_print_degraded': [u'true'], u'access_permission:modify_annotations': [u'true'], u'pdf:PDFVersion': [u'1.3'], u'X-TIKA:parse_time_millis': [u'886'], u'xmpTPg:NPages': [u'0'], u'resourceName': [u'BalanceSheet.pdf'], u'Content-Type': [u'application/pdf'], u'X-Parsed-By': [[u'org.apache.tika.parser.DefaultParser', u'org.apache.tika.parser.pdf.PDFParser']]}}
>>> 

Works fine. Will keep trying to find a more permanent fix than install Python 2.7.8.

chrismattmann commented 9 years ago

OK confirmed that I can monkey patch this in the latest version. I am going to add a check inside of the function where it downloads the tika jar checking for an IOError, and if so, then doing:

>>> import ssl
>>> if hasattr(ssl, '_create_unverified_context'):
    ssl._create_default_https_context = ssl._create_unverified_context