Dzung Nguyen
Dzung Nguyen

Reputation: 3944

Jython and httplib2

I'm trying to follow this blog post about headless Oauth authentication:

http://blog.databigbang.com/automated-browserless-oauth-authentication-for-twitter/

Basically I;m trying to use jython to call Htmlunit, open the authorization webpage and accept it. However there's some incompatibility between jython and httplib2

File "/Users/andrey/jython2.7b1/Lib/site-packages/httplib2-0.8-py2.7.egg/httplib2/iri2uri.py", line 71, in iri2uri
    authority = authority.encode('idna')
LookupError: unknown encoding 'idna'

How to fix this error? If I import encodings.idna, then stringprep, re, codecs must be also imported, which jython doesn't have.

Upvotes: 0

Views: 341

Answers (1)

ruscur
ruscur

Reputation: 195

Jython doesn't have idna support, instead you have to call Java's if you want to do the same thing.

To encode Unicode to IDNA ASCII format:

import java.net.IDN
authority = java.net.IDN.toAscii(authority)

To decode IDNA ASCII into Unicode:

authority = java.net.IDN.toUnicode(authority)

If you're modifying httplib2 (or any other library) and don't want to break its functionality for other Python implementations, you can do something like this:

import platform
if platform.python_implementation() == "Jython":
    import java.net.IDN
    # do IDNA things here
else:
    # use .encode('idna') Pythonically

Upvotes: 1

Related Questions