Some beginning python3 porting for urls.py

This commit is contained in:
Toshio Kuratomi 2016-04-27 07:15:51 -07:00
parent c2efb0b7e6
commit 2583f2774d

View file

@ -81,7 +81,6 @@
# agrees to be bound by the terms and conditions of this License # agrees to be bound by the terms and conditions of this License
# Agreement. # Agreement.
import httplib
import netrc import netrc
import os import os
import re import re
@ -91,7 +90,13 @@ import platform
import tempfile import tempfile
import base64 import base64
from ansible.module_utils.basic import get_distribution from ansible.module_utils.basic import get_distribution, get_exception
try:
import httplib
except ImportError:
# Python 3
import http.client as httplib
try: try:
import urllib2 import urllib2
@ -664,7 +669,8 @@ class SSLValidationHandler(urllib2.BaseHandler):
# close the ssl connection # close the ssl connection
#ssl_s.unwrap() #ssl_s.unwrap()
s.close() s.close()
except (ssl.SSLError, socket.error), e: except (ssl.SSLError, socket.error):
e = get_exception()
# fail if we tried all of the certs but none worked # fail if we tried all of the certs but none worked
if 'connection refused' in str(e).lower(): if 'connection refused' in str(e).lower():
raise ConnectionError('Failed to connect to %s:%s.' % (self.hostname, self.port)) raise ConnectionError('Failed to connect to %s:%s.' % (self.hostname, self.port))
@ -888,27 +894,33 @@ def fetch_url(module, url, data=None, headers=None, method=None,
follow_redirects=follow_redirects) follow_redirects=follow_redirects)
info.update(r.info()) info.update(r.info())
info.update(dict(msg="OK (%s bytes)" % r.headers.get('Content-Length', 'unknown'), url=r.geturl(), status=r.getcode())) info.update(dict(msg="OK (%s bytes)" % r.headers.get('Content-Length', 'unknown'), url=r.geturl(), status=r.getcode()))
except NoSSLError, e: except NoSSLError:
e = get_exception()
distribution = get_distribution() distribution = get_distribution()
if distribution is not None and distribution.lower() == 'redhat': if distribution is not None and distribution.lower() == 'redhat':
module.fail_json(msg='%s. You can also install python-ssl from EPEL' % str(e)) module.fail_json(msg='%s. You can also install python-ssl from EPEL' % str(e))
else: else:
module.fail_json(msg='%s' % str(e)) module.fail_json(msg='%s' % str(e))
except (ConnectionError, ValueError), e: except (ConnectionError, ValueError):
e = get_exception()
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))
except urllib2.HTTPError, e: except urllib2.HTTPError:
e = get_exception()
try: try:
body = e.read() body = e.read()
except AttributeError: except AttributeError:
body = '' body = ''
info.update(dict(msg=str(e), body=body, **e.info())) info.update(dict(msg=str(e), body=body, **e.info()))
info['status'] = e.code info['status'] = e.code
except urllib2.URLError, e: except urllib2.URLError:
e = get_exception()
code = int(getattr(e, 'code', -1)) code = int(getattr(e, 'code', -1))
info.update(dict(msg="Request failed: %s" % str(e), status=code)) info.update(dict(msg="Request failed: %s" % str(e), status=code))
except socket.error, e: except socket.error:
e = get_exception()
info.update(dict(msg="Connection failure: %s" % str(e), status=-1)) info.update(dict(msg="Connection failure: %s" % str(e), status=-1))
except Exception, e: except Exception:
e = get_exception()
info.update(dict(msg="An unknown error occurred: %s" % str(e), status=-1)) info.update(dict(msg="An unknown error occurred: %s" % str(e), status=-1))
return r, info return r, info