Skip to content

Proposed fix to #1180 - Make 'pip search' support http proxy #1898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions pip/commands/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from functools import reduce

from pip.basecommand import Command, SUCCESS
from pip.util import get_terminal_size
from pip.util import get_terminal_size, UrllibTransport
from pip.log import logger
from pip.compat import cmp
from pip.exceptions import CommandError
Expand Down Expand Up @@ -37,8 +37,9 @@ def run(self, options, args):
raise CommandError('Missing required argument (search query).')
query = args
index_url = options.index
proxy_url = options.proxy

pypi_hits = self.search(query, index_url)
pypi_hits = self.search(query, index_url, proxy_url)
hits = transform_hits(pypi_hits)

terminal_width = None
Expand All @@ -50,8 +51,11 @@ def run(self, options, args):
return SUCCESS
return NO_MATCHES_FOUND

def search(self, query, index_url):
pypi = xmlrpc_client.ServerProxy(index_url)
def search(self, query, index_url, proxy_url=None):
transport = None
if not proxy_url is None:
transport = UrllibTransport(index_url, proxy_url)
pypi = xmlrpc_client.ServerProxy(index_url, transport)
hits = pypi.search({'name': query, 'summary': query}, 'or')
return hits

Expand Down
40 changes: 40 additions & 0 deletions pip/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
from pip._vendor import pkg_resources, six
from pip._vendor.distlib import version
from pip._vendor.six.moves import input
from pip._vendor.six.moves import urllib
from pip._vendor.six.moves import xmlrpc_client

__all__ = ['rmtree', 'display_path', 'backup_dir',
'find_command', 'ask', 'Inf',
Expand Down Expand Up @@ -781,3 +783,41 @@ def readline(self):

def __iter__(self):
return self._gen


class UrllibTransport(xmlrpc_client.Transport):
"""Provides HTTP proxy support for Python's xmlrpclib, via urllib.

Original code borrowed from https://gist.github.com/nathforge/980961
"""
def __init__(self, index_url, proxy_url=None, use_datetime=False,
use_builtin_types=False):
# initialize its own and parent's attributes
xmlrpc_client.Transport.__init__(self, use_datetime)
self._use_builtin_types = use_builtin_types
index_parts = urllib.parse.urlsplit(index_url)
self._index_scheme = index_parts.scheme
# build the opener and set up the proxy settings
opener = urllib.request.build_opener()
if not proxy_url is None:
proxies = {}
if len(proxy_url) > 0:
proxies[self._index_scheme] = proxy_url
opener.add_handler(urllib.request.ProxyHandler(proxies))
self._opener = opener

def request(self, host, handler, request_body, verbose=False):
# collect request's params
index_netloc, extra_headers, x509 = self.get_host_info(host)
request_parts = (self._index_scheme, index_netloc, handler, None, None)
request_url = urllib.parse.urlunsplit(request_parts)
# build a request object
request = urllib.request.Request(request_url, request_body)
request.add_header('Content-Type', 'text/xml')
request.add_header('User-Agent', self.user_agent)
if not extra_headers is None:
for header, value in extra_headers:
request.add_header(header, value)
# send the request and parse it
self.verbose = verbose
return self.parse_response(self._opener.open(request))