How does Python urllib2 https work?
Asked Answered
Y

1

1

Looking at the documentation for urlib2 it says it supports HTTPS connections. However what it doesn't make clear is how you enable it do you for example take HTTPBasicAuth and replace the HTTP with HTTPS or do you just need to pass an HTTPS in url when you actually open the connection?

Yardmaster answered 5/8, 2013 at 16:18 Comment(0)
S
1

< Python 2.7.9:_

You can simply pass an HTTPS URL when you open the connection. Heed the warning in the Urllib2 documentation that states:

"Warning HTTPS requests do not do any verification of the server’s certificate."

As such, I recommend using Python Requests library that provides a better interface and many features, including SSL Cert verification and Unicode support.

Update 20150120:

Python 2.7.9 Added HTTPS Hostname verification as standard. See change comment in https://docs.python.org/2/library/httplib.html#httplib.HTTPSConnection

Thanks to @EnnoGröper for the notice.

Shane answered 5/8, 2013 at 16:21 Comment(4)
The lack of cert verification is not an issue as this is being used for an internal system but the "rules" state that no passwords may be transmitted without an SSL/SSH connection. ThanksYardmaster
@Bickerx2 see update. You may find Requests easier to work with.Shane
I did look at requests but I don't have the option to use requests, as the location I have to install my script, only the standard lib are available. Also getting the an additional library added is not worth the additional work for a granted "no" responseYardmaster
The warning in the docs disappeared and at least using 2.7.9 I'm pretty sure, that urllib2.urlopen does verify certificates by default.Benthos

© 2022 - 2024 — McMap. All rights reserved.