[JSR223] [JYTHON] Jython with requests successful?

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/

Definitely. However, I ran into problems with the requests module. One error was as reported in the OP. Another error was the lack of support for SNI as I explained several posts above. So requests is a no go for me unfortunately.

You could use HABApp and the normal request module since it’s python >= 3.6.

1 Like

Do you have a helper class to deal with curl / executeCommandLine? If none exists, I might write one. It would be super simple, but it would avoid code duplications. I’m thinking something like

curl = Curl("https://www.something.com")
curl.add_header(....)
curl.set_params(....)
curl.add_data(...) to be called multiple times - for POST), or curl.set_data(pass the data in a list)
curl.get() or curl.post()

In the background it will deal with executeCommandLine’s weird “@@” thing :smiley: