HTTPS使用Jython [英] HTTPS get using Jython

查看:109
本文介绍了HTTPS使用Jython的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在下面查看我的代码.基本上,它是一个脚本,该脚本以主机名作为参数查询API服务,然后返回与此服务器关联的一些元数据.现在,当从已安装到Windows机器中的Python 2.7编译器执行时,它确实运行良好,也可以从bash CLI中使用CURL来工作(这意味着该URL绝对有效),但是此脚本将在具有嵌入式JVM包装程序的HP应用程序,这意味着它将像本地计算机一样使用Jython而不是CPython-至少这是我认为的问题.这就是导致第一个问题的原因-SSL握手将失败,因为CPython忽略证书,而Jython不会.由于此内部API的证书是自签名的,因此得到以下信息:

Please see my code below. Basically it's a script that queries an API service with a hostname as the parameter, which then returns some meta data associated with this server. Now, it works really well when executed from a Python 2.7 compiler that's been installed into a windows machine, also works using CURL from a bash CLI (meaning that the URL is definitely working), but this script will be running on an HP application that has an embedded JVM wrapper, meaning that it will use Jython and not CPython like my local machine - at least that's what I think the issue is. This is what causes the first problem - SSL handshake will fail because while CPython ignores certificates, Jython doesn't. Since the certificate of this internal API is self-signed, I get this:

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')

尝试使用HTTP时,出现错误代码400:

When attempted using HTTP, I get error code 400:

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

我认为值得注意的是,这不可能是网络问题.我可以使用HTTP从浏览器中获取数据.

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

理想情况下,我对不安全的HTTP感到满意,因为它还是一个内部服务.另外,由于我的脚本将在45-50台不同的计算机上运行,​​因此信任证书将成问题,这会增加过多的开销.

Ideally, I would be happy with unsecure HTTP since it's an internal service anyway. Also, trusting certs is going to be problematic since my script will run in 45-50 different machines, adding way too much overhead.

另外,使用urllib2以外的其他东西(例如Python请求模块)也将是一个问题,因为任何非ootb的东西都必须分配给所有机器,这又增加了不可接受的开销.

Also, using something other than urllib2 (Python requests module, for example) is going to be a problem, because anything non-ootb would have to be distributed to all the machines, again adding unacceptable overhead.

我发现了这一点:信任所有证书Jython ,但坦率地说,我不太了解.

I have found this: trusting all certificates in Jython, but frankly I don't understand much.

我猜我的问题是:为什么HTTP失败?

I guess my question is: why HTTP fails?

将一些证书导入到JVM的密钥库中,现在我似乎正在传递SSL层,但是它仍然以代码400进行响应.

Imported some certs into JVM's keystore, now I seem to be passing the SSL layer but it still responds with code 400.

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'

推荐答案

我设法通过打印出"request"实例的属性来自行解决.这样做表明,base64.encode显然返回了带有尾随换行符的字符串,这导致服务器以400响应.我想使用encode会绕过该问题,尽管我不明白为什么base64.encode会返回值首先是尾随换行符.

I managed to figure it out by myself by printing out the properties of the "request" instance. Doing so revealed that base64.encode apparently returns the string with a trailing newline symbol, which causes the server to respond with 400. I guess using encode would have bypassed the issue, although I can't understand why base64.encode gives the value back with a trailing newline in the first place.

关闭案例...

这篇关于HTTPS使用Jython的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆