HTTPS connection Python
Asked Answered
B

8

49

I am trying to verify the that target exposes a https web service. I have code to connect via HTTP but I am not sure how to connect via HTTPS. I have read you use SSL but I have also read that it did not support certificate errors. The code I have got is from the python docs:

import httplib
conn = httplib.HTTPConnection("www.python.org")
conn.request("GET", "/index.html")
r1 = conn.getresponse()
print r1.status, r1.reason

Does anyone know how to connect to HTTPS?

I already tried the HTTPSConenction but it responds with an error code claiming httplib does not have attribute HTTPSConnection. I also don't have socket.ssl available.

I have installed Python 2.6.4 and I don't think it has SSL support compiled into it. Is there a way to integrate this suppot into the newer python without having to install it again.

I have installed OpenSSL and pyOpenSsl and I have tried the below code from one of the answers:

import urllib2
from OpenSSL import SSL
try: 
    response = urllib2.urlopen('https://example.com')  
    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

I have got an error:

    Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/home/build/workspace/downloads/Python-2.6.4/Lib/urllib.py", line 87, in urlopen
    return opener.open(url)
  File "/home/build/workspace/downloads/Python-2.6.4/Lib/urllib.py", line 203, in open
    return self.open_unknown(fullurl, data)
  File "/home/build/workspace/downloads/Python-2.6.4/Lib/urllib.py", line 215, in open_unknown
    raise IOError, ('url error', 'unknown url type', type)
IOError: [Errno url error] unknown url type: 'https'

Does anyone know how to get this working?

-- UPDATE

I have found out what the problem was, the Python version I was using did not have support for SSL. I have found this solution currently at: http://www.webtop.com.au/compiling-python-with-ssl-support.

The code will now work after this solution which is very good. When I import ssl and HTTPSConnection I know don't get an error.

Thanks for the help all.

Benedix answered 27/1, 2010 at 11:22 Comment(0)
W
68

Python 2.x: docs.python.org/2/library/httplib.html:

Note: HTTPS support is only available if the socket module was compiled with SSL support.

Python 3.x: docs.python.org/3/library/http.client.html:

Note HTTPS support is only available if Python was compiled with SSL support (through the ssl module).

#!/usr/bin/env python

import httplib
c = httplib.HTTPSConnection("ccc.de")
c.request("GET", "/")
response = c.getresponse()
print response.status, response.reason
data = response.read()
print data
# => 
# 200 OK
# <!DOCTYPE html ....

To verify if SSL is enabled, try:

>>> import socket
>>> socket.ssl
<function ssl at 0x4038b0>
Wart answered 27/1, 2010 at 11:33 Comment(3)
Note that this doesn't verify the remote certificate, therefore you don't really get security this way (as you don't really know which server you're really talking to).Delila
Wrong url in tequestHospers
how to check IP with this response ?Forgotten
A
14

To check for ssl support in Python 2.6+:

try:
    import ssl
except ImportError:
    print "error: no ssl support"

To connect via https:

import urllib2

try:
    response = urllib2.urlopen('https://example.com') 
    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
Audsley answered 27/1, 2010 at 12:53 Comment(0)
O
11
import requests
r = requests.get("https://stackoverflow.com") 
data = r.content  # Content of response

print r.status_code  # Status code of response
print data
Ogbomosho answered 22/5, 2015 at 14:50 Comment(2)
@notilas, this code does work. If you just print r, you will get a string version of the response object itself rather than the content of the response, which is what you typed above, <Resposne [200]>. That is simply saying that the request was successful (200 is the HTTP code for success). Try looking at r.content or r.text (or r.json() if you're content is JSON) to get the actual result of the request.Christology
@Christology Thanks for kind explanation.Expressly
C
9

using

class httplib.HTTPSConnection

http://docs.python.org/library/httplib.html#httplib.HTTPSConnection

Claudiaclaudian answered 27/1, 2010 at 11:23 Comment(0)
F
6

If using httplib.HTTPSConnection:

Please take a look at:

Changed in version 2.7.9:

This class now performs all the necessary certificate and hostname checks by default. To revert to the previous, unverified, behavior ssl._create_unverified_context() can be passed to the context parameter. You can use:

if hasattr(ssl, '_create_unverified_context'):
  ssl._create_default_https_context = ssl._create_unverified_context
Firing answered 30/10, 2015 at 7:39 Comment(0)
D
5

Why haven't you tried httplib.HTTPSConnection? It doesn't do SSL validation but this isn't required to connect over https. Your code works fine with https connection:

>>> import httplib
>>> conn = httplib.HTTPSConnection("mail.google.com")
>>> conn.request("GET", "/")
>>> r1 = conn.getresponse()
>>> print r1.status, r1.reason
200 OK
Dreamland answered 27/1, 2010 at 11:27 Comment(0)
M
4

Assuming SSL support is enabled for the socket module.

connection1 = httplib.HTTPSConnection('www.somesecuresite.com')
Monies answered 27/1, 2010 at 11:55 Comment(0)
I
-1

I had some code that was failing with an HTTPConnection (MOVED_PERMANENTLY error), but as soon as I switched to HTTPS it worked perfectly again with no other changes needed. That's a very simple fix!

Industrialize answered 14/11, 2016 at 19:10 Comment(1)
How is this different compared to other answers? Maybe try to elaborate a bit more.Epicontinental

© 2022 - 2024 — McMap. All rights reserved.