-1

Two Issues:

First one is that am trying to call a JSON API using python for which I used 'Import Request' in my code. But got an error stating : No module named 'requests'

Secondly when i try to install pip,on cmd, i get an error that states - Could not fetch URL https://pypi.org/simple/pip:

My code might be wrong. Please help to get this resolved.

Error on Command prompt:

C:\Users\MEIPE\Desktop>python get-pip.py Collecting pip Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))': /simple/pip/ Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))': /simple/pip/ Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))': /simple/pip/ Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))': /simple/pip/ Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))': /simple/pip/ Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError(SSLCertVerificationError (1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))) - skipping Could not find a version that satisfies the requirement pip (from versions: )No matching distribution found for pip Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))) - skipping

Code:

import urllib.parse
import requests

main_api = 'http://nagiosdatagateway.vestas.net/esq/ITE1452552/logstash-?'

date = '2018.12.16'
url = main_api + urllib.parse.urlencode({'date':date})

json_date = requests.get(url).json()

print(json_data)

Want to call a dynamically changing JSON API url and store the data in a SQL Server table. This would help me to schedule a python script to be executed daily to get JSON data and then from the SQL table, i will get monthly reports.

yatu
  • 86,083
  • 12
  • 84
  • 139
Maria
  • 1
  • 1
  • 5
  • 1
    didn't pip come with your python distribution? I believe it usually is packaged with it – AJS Dec 27 '18 at 12:42
  • try pip --help in your CMD, if you see an output it should already be there – AJS Dec 27 '18 at 12:42
  • What operating system is this? If the certificate for `pypi.org` failed to validate, can you confirm that you have the DigiCert root CA cert installed in your operating system trust store? Also: what version of Python is this, and how was it installed? You might just need to install a newer Python interpreter. – Daniel Pryden Dec 27 '18 at 12:55
  • Thanks, did check. Yes its installed. But when i typed pip install requests it threw error: Proxy Error I suppose. Similar to one mentioned above – Maria Dec 27 '18 at 12:59

4 Answers4

2

No module named 'requests':

This simply means you have not installed the package "requests", it is solved (like you probably found out, judging from the other issue) by installing pip and running the command:

pip install requests

PIP issue:

The error you are getting from pip indicates that the SSL certificates are not correct.

This can happen if you are behind a proxy which repackages the SSL communication using its own certificate, then you need to add that certificate to the ones used by requests (requests uses certifi to know which certificates to trust, not the operating systems certificates).

You can do this by setting the environment variable "REQUESTS_CA_BUNDLE" to a bundle which contains the proxy certificate you need.

Another way to get pip to work is to bypass the security by telling pip you trust the hosts, regardless of the bad certificates, by using the flag "--trusted-host" with each of the hosts.

python get-pip.py --trusted-host=pypi.org --trusted-host=...

You will probably have to repeat this step as pip fails on the first SSL verification error, so each time it will fail on a new one until all are added

SimonF
  • 1,855
  • 10
  • 23
  • I found this .. and please let me know if itsokay to install older version : https://stackoverflow.com/questions/25835554/ssl-certificate-verification-failure-in-python-on-mavericks – Maria Dec 27 '18 at 13:43
  • What do you mean? Did you try any of the suggestions? – SimonF Dec 27 '18 at 15:31
  • Yes, I tried. It din not work. And https://stackoverflow.com/questions/25835554/ssl-certificate-verification-failure-in-python-on-mavericks The article states : Difference between python3 and python2 Here's where it starts to get interesting: I don't get the same issue when using python2.7: "I get the error in python3.4 but not python2.7 even though the cert.pem used in both cases is exactly the same." – Maria Dec 28 '18 at 04:14
  • Did you try the alternative solution? – SimonF Dec 28 '18 at 04:56
  • Also, did you check the accepted answer for that issue? It clearly states that the issue occurred because he/she installed the incorrect python3 package. Have you done that? – SimonF Dec 28 '18 at 04:58
  • Am yet to do it. Will update soon after i try it. Switched to another Automated Email task. – Maria Dec 28 '18 at 09:06
1

Whenever I need to install or update something I have to put:

pip install --trusted-host=pypi.org --trusted-host=files.pythonhosted.org --user {name of whatever I'm installing}

I found this solution here: https://github.com/pypa/pip/issues/5363

4b0
  • 21,981
  • 30
  • 95
  • 142
1

Following command can fix this:

python get-pip.py --trusted-host=files.pythonhosted.org --trusted-host=pypi.org

Run as administrator in windows

barbsan
  • 3,418
  • 11
  • 21
  • 28
zzy
  • 139
  • 6
-2

I downloaded 2.7

Ran the code below and it worked. Am yet to insert it into an SQL table

  import urllib, json
  import re
  url = "http://nagiosdatagateway.XXXXX.net/esq/ITE1452552/logstash- 
  2018.12.16/2/desc"
  response = urllib.urlopen(url)
  data = json.loads(response.read())
  print (json.dumps(data,indent = 2))
Maria
  • 1
  • 1
  • 5