Sorry for the Necro post but this remains a hassle and my workaround may help someone.
We have authenticating proxy + man in the middle CA certs installed on or corporate network.
Getting past this the java way while not impossible is not very pleasant and involves writing authenticators.
Since the update to Jython using 2.7 under ignition 8.* it is quite possible to use the requests library which is arguably a whole lot nicer than the java client. I use requests-2.14 as it is one of the last to support python 2.7.
I also load it at runtime from a DB blob with no special gateway/client configuration required.
I use postgres and I have a table like this setup in an accessible database:
If you use another db you will have to alter sql stuff accordingly to your flavour.
CREATE TABLE python_modules (
id serial4 NOT NULL,
"name" varchar NULL,
file varchar NULL,
contents bytea NULL,
startpath varchar NULL,
CONSTRAINT pk_python_modules_id PRIMARY KEY (id))
I then have a gateway script in my case located at shared.jython
LOADED={}
"""workaround to dynamically load python modules at runtime from blobs in DB. No need for any special gateway install
How to use
In script console - once off to save to db
shared.jython.saveToDB("requests","<specify local path to>/requests-2.14.2.tar.gz","requests-2.14.2")
To load and use requests
shared.jython.loadFromDB("requests")
import requests
"""
def loadFromDB(name,force=False):
import imp,tempfile,tarfile,os
global LOADED
if name in LOADED and not force:
return
sql="""SELECT id,contents,filename,startpath FROM python_modules WHERE name=?"""
ret=system.db.runPrepQuery(sql,[name])
if ret:
fn=ret[0][2]
#write file to namedtempfile
tf=tempfile.NamedTemporaryFile()
system.file.writeFile(tf.name,ret[0][1])
print "tempfile",tf.name
#check if tar.gz
if tarfile.is_tarfile(tf.name):
tar=tarfile.open(tf.name)
td=tempfile.mkdtemp()
tar.extractall(td)
tar.close()
os.unlink(tf.name)
#now should have a tempdir with loaded module
sp=ret[0][3]
if sp:
td=os.path.join(td,sp)
mod= imp.find_module(name,[td])
imp.load_module(name,*mod)
LOADED[name]=td
else:
os.unlink(tf.name)
def closeModule(name):
#extra step to remove tempdir when no longer needed
import shutil,os
global LOADED
if name in LOADED:
shutil.rmtree(LOADED[name])
del LOADED[name]
def saveToDB(name,filepath,startpath):
import os
print filepath
if not filepath and not os.path.isfile(filepath):
return
sql="""SELECT id FOM python_modules WHERE name=?"""
ret=system.db.runPrepQuery(sql,[name])
bytes=system.file.readFileAsBytes(filepath)
filename=os.path.basename(filepath) #take care with windows paths as it may return the whole path
if ret:
#update
sql="""UPDATE python_modules SET filename=?, contents=? ,startpath=? WHERE id =?"""
system.db.runPrepUpdate(sql,[filename,bytes,startpath,ret[0][0]])
else:
#insert
sql="""INSERT INTO python_modules (name,filename,contents,startpath) values (?,?,?,?)"""
system.db.runPrepUpdate(sql,[name,filename,bytes,startpath])
Now I can simply use requests to make calls via authenticating proxy
eg in a script console
Jython 2.7.3, executing locally in the Designer.
Press (Ctrl + Space) to activate autocompletion.
>>> shared.jython.loadFromDB("requests")
tempfile /tmp/tmptnWKE0
>>> import requests
>>> proxies= {'http': 'http://<user>:<pass>@<proxy server>:<proxy port>', 'https': 'http://<user>:<pass>@<proxy server>:<proxy port>'}
>>> response=requests.get("http://date.jsontest.com/",proxies=proxies)
>>> print(response.json())
{u'date': u'06-24-2024', u'milliseconds_since_epoch': 1719203278943L, u'time': u'04:27:58 AM'}