HTTPS uses Jython

See my code below. This is basically a script that requests an API service with the host name as a parameter, which then returns some metadata associated with this server. Now it works fine when executed from a Python 2.7 compiler that was installed on a Windows computer, also works using CURL from the CLI bash (which means the URL is definitely working), but this script will work with an HP application that has a built-in JVM shell which means that it will use Jython and not CPython as my local machine - at least what I think is the problem. This is why the first problem is that SSL handshaking will not work, because while CPython ignores certificates, Jython does not. Since the certificate of this internal API is self-signed, I get the following:

 jvm 1 | sslerror(-1, 'SSL handshake exception: Differences between the SSL socket behaviour of cpython vs. jython are explained on the wiki: http://wiki.python.org/jython/NewSocketModule#SSL_Support') 

When I try to use HTTP, I get error code 400:

 400: ('Bad Request', 'Bad request syntax or unsupported method') 

I think it's worth noting that this cannot be a network problem. I can get data from the browser using HTTP, just fine.

Ideally, I would be pleased with insecure HTTP, as it is an internal service. In addition, trusting certificates will be problematic, as my script will run on 45-50 different machines, adding too much overhead.

Also, using something other than urllib2 (like the Python request module) will be a problem, because something non-ootb needs to be distributed on all machines, again adding inappropriate service data.

I found this: trust all certificates in Jython , but to be honest, I understand a little.

I think my question is: why does HTTP fail?

EDIT: I imported some certificates into the JVM repository, now I seem to be passing SSL level, but it still responds with 400 code.

 import urllib2 import base64 baseUrl = 'https://my-api-example.com/' hostname = 'server1.example.com' username = "vinnie" password = 'pooh' request = urllib2.Request(baseUrl + hostname) base64string = base64.encodestring('%s:%s' % (username, password)) request.add_header("Authorization", "Basic %s" % base64string) try: data = urllib2.urlopen(request) print data.read() except urllib2.HTTPError, e: print e.code except urllib2.URLError, e: print e.reason except: print 'Undefined error' 
+1
source share
1 answer

I managed to figure this out by printing out the properties of the query instance. This showed that base64.encode apparently returns a string with the back line of a newline, which causes the server to respond to 400. I assume that using encoding would bypass the problem, although I cannot understand why base64.encode returns a value with the final new line in the first place.

Case is closed...

0
source

All Articles