[JSR223] [JYTHON] Jython with requests successful?

Hi,

i just started to use jython and so far everything works fine. But now i try to use the requests module. I installed it and again it ist working a little bit.

I got the following error

2019-11-26 21:05:35.595 [ERROR] [jsr223.jython.Read ice warning      ] - Traceback (most recent call last):
  File "/etc/openhab2/automation/lib/python/core/log.py", line 51, in wrapper
    return fn(*args, **kwargs)
  File "<script>", line 19, in ReadIceWarning
  File "/etc/openhab2/automation/lib/python/requests/api.py", line 116, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/etc/openhab2/automation/lib/python/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/etc/openhab2/automation/lib/python/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/etc/openhab2/automation/lib/python/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/etc/openhab2/automation/lib/python/requests/adapters.py", line 514, in send
    raise SSLError(e, request=request)
SSLError: HTTPSConnectionPool(host='xxx', port=443): Max retries exceeded with url: /api/ (Caused by SSLError(SSLError(1, u'Received fatal alert: handshake_failure'),))

Using plain python everything works. Has anyone used requests with jython and with success?

Thomas

I tried for all of 15 minutes one day and ran into a different problem (I didn’t have all the libraries that requests depends on like url). In the end I just used the sendHttpGetRequest OH Action. Are you doing something that absolutely has to be done with requests?

In your case, it looks like requests is working and there is something wrong with the SSL configuration on the server you are contacting. Does the server use a self signed certificate or something like that? Maybe your host machine doesn’t have the same cypher set as the server does so it can’t decide on what crypto algorithm to use. Unfortunately, the error doesn’t really say what the SSL problem is, but I guess the good news is requests is working for you. It’s just that it cannot establish the SSL connection to the host machine you are trying to connect to.

How didi you install the requests module? The pip with some parameters did the trick for me. Unfortunately i tried it with different urls and none are working. And i don’t think that the problem is on the server side, because with chrome i get the results.

Then i will use the OH Action …

Because I just downloaded the jar file I can’t use pip to install the libraries. I’ve no doubt I could make it work but it wasn’t worth my time since I had other options.

But Chrome has a different set of trusted CAs and a different set of crypto algorithms which match what the server is providing. Your requests library/certificate configuration is not compatible with the server.

Your question reminds me that the docs need to include some instructions on adding additional modules.

2 Likes

on my openHAB server Python is installed, so i used pip to install the requests package with

pip install --target=/etc/openhab2/automation/lib/jython requests

That resolved all dependicies.

1 Like

I’m running into the same problem. I did my install using pip to conf/automation/lib/python/system (and added this to sys.path). I got the same error as the OP.

pip said:

Successfully installed certifi chardet-2.2.1 idna requests urllib3

Have tried the same functionality using standard python? Does that work?

normal python works, however my openhab is inside a docker, so I’m guessing it’s a completely different environment.

From my point of view it is a problem with jython

Python 2 or 3? Jython is Pythoin 2 based.

Both python 2.7.5 and 3.6.8 work.

Here’s the code

import requests
def uv_protection_time():
    token = "xxxx" 
    params= {"lat": xxxxx, "lng": xxxxx, "from": 3, "to": 3, "alt": 100}
    r = requests.get("https://api.openuv.io/api/v1/protection", params=params, headers={"x-access-token": token})
    jsondata = r.json()
    print (jsondata['result']['from_time'])

uv_protection_time()

i will open an issue with jython

I’m using @5iver’s jar from [beta testers wanted!] Jython addon w/ helper libraries to have jython support on my installation.

Another similar issue I came across with jython and requests is that python < 2.7.9 doesn’t support SNI. Whilst there is a solution to this, it involves using pyOpenSSL which in turns, requires a cryptography module which is a compiled module, and apparently jython doesn’t support this

I hope I’m wrong and that there’s a solution to this. I have spent hours trying.

My main requirements are:

  • Need to perform HTTPS GET/POST
  • Using a custom header, e.g. “x-access-token: XXXXX”, “Authorization: XYZ SubscriberID=XXXXX”, etc.

AFAIK, the Openhab HTTP Action doesn’t support custom headers. Is there work on making it support custom headers?

I am reluctant to use HTTP Binding v1, so I haven’t even tried to see if it would work

There’s a HTTP Binding v2 WIP mentioned in here somewhere - but it seems to be abandoned at the moment.

If we could update Jython to python version 2.7.9+ that would be great too.

The jython project is not affiliated to OH and that are only at 2.7.1. We have no control or input into their development.

In OH3, I/we will be looking into integrating GraalVM into OH, which should allow for a much more modern Python and the possibility of other lanaguages. This has been discussed a few times in the forum. I still have this topic open in another tab (haven’t given up on this!) and might get to look at it this weekend. Another option for you is to use Java libraries. While custom headers may not be allowed with the Action, I’m sure the library supports them.

1 Like

Do you have any examples of using Java libraries from jython?

In the mean time I am resorting to using the command line curl, with Exec.executeCommandLine()

Pretty much all of the core modules use OH or Java libraries. Just import them as you would a Python library.

This is what I have used too.

I think python requests supports custom headers.

https://2.python-requests.org/en/master/