Highlighted
Honored Contributor.
Honored Contributor.
438 views

Python "requests" module usage

Hello,

Until now i could use urllib2 to connect directly to various APIs through python scripts, but for a new case (for instance connection to a sharepoint site to retrieve lists), i found 2 solutions but none of them are working :

- Using the "requests" module, which seems to be present on the probe but i can't import it in my script with this error : "ImportError: No module named urllib3".
- Importing a new urllib2 compatible library, but i can't manage to do it : i first tried to import all the .py files as external resources for my package, but i found that all externals libraries are in a package named "ThirdPartyLibs", but i don't know how to add my lib (python-ntlm) to it.

Any advices ? I can't find documentations for this.

Thanks,
Best regards,
Yann Pingot

0 Likes
19 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

I saw a similar error last year, the fix was:

They copied a piece of code extracting it from the   complicated wrapped classes of OOTB  RestClients… check arxview_rest.py

Simply  copy the definition of   the class “class TrustAllCert”  in your python

Call at the beginning     the following

   TrustAllCert.enableTrustAllCertificates() 

It's a 3rd party dependency which may get left behind sometime.

 

 

Kind regards,
Bogdan Mureșan
EMEA CMS Technical Success
0 Likes
Highlighted
Honored Contributor.
Honored Contributor.

Hi Bogdan,

If i'm not mistaken this fix is used to accept all certificates (self-signed included) for urllib/urllib2 requests, i'm using a similar code in other scripts as a decorator for my functions.

In my case i need to use an external library or the embedded requests (which will save me a lot of time) , i don't think it's linked 🙂

Best regards,
Yann Pingot

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

I see.
Searching of urllib in Adapter Management I found it under ThirdPartyLibs as you said. 
Did you try to export that package, replace the problematc library and deploy again the altered package?

 

 

Kind regards,
Bogdan Mureșan
EMEA CMS Technical Success
0 Likes
Highlighted
Honored Contributor.
Honored Contributor.

I've just tried modifying the exported zip, include the new library, and re-zipping it, it seems that i cannot be imported again (with the standard deploy from local disk button) :

image.png

Best regards,
Yann Pingot

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

Awkward. We don't have a check on those files as far as I know.

Can you edit the file directly from UI?

Kind regards,
Bogdan Mureșan
EMEA CMS Technical Success
Highlighted
Honored Contributor.
Honored Contributor.

I've managed to import files through intelliJ, but it doesn't help.

I'm trying to use the embedded "piezo/requests" library as we can find in vmware_vrops_rest.py script (import piezo.ports.requests as requests) instead of importing a new one but it doesn't work either :

<< Progress message, Severity: Error>>
Échec d´exécution du script. Détails : Traceback (most recent call last):
File "requests/packages/urllib3/packages/__init__", line 3, in <module>
File "post_import_hooks", line 38, in _post_import_notifier_fn
File "post_import_hooks", line 27, in __import_and_notify
ValueError: Attempted relative import in non-package

This is strange as it should be used by a working ootb script, unless it doesn't ?

Best regards,
Yann Pingot

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

It's the first time that I see the error "Attempted relative import in non-package"

We have an option to deploy a fake CP update as we had in the past but that's too experimental.

Maybe we can find a workaround. What's the end goal of the custom code?

 

 

Kind regards,
Bogdan Mureșan
EMEA CMS Technical Success
0 Likes
Highlighted
Honored Contributor.
Honored Contributor.

The goal is to call the Microsoft Sharepoint API, for that i need to do an NTLM authentication if i'm correct.

In this case urllib2 is not enough as it doesn't support NTLM auth natively.

The best choice is the "Requests" library which helps a lot for every HTTP calls, session management, etc.
If this library was working it'll also help a lot our other developped adapters, i didn't have the choice to build manually all requests done in them with urllib2 because of that, which is not really efficient and time consuming.

You can find some documentation about Sharepoint API here : https://docs.microsoft.com/fr-fr/sharepoint/dev/sp-add-ins/get-to-know-the-sharepoint-rest-service

Best regards,
Yann Pingot

0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

I understand now.
We do use NTLM mainly for DB connection/auth.

We have the Sharepoint discovery job but now I see that we don't say anything related to the NTLM authentication. This must be a design limitation for the moment https://docs.microfocus.com/itom/Universal_Discovery_Content_Pack:2019.08/SharePtDscvr

Will check this.

 

 

Kind regards,
Bogdan Mureșan
EMEA CMS Technical Success
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

Theoretically you can define a set of NTLM credentials and use them to connect to the Sharepoint in the same manner we connect to DBs on other discovery jobs or the SCCM or SIM integrations. The credentials will need to be retrieved with Framework.GetParameter(credentials_id) from the discovery script and reference them as NTLM (v1 or v2) but I can't find a relevant example of using NTLM credentials over Rest API.

 

 

Kind regards,
Bogdan Mureșan
EMEA CMS Technical Success
0 Likes
Highlighted
Honored Contributor.
Honored Contributor.

Hi Bogdan,

It's starting to get a little tricky just to do a simple http call.

Since the requests module should be used on the vROps adapter, i've tried activating it and the error is exactly the same.

I wonder if there isn't a conflict between 2 libraries with the same name "requests".
When we try to include this one "import piezo.ports.requests as requests" as in the vrOps adapter, the import error (with the relative error thing) is coming from the other one directly located in discoveryScripts folder :

image.png

Best regards,
Yann Pingot

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.