Python ssl._create_unverified_context() Examples
The following are 30
code examples of ssl._create_unverified_context().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ssl
, or try the search function
.
Example #1
Source File: liberty_crawler.py From agentless-system-crawler with Apache License 2.0 | 11 votes |
def retrieve_status_page(user, password, url): try: ssl._create_unverified_context except AttributeError: pass else: ssl._create_default_https_context = ssl._create_unverified_context password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None, url, user, password) handler = urllib2.HTTPBasicAuthHandler(password_mgr) opener = urllib2.build_opener(handler) urllib2.install_opener(opener) req = urllib2.Request(url) try: response = urllib2.urlopen(req) return response.read() except Exception: raise CrawlError("can't access to http://%s", url)
Example #2
Source File: sms.py From Servo with BSD 2-Clause "Simplified" License | 7 votes |
def send(self, note, number): conf = Configuration.conf() if not conf.get('sms_http_url'): raise ValueError(_("No SMS HTTP gateway defined")) params = urllib.urlencode({ 'username' : conf['sms_http_user'], 'password' : conf['sms_http_password'], 'text' : note.body.encode('utf8'), 'to' : number }) f = urllib.urlopen("%s?%s" % (conf['sms_http_url'], params), context=_create_unverified_context()) return f.read()
Example #3
Source File: __main__.py From vnpy_crypto with MIT License | 6 votes |
def install_pip(): try: import pip # NOQA except ImportError: f = tempfile.NamedTemporaryFile(suffix='.py') with contextlib.closing(f): print("downloading %s to %s" % (GET_PIP_URL, f.name)) if hasattr(ssl, '_create_unverified_context'): ctx = ssl._create_unverified_context() else: ctx = None kwargs = dict(context=ctx) if ctx else {} req = urlopen(GET_PIP_URL, **kwargs) data = req.read() f.write(data) f.flush() print("installing pip") code = os.system('%s %s --user' % (PYTHON_EXE, f.name)) return code
Example #4
Source File: e2m3u2bouquet.py From e2m3u2bouquet with GNU General Public License v3.0 | 6 votes |
def _process_provider_update(self): """Download provider update file from url""" downloaded = False updated = False path = tempfile.gettempdir() filename = os.path.join(path, 'provider-{}-update.txt'.format(self.config.name)) self._update_status('----Downloading providers update file----') print('\n{}'.format(Status.message)) print('provider update url = ', self.config.provider_update_url) try: context = ssl._create_unverified_context() urllib.urlretrieve(self.config.provider_update_url, filename, context=context) downloaded = True except Exception: pass # fallback to no ssl context if not downloaded: try: urllib.urlretrieve(self.config.provider_update_url, filename) except Exception, e: print('[e2m3u2b] process_provider_update error. Type:', type(e)) print('[e2m3u2b] process_provider_update error: ', e)
Example #5
Source File: downloader.py From gibMacOS with MIT License | 6 votes |
def open_url(self, url, headers = None): # Fall back on the default ua if none provided headers = self.ua if headers == None else headers # Wrap up the try/except block so we don't have to do this for each function try: response = urlopen(Request(url, headers=headers)) except Exception as e: if sys.version_info >= (3, 0) or not (isinstance(e, urllib2.URLError) and "CERTIFICATE_VERIFY_FAILED" in str(e)): # Either py3, or not the right error for this "fix" return None # Py2 and a Cert verify error - let's set the unverified context context = ssl._create_unverified_context() try: response = urlopen(Request(url, headers=headers), context=context) except: # No fixing this - bail return None return response
Example #6
Source File: proxy_handling.py From rosreestr2coord with MIT License | 6 votes |
def ip_adress_proxies(url='https://www.ip-adress.com/proxy_list/'): # Downloading without proxy opener = urllib.request.build_opener(urllib.request.ProxyHandler()) urllib.request.install_opener(opener) request = urllib.request.Request(url) request.add_header('user-agent', USER_AGENT) parsed_uri = urlparse(url) host = '{uri.scheme}://{uri.netloc}/'.format(uri=parsed_uri) request.add_header('referer', host) s = False try: context = ssl._create_unverified_context() with urlopen(request, context=context, timeout=3000) as response: s = response.read().decode('utf-8') except Exception as er: print(er) pattern = r'\d*\.\d*\.\d*\.\d*\</a>:\d*' found = [i.replace('</a>', '') + '\n' for i in re.findall(pattern, s)] return found
Example #7
Source File: retrieve_orig_nlcd.py From Spectrum-Access-System with Apache License 2.0 | 6 votes |
def RetrieveHTTPFile(resource, force=False, write_file=''): if not write_file: write_file = resource.split('/')[-1] if os.path.exists(write_file) and not force: print('Resource %s already retrieved' % resource) return context = ssl._create_unverified_context() f = urllib.request.urlopen(resource, context=context) if f.getcode() != 200: raise Exception('Could not find resource %s' % resource) with open(write_file, 'wb') as out: while True: c = f.read(64*1024) if not c: break out.write(c) f.close()
Example #8
Source File: test_class.py From django-gcloud-storage with BSD 3-Clause "New" or "Revised" License | 6 votes |
def urlopen(*args, **kwargs): try: from urllib.request import urlopen except ImportError: from urllib2 import urlopen try: # Ignore SSL errors (won't work on Py3.3 but can be ignored there) kwargs["context"] = ssl._create_unverified_context() except AttributeError: # Py3.3 pass return urlopen(*args, **kwargs) # noinspection PyClassHasNoInit,PyMethodMayBeStatic
Example #9
Source File: request_helper.py From Moodle-Downloader-2 with GNU General Public License v3.0 | 6 votes |
def __init__(self, moodle_domain: str, moodle_path: str = '/', token: str = '', skip_cert_verify=False): """ Opens a connection to the Moodle system """ if skip_cert_verify: context = ssl._create_unverified_context() else: context = ssl.create_default_context(cafile=certifi.where()) self.connection = HTTPSConnection(moodle_domain, context=context) self.token = token self.moodle_domain = moodle_domain self.moodle_path = moodle_path RequestHelper.stdHeader = { 'User-Agent': ('Mozilla/5.0 (X11; Linux x86_64)' + ' AppleWebKit/537.36 (KHTML, like Gecko)' + ' Chrome/78.0.3904.108 Safari/537.36'), 'Content-Type': 'application/x-www-form-urlencoded' }
Example #10
Source File: sms.py From Servo with BSD 2-Clause "Simplified" License | 6 votes |
def send(self): pwhash = md5(self.conf['sms_http_password']).hexdigest() params = { 'username' : self.conf['sms_http_user'], 'password' : pwhash, 'message' : self.body, 'from' : self.sender, 'to' : self.recipient, } if self.msg: dlruri = settings.SERVO_URL + '/api/messages/?id={0}'.format(self.msg.code) params['notify_url'] = dlruri params = urllib.urlencode(params) r = urllib.urlopen(self.URL, params, context=_create_unverified_context()).read() if 'ERROR:' in r: raise ValueError(self.ERRORS.get(r, _('Unknown error (%s)') % r))
Example #11
Source File: data_loader.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 6 votes |
def load_mnist(training_num=50000): data_path = os.path.join(os.path.dirname(os.path.realpath('__file__')), 'mnist.npz') if not os.path.isfile(data_path): from six.moves import urllib origin = ( 'https://github.com/sxjscience/mxnet/raw/master/example/bayesian-methods/mnist.npz' ) print('Downloading data from %s to %s' % (origin, data_path)) ctx = ssl._create_unverified_context() with urllib.request.urlopen(origin, context=ctx) as u, open(data_path, 'wb') as f: f.write(u.read()) print('Done!') dat = numpy.load(data_path) X = (dat['X'][:training_num] / 126.0).astype('float32') Y = dat['Y'][:training_num] X_test = (dat['X_test'] / 126.0).astype('float32') Y_test = dat['Y_test'] Y = Y.reshape((Y.shape[0],)) Y_test = Y_test.reshape((Y_test.shape[0],)) return X, Y, X_test, Y_test
Example #12
Source File: note.py From Servo with BSD 2-Clause "Simplified" License | 6 votes |
def send_sms_builtin(self, recipient, sender=None): """ Sends SMS through built-in gateway """ if not settings.SMS_HTTP_URL: raise ValueError(_('System is not configured for built-in SMS support.')) if sender is None: location = self.created_by.location sender = location.title data = urllib.urlencode({ 'username' : settings.SMS_HTTP_USERNAME, 'password' : settings.SMS_HTTP_PASSWORD, 'numberto' : recipient.replace(' ', ''), 'numberfrom': sender.encode(SMS_ENCODING), 'message' : self.body.encode(SMS_ENCODING), }) from ssl import _create_unverified_context f = urllib.urlopen(settings.SMS_HTTP_URL, data, context=_create_unverified_context()) return f.read()
Example #13
Source File: utils.py From rosreestr2coord with MIT License | 6 votes |
def make_request(url, with_proxy=False): # original function if url: logger.debug(url) if with_proxy: return make_request_with_proxy(url) try: headers = get_rosreestr_headers() request = Request(url, headers=headers) context = ssl._create_unverified_context() with urlopen(request, context=context, timeout=3000) as response: read = response.read() is_error = is_error_response(url, read) if is_error: raise Exception(is_error) return read except Exception as er: logger.warning(er) print(er) # raise TimeoutException() return False
Example #14
Source File: netatmo-parser.py From rainmachine-developer-resources with GNU General Public License v3.0 | 6 votes |
def postRequest(self, url, params): params = urlencode(params) headers = {"Content-Type" : "application/x-www-form-urlencoded;charset=utf-8"} req = urllib2.Request(url=url, data=params, headers=headers) try: response = urllib2.urlopen(req) log.debug("%s?%s" % (response.geturl(), params)) return json.loads(response.read()) except urllib2.URLError, e: log.debug(e) if hasattr(ssl, '_create_unverified_context'): #for mac os only in order to ignore invalid certificates try: context = ssl._create_unverified_context() response = urllib2.urlopen(req, context=context) return json.loads(response.read()) except Exception, e: log.exception(e)
Example #15
Source File: _hm.py From pyhomematic with MIT License | 6 votes |
def __init__(self, *args, **kwargs): """ Initialize new proxy for server and get local ip """ self._remote = kwargs.pop("remote", None) self._skipinit = kwargs.pop("skipinit", False) self._callbackip = kwargs.pop("callbackip", None) self._callbackport = kwargs.pop("callbackport", None) self._ssl = kwargs.pop("ssl", False) self._verify_ssl = kwargs.pop("verify_ssl", True) self.lock = threading.Lock() if self._ssl and not self._verify_ssl and self._verify_ssl is not None: kwargs['context'] = ssl._create_unverified_context() xmlrpc.client.ServerProxy.__init__(self, encoding="ISO-8859-1", *args, **kwargs) urlcomponents = urllib.parse.urlparse(args[0]) self._remoteip = urlcomponents.hostname self._remoteport = urlcomponents.port LOG.debug("LockingServerProxy.__init__: Getting local ip") tmpsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) tmpsocket.connect((self._remoteip, self._remoteport)) self._localip = tmpsocket.getsockname()[0] tmpsocket.close() LOG.debug("LockingServerProxy.__init__: Got local ip %s" % self._localip)
Example #16
Source File: proxy2.py From selenium-wire with MIT License | 6 votes |
def _create_connection(self, origin): scheme, netloc = origin if origin not in self.tls.conns: proxy_config = self.server.proxy_config kwargs = { 'timeout': self.timeout } if scheme == 'https': connection = ProxyAwareHTTPSConnection if not self.server.options.get('verify_ssl', False): kwargs['context'] = ssl._create_unverified_context() else: connection = ProxyAwareHTTPConnection self.tls.conns[origin] = connection(proxy_config, netloc, **kwargs) return self.tls.conns[origin]
Example #17
Source File: data_loader.py From training_results_v0.6 with Apache License 2.0 | 6 votes |
def load_mnist(training_num=50000): data_path = os.path.join(os.path.dirname(os.path.realpath('__file__')), 'mnist.npz') if not os.path.isfile(data_path): from six.moves import urllib origin = ( 'https://github.com/sxjscience/mxnet/raw/master/example/bayesian-methods/mnist.npz' ) print('Downloading data from %s to %s' % (origin, data_path)) ctx = ssl._create_unverified_context() with urllib.request.urlopen(origin, context=ctx) as u, open(data_path, 'wb') as f: f.write(u.read()) print('Done!') dat = numpy.load(data_path) X = (dat['X'][:training_num] / 126.0).astype('float32') Y = dat['Y'][:training_num] X_test = (dat['X_test'] / 126.0).astype('float32') Y_test = dat['Y_test'] Y = Y.reshape((Y.shape[0],)) Y_test = Y_test.reshape((Y_test.shape[0],)) return X, Y, X_test, Y_test
Example #18
Source File: downloads.py From ungoogled-chromium with BSD 3-Clause "New" or "Revised" License | 6 votes |
def _download_via_urllib(url, file_path, show_progress, disable_ssl_verification): reporthook = None if show_progress: reporthook = _UrlRetrieveReportHook() if disable_ssl_verification: import ssl # TODO: Remove this or properly implement disabling SSL certificate verification orig_https_context = ssl._create_default_https_context #pylint: disable=protected-access ssl._create_default_https_context = ssl._create_unverified_context #pylint: disable=protected-access try: urllib.request.urlretrieve(url, str(file_path), reporthook=reporthook) finally: # Try to reduce damage of hack by reverting original HTTPS context ASAP if disable_ssl_verification: ssl._create_default_https_context = orig_https_context #pylint: disable=protected-access if show_progress: print()
Example #19
Source File: cls_api_helper.py From vsphere-automation-sdk-python with MIT License | 6 votes |
def upload_files_in_session(self, files_map, session_id): for f_name, f_path in files_map.items(): file_spec = self.client.upload_file_service.AddSpec(name=f_name, source_type=UpdateSessionFile.SourceType.PUSH, size=os.path.getsize(f_path)) file_info = self.client.upload_file_service.add(session_id, file_spec) # Upload the file content to the file upload URL with open(f_path, 'rb') as local_file: request = urllib2.Request(file_info.upload_endpoint.uri, local_file) request.add_header('Cache-Control', 'no-cache') request.add_header('Content-Length', '{0}'.format(os.path.getsize(f_path))) request.add_header('Content-Type', 'text/ovf') if self.skip_verification and hasattr(ssl, '_create_unverified_context'): # Python 2.7.9 has stronger SSL certificate validation, # so we need to pass in a context when dealing with # self-signed certificates. context = ssl._create_unverified_context() urllib2.urlopen(request, context=context) else: # Don't pass context parameter since versions of Python # before 2.7.9 don't support it. urllib2.urlopen(request)
Example #20
Source File: downloader.py From thinkpad-x1c5-hackintosh with MIT License | 6 votes |
def open_url(self, url, headers = None): # Fall back on the default ua if none provided headers = self.ua if headers == None else headers # Wrap up the try/except block so we don't have to do this for each function try: response = urlopen(Request(url, headers=headers)) except Exception as e: if sys.version_info >= (3, 0) or not (isinstance(e, urllib2.URLError) and "CERTIFICATE_VERIFY_FAILED" in str(e)): # Either py3, or not the right error for this "fix" return None # Py2 and a Cert verify error - let's set the unverified context context = ssl._create_unverified_context() try: response = urlopen(Request(url, headers=headers), context=context) except: # No fixing this - bail return None return response
Example #21
Source File: client.py From kylinpy with MIT License | 6 votes |
def _request(self, method, endpoint, params=None, json=None, headers=None, timeout=None): method = method.upper() request_data = None if headers: self._update_headers(headers) if json: request_data = _json.dumps(json).encode('utf-8') if self.unverified: opener = urllib.build_opener( HTTPSHandler(context=ssl._create_unverified_context()), ) else: opener = urllib.build_opener() request = urllib.Request(self._build_url(endpoint, params), data=request_data) if self.request_headers: for key, value in self.request_headers.items(): request.add_header(key, value) if request_data and ('Content-Type' not in self.request_headers): request.add_header('Content-Type', 'application/json') request.get_method = lambda: method return Response(self._make_request(opener, request, timeout=timeout))
Example #22
Source File: downloader.py From USBMap with MIT License | 6 votes |
def open_url(self, url, headers = None): # Fall back on the default ua if none provided headers = self.ua if headers == None else headers # Wrap up the try/except block so we don't have to do this for each function try: response = urlopen(Request(url, headers=headers)) except Exception as e: if sys.version_info >= (3, 0) or not (isinstance(e, urllib2.URLError) and "CERTIFICATE_VERIFY_FAILED" in str(e)): # Either py3, or not the right error for this "fix" return None # Py2 and a Cert verify error - let's set the unverified context context = ssl._create_unverified_context() try: response = urlopen(Request(url, headers=headers), context=context) except: # No fixing this - bail return None return response
Example #23
Source File: hsecscan.py From hsecscan with GNU General Public License v2.0 | 6 votes |
def scan(url, redirect, insecure, useragent, postdata, proxy): request = urllib2.Request(url.geturl()) request.add_header('User-Agent', useragent) request.add_header('Origin', 'http://hsecscan.com') request.add_header('Accept', '*/*') if postdata: request.add_data(urllib.urlencode(postdata)) build = [urllib2.HTTPHandler()] if redirect: build.append(RedirectHandler()) if proxy: build.append(urllib2.ProxyHandler({'http': proxy, 'https': proxy})) if insecure: context = ssl._create_unverified_context() build.append(urllib2.HTTPSHandler(context=context)) urllib2.install_opener(urllib2.build_opener(*build)) response = urllib2.urlopen(request) print '>> RESPONSE INFO <<' print_response(response.geturl(), response.getcode(), response.info()) print '>> RESPONSE HEADERS DETAILS <<' for header in response.info().items(): check_header(header) print '>> RESPONSE MISSING HEADERS <<' missing_headers(response.info().items(), url.scheme)
Example #24
Source File: addon_updator.py From Screencast-Keys with GNU General Public License v3.0 | 6 votes |
def _request(url, json_decode=True): ssl._create_default_https_context = ssl._create_unverified_context req = urllib.request.Request(url) try: result = urllib.request.urlopen(req) except urllib.error.HTTPError as e: raise RuntimeError("HTTP error ({})".format(str(e.code))) except urllib.error.URLError as e: raise RuntimeError("URL error ({})".format(str(e.reason))) data = result.read() result.close() if json_decode: try: return json.JSONDecoder().decode(data.decode()) except Exception as e: raise RuntimeError("API response has invalid JSON format ({})" .format(str(e.reason))) return data.decode()
Example #25
Source File: vsphere.py From conjure-up with MIT License | 5 votes |
def login(self): context = None if hasattr(ssl, '_create_unverified_context'): context = ssl._create_unverified_context() self.si = SmartConnect(host=self.host, user=self.username, pwd=self.password, port=self.port, sslContext=context) self._retrieve_content()
Example #26
Source File: cls_api_helper.py From vsphere-automation-sdk-python with MIT License | 5 votes |
def download_files(self, library_item_id, directory): """ Download files from a library item Args: library_item_id: id for the library item to download files from directory: location on the client machine to download the files into """ downloaded_files_map = {} # create a new download session for downloading the session files session_id = self.client.download_service.create(create_spec=DownloadSessionModel( library_item_id=library_item_id), client_token=generate_random_uuid()) file_infos = self.client.download_file_service.list(session_id) for file_info in file_infos: self.client.download_file_service.prepare(session_id, file_info.name) download_info = self.wait_for_prepare(session_id, file_info.name) if self.skip_verification and hasattr(ssl, '_create_unverified_context'): # Python 2.7.9 has stronger SSL certificate validation, # so we need to pass in a context when dealing with self-signed # certificates. context = ssl._create_unverified_context() response = urllib2.urlopen( url=download_info.download_endpoint.uri, context=context) else: # Don't pass context parameter since versions of Python # before 2.7.9 don't support it. response = urllib2.urlopen(download_info.download_endpoint.uri) file_path = os.path.join(directory, file_info.name) with open(file_path, 'wb') as local_file: local_file.write(response.read()) downloaded_files_map[file_info.name] = file_path self.client.download_service.delete(session_id) return downloaded_files_map
Example #27
Source File: lookup_service_helper.py From vsphere-automation-sdk-python with MIT License | 5 votes |
def connect(self): if self.client is None: # Suds library doesn't support passing unverified context to disable # server certificate verification. Thus disable checking globally in # order to skip verification. This is not recommended in production # code. see https://www.python.org/dev/peps/pep-0476/ if self.skip_verification: import ssl try: _create_unverified_https_context = \ ssl._create_unverified_context except AttributeError: # Legacy Python that doesn't verify HTTPS certificates by # default pass else: # Handle target environment that doesn't support HTTPS # verification ssl._create_default_https_context = \ _create_unverified_https_context self.client = Client(url=self.wsdl_url, location=self.soap_url) assert self.client is not None self.client.set_options(service='LsService', port='LsPort') self.managedObjectReference = self.client.factory.create( 'ns0:ManagedObjectReference') self.managedObjectReference._type = 'LookupServiceInstance' self.managedObjectReference.value = 'ServiceInstance' lookupServiceContent = self.client.service.RetrieveServiceContent( self.managedObjectReference) self.serviceRegistration = lookupServiceContent.serviceRegistration
Example #28
Source File: ssl_helper.py From vsphere-automation-sdk-python with MIT License | 5 votes |
def get_unverified_context(): """ Get an unverified ssl context. Used to disable the server certificate verification. @return: unverified ssl context. """ context = None if hasattr(ssl, '_create_unverified_context'): context = ssl._create_unverified_context() return context
Example #29
Source File: ssl_context.py From chia-blockchain with Apache License 2.0 | 5 votes |
def ssl_context_for_client( root_path: Path, config: Dict, auth: bool ) -> Optional[ssl.SSLContext]: paths = load_ssl_paths(root_path, config) if paths is None: return paths private_cert, private_key = paths ssl_context = ssl._create_unverified_context(purpose=ssl.Purpose.SERVER_AUTH) ssl_context.load_cert_chain(certfile=private_cert, keyfile=private_key) if auth: ssl_context.verify_mode = ssl.CERT_REQUIRED ssl_context.load_verify_locations(private_cert) else: ssl_context.verify_mode = ssl.CERT_NONE return ssl_context
Example #30
Source File: test_httpclient.py From pykit with MIT License | 5 votes |
def test_https(self): cases = ( ('/get_1b', 'a'), ('/get_10k', 'bc' * 5 * KB), ('/get_30m', 'cde' * 10 * MB), ) context = ssl._create_unverified_context() cli = http.Client(HOST, PORT, https_context=context) for uri, expected_res in cases: cli.request(uri) body = cli.read_body(None) self.assertEqual(200, cli.status) self.assertEqual(expected_res, body)