def test_proxy(self, proxy: Optional[str]) -> None: session = PipSession(trusted_hosts=[]) if not proxy: # if user didn't pass --proxy then try to get it from the system. env_proxy = getproxies().get("http", None) proxy = urlparse(env_proxy).netloc if env_proxy else None if proxy: # set proxy scheme to session.proxies session.proxies = { "http": f"{proxy}", "https": f"{proxy}", "ftp": f"{proxy}", } connection_error = None try: session.request("GET", "https://pypi.org", timeout=1) except requests.exceptions.ConnectionError as e: connection_error = e assert connection_error is None, ( f"Invalid proxy {proxy} or session.proxies: " f"{session.proxies} is not correctly passed to session.request.")
def _build_session(self, options, retries=None, timeout=None): # type: (Values, Optional[int], Optional[int]) -> PipSession assert not options.cache_dir or os.path.isabs(options.cache_dir) session = PipSession( cache=(os.path.join(options.cache_dir, "http") if options.cache_dir else None), retries=retries if retries is not None else options.retries, trusted_hosts=options.trusted_hosts, index_urls=self._get_index_urls(options), ) # Handle custom ca-bundles from the dashboard if options.cert: session.verify = options.cert # Handle SSL client certificate if options.client_cert: session.cert = options.client_cert # Handle timeouts if options.timeout or timeout: session.timeout = (timeout if timeout is not None else options.timeout) # Handle configured proxies if options.proxy: session.proxies = { "http": options.proxy, "https": options.proxy, } # Determine if we can prompt the dashboard for authentication or not session.auth.prompting = not options.no_input return session
def _build_session( self, options: Values, retries: Optional[int] = None, timeout: Optional[int] = None, fallback_to_certifi: bool = False, ) -> PipSession: cache_dir = options.cache_dir assert not cache_dir or os.path.isabs(cache_dir) if "truststore" in options.features_enabled: try: ssl_context = _create_truststore_ssl_context() except Exception: if not fallback_to_certifi: raise ssl_context = None else: ssl_context = None session = PipSession( cache=os.path.join(cache_dir, "http") if cache_dir else None, retries=retries if retries is not None else options.retries, trusted_hosts=options.trusted_hosts, index_urls=self._get_index_urls(options), ssl_context=ssl_context, ) # Handle custom ca-bundles from the user if options.cert: session.verify = options.cert # Handle SSL client certificate if options.client_cert: session.cert = options.client_cert # Handle timeouts if options.timeout or timeout: session.timeout = timeout if timeout is not None else options.timeout # Handle configured proxies if options.proxy: session.proxies = { "http": options.proxy, "https": options.proxy, } # Determine if we can prompt the user for authentication or not session.auth.prompting = not options.no_input return session