# Python requests library

**URL:** <https://forum.inductiveautomation.com/t/python-requests-library/59778>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [May 19, 2022, 1:03pm UTC](https://forum.inductiveautomation.com/t/python-requests-library/59778 "2022-05-19T13:03:11Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![louagejasper](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/louagejasper/32/86311_2.png) [@louagejasper](https://forum.inductiveautomation.com/u/louagejasper)\
**Post date:** [May 19, 2022, 1:03pm UTC](https://forum.inductiveautomation.com/t/python-requests-library/59778/1 "2022-05-19T13:03:11Z")

</div>

Hi,

We try to use the requests library in Ignition (8.1.15).

We get this error:

```auto
Error running action 'component.onActionPerformed' on jasperlg@D/root/Button: Traceback (most recent call last): File "<function:runAction>", line 2, in runAction ImportError: Error loading module requests: Traceback (most recent call last): File "<module:requests>", line 86, in <module> File "<module:requests>", line 72, in getImage File "<module:requests>", line 48, in create_signed_jwt File "/usr/local/bin/ignition/user-lib/pylib/site-packages/jwt/api_jwt.py", line 64, in encode return super(PyJWT, self).encode( File "/usr/local/bin/ignition/user-lib/pylib/site-packages/jwt/api_jws.py", line 118, in encode raise NotImplementedError( NotImplementedError: Algorithm 'RS256' could not be found. Do you have cryptography installed?

```

We used pip for Jython, so we could import the library with all it’s dependencies, but it still asked for the cryptogrpahy library.  
I tried installing cryptography, but pip on jython refused to install it and gives this error:

```auto
  ERROR: Command errored out with exit status 1:
   command: /usr/local/jython/bin/jython /usr/local/jython/Lib/site-packages/pip install --ignore-installed --no-user --prefix /tmp/pip-build-env-mmTpXJ/overlay --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- 'setuptools>=40.6.0' wheel 'cffi>=1.12; platform_python_implementation != '"'"'PyPy'"'"''
       cwd: None
  Complete output (34 lines):
  WARNING: An illegal reflective access operation has occurred
  WARNING: Illegal reflective access by org.python.core.io.StreamIO (file:/usr/local/jython/jython.jar) to field java.io.FilterInputStream.in
  WARNING: Please consider reporting this to the maintainers of org.python.core.io.StreamIO
  WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
  WARNING: All illegal access operations will be denied in a future release
  DEPRECATION: pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
  WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
  Collecting setuptools>=40.6.0
    Using cached setuptools-44.1.1-py2.py3-none-any.whl (583 kB)
  Collecting wheel
    Using cached wheel-0.37.1-py2.py3-none-any.whl (35 kB)
  Collecting cffi>=1.12
    Using cached cffi-1.15.0.tar.gz (484 kB)
      ERROR: Command errored out with exit status 1:
       command: /usr/local/jython/bin/jython -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-jXS2Lc/cffi/setup.py'"'"'; __file__ ='"'"'/tmp/pip-install-jXS2Lc/cffi/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)( __file__ );code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__ , '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-8Qff1t
           cwd: /tmp/pip-install-jXS2Lc/cffi/
      Complete output (15 lines):
      Traceback (most recent call last):
        File "<string>", line 1, in <module>
        File "/tmp/pip-install-jXS2Lc/cffi/setup.py", line 148, in <module>
          ask_supports_thread()
        File "/tmp/pip-install-jXS2Lc/cffi/setup.py", line 76, in ask_supports_thread
          ok = (sys.platform != 'win32' and
        File "/usr/local/jython/Lib/distutils/command/config.py", line 232, in try_compile
          self._compile(body, headers, include_dirs, lang)
        File "/usr/local/jython/Lib/distutils/command/config.py", line 232, in try_compile
          self._compile(body, headers, include_dirs, lang)
        File "/usr/local/jython/Lib/distutils/command/config.py", line 136, in _compile
          (obj,) = self.compiler.object_filenames([src])
        File "/usr/local/jython/Lib/distutils/ccompiler.py", line 821, in object_filenames
          if ext not in self.src_extensions:
      TypeError: 'NoneType' object is not iterable
      ----------------------------------------
  ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
  ----------------------------------------
ERROR: Command errored out with exit status 1: /usr/local/jython/bin/jython /usr/local/jython/Lib/site-packages/pip install --ignore-installed --no-user --prefix /tmp/pip-build-env-mmTpXJ/overlay --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- 'setuptools>=40.6.0' wheel 'cffi>=1.12; platform_python_implementation != '"'"'PyPy'"'"'' Check the logs for full command output.

```

Could it be that cryptography is CPython only? Or how come this can’t be installed? I had the same issue with the Pillow library.  
I also tried downloading the libraries manually (for Py2.7), but that gave an error in Ignition.  
I’m having a hard time debugging stuff like this.

Any help here would be great.

---

<div class="post-metadata">

**Author:** ![code\_skin](https://avatars.discourse-cdn.com/v4/letter/c/43a26b/32.png) [@code\_skin](https://forum.inductiveautomation.com/u/code_skin)\
**Post date:** [May 19, 2022, 1:04pm UTC](https://forum.inductiveautomation.com/t/python-requests-library/59778/2 "2022-05-19T13:04:19Z")

</div>

I would recommend using httpClient() instead of requests.... [system.net.httpClient | Ignition User Manual](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-net/system-net-httpClient)

More information about python libraries can be found here: [https://support.inductiveautomation.com/hc/en-us/articles/360056397252-Python-In-Ignition](https://support.inductiveautomation.com/hc/en-us/articles/360056397252-Python-In-Ignition)

_edit_: updated doc link.

---

<div class="post-metadata">

**Author:** ![Kevin.McClusky](https://avatars.discourse-cdn.com/v4/letter/k/47e85d/32.png) [@Kevin.McClusky](https://forum.inductiveautomation.com/u/Kevin.McClusky)\
**Post date:** [May 20, 2022, 12:25am UTC](https://forum.inductiveautomation.com/t/python-requests-library/59778/3 "2022-05-20T00:25:36Z")

</div>

`Could it be that cryptography is CPython only?` - Yes, that’s what’s happening here. If you look through the dependencies that pip is trying to install, you’ll see that it’s getting stuck on cffi. The cffi package is a “C Foreign Function Interface for Python”.

As mentioned here, I’d definitely explore system.net.httpClient. It can do most of what requests does, and is normally really good at it. It might be a little less ‘pythonic’, but it’s very capable, and even gives you access to the underlying HttpClient / HttpRequest / HttpResponse objects if you need them.

---

<div class="post-metadata">

**Author:** ![louagejasper](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/louagejasper/32/86311_2.png) [@louagejasper](https://forum.inductiveautomation.com/u/louagejasper)\
**Post date:** [May 20, 2022, 7:04am UTC](https://forum.inductiveautomation.com/t/python-requests-library/59778/4 "2022-05-20T07:04:50Z")

</div>

Thank you Kevin! That helps.  
Now I will have to find a way to encrypt my message RS256 with java libs probably.

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [May 20, 2022, 8:15am UTC](https://forum.inductiveautomation.com/t/python-requests-library/59778/5 "2022-05-20T08:15:13Z")

</div>

urllib should work without having to install anything new.

Haven’t used it myself though, the 2.7 version looks a bit outdated, but it seems to work.

[https://docs.python.org/2/library/urllib.html](https://docs.python.org/2/library/urllib.html)

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/9/0/90b28917ae5d64a270c2d68d9ab7d77368097bec.png)

---

<div class="post-metadata">

**Author:** ![louagejasper](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/louagejasper/32/86311_2.png) [@louagejasper](https://forum.inductiveautomation.com/u/louagejasper)\
**Post date:** [May 20, 2022, 8:24am UTC](https://forum.inductiveautomation.com/t/python-requests-library/59778/6 "2022-05-20T08:24:36Z")

</div>

Yes, urllib works directly. Any idea on how to encrypt to RS256?

---

<div class="post-metadata">

**Author:** ![victordcq](https://avatars.discourse-cdn.com/v4/letter/v/f6c823/32.png) [@victordcq](https://forum.inductiveautomation.com/u/victordcq)\
**Post date:** [May 20, 2022, 11:01am UTC](https://forum.inductiveautomation.com/t/python-requests-library/59778/7 "2022-05-20T11:01:40Z")

</div>

If you post to https, encryption should work automattically, i think.  
Otherwise no idea, you’ll have to find a lib or the correct algoritme xd

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 20, 2022, 12:06pm UTC](https://forum.inductiveautomation.com/t/python-requests-library/59778/8 "2022-05-20T12:06:57Z")

</div>

If you encounter international characters, urllib is likely to let you down in a jython environment. Use httpClient.

---

<div class="post-metadata">

**Author:** ![louagejasper](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/louagejasper/32/86311_2.png) [@louagejasper](https://forum.inductiveautomation.com/u/louagejasper)\
**Post date:** [May 20, 2022, 2:25pm UTC](https://forum.inductiveautomation.com/t/python-requests-library/59778/9 "2022-05-20T14:25:08Z")

</div>

Yes, it worked with httpClient! Indeed, quite easy to implement.

The Google cloud integration was a bit harder.  
If anyone needs to encrypt something with RS256 in Jython,

you can do this:

```auto
from java.security import KeyFactory
from java.security import Signature
from java.security.spec import PKCS8EncodedKeySpec
from java.util import Base64

def signSHA256RSA(input, pkey):
	#Remove markers and new line characters in private key
	realPK = pkey.replace("-----END PRIVATE KEY-----", "")
	realPK = realPK.replace("-----BEGIN PRIVATE KEY-----", "")
	realPK = realPK.replace("\n", "")
	b1 = Base64.getDecoder().decode(realPK)
	spec = PKCS8EncodedKeySpec(b1)
	kf = KeyFactory.getInstance("RSA")
	privateSignature = Signature.getInstance("SHA256withRSA")
	privateSignature.initSign(kf.generatePrivate(spec))
	privateSignature.update(str(input))
	s = privateSignature.sign()
	return Base64.getUrlEncoder().encodeToString(s)

```
