1

I'm very new to python. The job assigned to me was to perform a put and get operation on one of the products web ui using python. I tried with the following code,

import urllib2 
import ssl

try:
    response = urllib2.urlopen('https://the product web ui address') 
    print 'response headers: "%s"' % response.info() except IOError, e:
    if hasattr(e, 'code'): # HTTPError
        print 'http error code: ', e.code
    elif hasattr(e, 'reason'): # URLError
        print "can't connect, reason: ", e.reason
    else:
        raise

This is raising an error:

can't connect, reason: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:590)

The url, while accessing through browser will show that it is having a certificate error. We need to click on proceed to go to the url. Can somebody help me with a way to perform put and get operation on such a uri. Sorry if this question seems dumb, thanks for the support.

Also, i tried with requests. Attaching the code. It also gives the same error

import requests
import sys

try:

    r = requests.get("https://the product web ui address")
    data = r.content
    print r.status_code
    print data
except:
    print "Error Occured ",sys.exc_info()[0]

The error is :

Error Occured class 'requests.exceptions.SSLError'

Pranav
  • 115
  • 1
  • 11

1 Answers1

1

I got it.

import requests
import sys

try:

    r = requests.get("https://web ui address",verify=False)
    data = r.content
    f = open('myfile.xml','w')
    f.write(data)
    f.close()
    print "Status Code returned : " ,r.status_code
except:
    print "Error Occured ",sys.exc_info()[0]

Adding verify=False, solved my problem.Now it throws a warning but no errors. Its working fine.

Pranav
  • 115
  • 1
  • 11
  • That will work if this is a one and done type scenario. However, if you are going to be reusing this code often, it would be worth the effort to set up the certificates so that things verify. Check out this question regarding SSL and urlib2 http://stackoverflow.com/questions/27835619/ssl-certificate-verify-failed-error – WombatPM Sep 26 '16 at 05:06