Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between requests.get() and urrlib.request.urlopen() python [duplicate]

Tags:

python

When I use:

import requests
r = requests.get("https://example.com")  

I get the following exception:

requests.exceptions.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)

However, If i use following code:

url = "https://www.example.com"
request = urllib.request.urlopen(url)

It gives me the correct response[200] code. Why is it so? What is the difference between these two methods and which one should be preferred?

like image 813
Anmol Bhatia Avatar asked Jun 30 '16 05:06

Anmol Bhatia


1 Answers

Use the former one: I will add the source of why it's better. Anyways you need to set verify as False to prevent requests from verifying SSL certificates for HTTPS requests:

import requests
r = requests.get("https://example.com", verify=False)

Edit:

Difference between requests.get() and urllib.request.urlopen() python

What are the differences between the urllib, urllib2, and requests module?

like image 73
Devi Prasad Khatua Avatar answered Sep 19 '22 10:09

Devi Prasad Khatua