How to get urllib3 and requests working with jython 2.7 beta 1?
Asked Answered
H

2

6

Smart folks,

I would like to use the awesome requests module in my jython program. It installs and runs just fine in python but I cannot get it to install in jython. I have tried both Jython 2.7a2 and 2.7b1 on mac and ubuntu and get the same errors related to urllib3.

First installed ez_setup.py as mentioned in How can I use jython setup.py install?

Then run easy_install from within the jython bin directory results in exception:

NameError: name 'CERT_NONE' is not defined
gautam-mbp:bin gautam$ ./easy_install requests
Searching for requests
Reading http://pypi.python.org/simple/requests/
Reading http://python-requests.org
Reading https://github.com/kennethreitz/requests
Best match: requests 1.1.0
Downloading http://pypi.python.org/packages/source/r/requests/requests-1.1.0.tar.gz#md5=a0158815af244c32041a3147ee09abf3
Processing requests-1.1.0.tar.gz
Running requests-1.1.0/setup.py -q bdist_egg --dist-dir /var/folders/jf/cb2pc45s7d94hd6sndysvyxw0000gn/T/easy_install-MnOao_/requests-1.1.0/egg-dist-tmp-E2Rkg1
Traceback (most recent call last):
  File "./easy_install", line 7, in <module>
    sys.exit(
  File "/Users/gautam/jython27b1/Lib/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/easy_install.py", line 1712, in main
  File "/Users/gautam/jython27b1/Lib/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/easy_install.py", line 1700, in with_ei_usage
  File "/Users/gautam/jython27b1/Lib/site-packages/setuptools-0.6c11-py2.7.egg/setuptools/command/easy_install.py", line 1712, in <lambda>
-----------lots of stack trace---------------
  File "setup.py", line 6, in <module>
  File "/var/folders/jf/cb2pc45s7d94hd6sndysvyxw0000gn/T/easy_install-MnOao_/requests-1.1.0/requests/__init__.py", line 52, in <module>
  File "/var/folders/jf/cb2pc45s7d94hd6sndysvyxw0000gn/T/easy_install-MnOao_/requests-1.1.0/requests/utils.py", line 23, in <module>
  File "/var/folders/jf/cb2pc45s7d94hd6sndysvyxw0000gn/T/easy_install-MnOao_/requests-1.1.0/requests/compat.py", line 7, in <module>
  File "/var/folders/jf/cb2pc45s7d94hd6sndysvyxw0000gn/T/easy_install-MnOao_/requests-1.1.0/requests/packages/__init__.py", line 3, in <module>
  File "/var/folders/jf/cb2pc45s7d94hd6sndysvyxw0000gn/T/easy_install-MnOao_/requests-1.1.0/requests/packages/urllib3/__init__.py", line 16, in <module>
  File "/var/folders/jf/cb2pc45s7d94hd6sndysvyxw0000gn/T/easy_install-MnOao_/requests-1.1.0/requests/packages/urllib3/connectionpool.py", line 45, in <module>
  File "/var/folders/jf/cb2pc45s7d94hd6sndysvyxw0000gn/T/easy_install-MnOao_/requests-1.1.0/requests/packages/urllib3/util.py", line 293, in <module>
NameError: name 'CERT_NONE' is not defined

Looks like problem related to urllib3 not working with jython. Appreciate help in getting requests (and urllib3 ) to work on jython. The same error shows up on ubuntu as well.

Thanks Gautam

Hemimorphic answered 17/2, 2013 at 9:3 Comment(1)
This is a bug. :( Just filed it, you can track it here: github.com/shazow/urllib3/issues/150 Will try to fix it soon. Sorry for the inconvenience.Prismoid
S
6

Edit: as of May 2015, the current stable version of requests, 2.7.0, no longer suffers from the original error in Jython --Aprillion


The bug you are facing has been fixed some time ago in the development versions of urllib3 and requests.

As you won't be able to install it as it will blow up pip/easy_install with java.lang.RuntimeException: java.lang.RuntimeException: Class file too large! you have to run it directly from the source tree.

There you can fix the import bug by replacing all of the ssl_wrap_socket stuff at the end of requests/packages/urllib3/util.py with a dummy method like:

def ssl_wrap_socket(sock, keyfile=None, certfile=None, cert_reqs=None,
                    ca_certs=None, server_hostname=None,
                    ssl_version=None):
    raise Error('This is not meant to be called')

(If ssl support is a concern to you: it is broken at the moment anyways)

As jython is missing support for idna you have to remove this too. Go to line 290 of request/models.py and replace 'idna' by 'utf-8'.

So if you don't need to use ssl or idna those steps should work.

Followup: The relevant ssl bug in jython is tracked here: http://bugs.jython.org/issue2016

Sleuth answered 20/2, 2013 at 19:53 Comment(3)
Will try it out today - can you please send me some information on how to install dev versions of urllib3 and requests?Hemimorphic
You don't need the dev version. Download the tar.gz file from pypi, unpack it and then modify files util.py and models,py like explained in the answer. Then try to use it with a jython interpreter started in the directory of the unpacked requests. If you want to use it from some other place you have to adjust this pythons path, there is documentation for that.Sleuth
This is great. Just tested with Jython2.7b1 and requests-2.1.0. Still have to fix the idna issue (now on line 344). But no longer need to fix the ssl_wrap_socket (although ssl will still not work when called). Thanks for posting this answer.Ayr
S
1

This problem has been resolved - please use Jython 2.7 beta 4 (or the final release).

Stratovision answered 16/5, 2014 at 16:59 Comment(1)
when will 2.7 b3 be released? I am not seeing it yet from here jython.org/downloads.htmlGiuseppinagiustina

© 2022 - 2024 — McMap. All rights reserved.