urllib.request.HTTPPasswordMgrWithDefaultRealm

Here are the examples of the python api urllib.request.HTTPPasswordMgrWithDefaultRealm taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

13 Examples 7

Example 1

Project: trackma Source File: libmelative.py
Function: init
    def __init__(self, messenger, account, userconfig):
        """Initializes the useragent through credentials."""
        super(libmelative, self).__init__(messenger, account, userconfig)

        self.username = account['username']

        self.password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
        self.password_mgr.add_password("Melative", "melative.com:80", account['username'], account['password']);

        self.handler = urllib.request.HTTPBasicAuthHandler(self.password_mgr)
        self.opener = urllib.request.build_opener(self.handler)

        urllib.request.install_opener(self.opener)

Example 2

Project: suds-py3 Source File: https.py
Function: init
    def __init__(self, **kwargs):
        """
        @param kwargs: Keyword arguments.
            - B{proxy} - An http proxy to be specified on requests.
                 The proxy is defined as {protocol:proxy,}
                    - type: I{dict}
                    - default: {}
            - B{timeout} - Set the url open timeout (seconds).
                    - type: I{float}
                    - default: 90
            - B{username} - The username used for http authentication.
                    - type: I{str}
                    - default: None
            - B{password} - The password used for http authentication.
                    - type: I{str}
                    - default: None
        """
        HttpTransport.__init__(self, **kwargs)
        self.pm = HTTPPasswordMgrWithDefaultRealm()

Example 3

Project: py-gocd Source File: server.py
Function: add_basic_auth
    def _add_basic_auth(self):
        auth_handler = HTTPBasicAuthHandler(
            HTTPPasswordMgrWithDefaultRealm()
        )
        auth_handler.add_password(
            realm=None,
            uri=self.host,
            user=self.user,
            passwd=self.password,
        )
        install_opener(build_opener(
            auth_handler,
            HTTPHandler(debuglevel=self.request_debug_level),
            HTTPSHandler(debuglevel=self.request_debug_level),
        ))

Example 4

Project: tomcat-manager Source File: tomcat-manager.py
Function: init
	def __init__(self, url="http://localhost:8080/manager", userid=None, password=None):
		self.__managerURL = url
		self.__userid = userid
		self.__password = password
		self.hasConnected = False
		
		if userid and password:
			self.__passman = urllib.request.HTTPPasswordMgrWithDefaultRealm()
			self.__passman.add_password(None, self.__managerURL, self.__userid, self.__password)
			self.__auth_handler = urllib.request.HTTPBasicAuthHandler(self.__passman)
			self.__opener = urllib.request.build_opener(self.__auth_handler)
		else:
			self.__opener = urllib.request.build_opener()

Example 5

Project: OpenColorado-Tools-and-Utilities Source File: __init__.py
Function: init
    def __init__(self, base_location=None, api_key=None, is_verbose=False,
                 http_user=None, http_pass=None):
        if base_location is not None:
            self.base_location = base_location
        self.api_key = api_key
        self.is_verbose = is_verbose
        if http_user and http_pass:
            password_mgr = HTTPPasswordMgrWithDefaultRealm()
            password_mgr.add_password(None, base_location,
                                      http_user, http_pass)
            handler = HTTPBasicAuthHandler(password_mgr)
            opener = build_opener(handler)
            install_opener(opener)

Example 6

Project: SiCKRAGE Source File: autoProcessTV.py
def processEpisode(dir_to_process, org_NZB_name=None, status=None):
    # Default values
    host = "localhost"
    port = "8081"
    username = ""
    password = ""
    ssl = 0
    web_root = "/"

    default_url = host + ":" + port + web_root
    if ssl:
        default_url = "https://" + default_url
    else:
        default_url = "http://" + default_url

    # Get values from config_file
    config = configparser.RawConfigParser()
    config_filename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessTV.cfg")

    if not os.path.isfile(config_filename):
        print ("ERROR: " + config_filename + " doesn\'t exist")
        print ("copy /rename " + config_filename + ".sample and edit\n")
        print ("Trying default url: " + default_url + "\n")

    else:
        try:
            print ("Loading config from " + config_filename + "\n")

            with io.open(config_filename, "r") as fp:
                config.readfp(fp)

            # Replace default values with config_file values
            host = config.get("sickrage", "host")
            port = config.get("sickrage", "port")
            username = config.get("sickrage", "username")
            password = config.get("sickrage", "password")

            try:
                ssl = int(config.get("sickrage", "ssl"))

            except (configparser.NoOptionError, ValueError):
                pass

            try:
                web_root = config.get("sickrage", "web_root")
                if not web_root.startswith("/"):
                    web_root = "/" + web_root

                if not web_root.endswith("/"):
                    web_root += "/"

            except configparser.NoOptionError:
                pass

        except EnvironmentError:
            e = sys.exc_info()[1]
            print ("Could not read configuration file: " + str(e))
            # There was a config_file, don't use default values but exit
            sys.exit(1)

    params = {'quiet': 1, 'dir': dir_to_process}

    if org_NZB_name is not None:
        params['nzbName'] = org_NZB_name

    if status is not None:
        params['failed'] = status

    if ssl:
        protocol = "https://"
    else:
        protocol = "http://"

    url = protocol + host + ":" + port + web_root + "home/postprocess/processEpisode?" + urlencode(params)

    print ("Opening URL: " + url)

    try:
        password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
        password_mgr.add_password(None, url, username, password)
        handler = HTTPBasicAuthHandler(password_mgr)
        opener = urllib2.build_opener(handler)
        urllib2.install_opener(opener)

        result = opener.open(url).readlines()

        for line in result:
            if line:
                print (line.strip())

    except IOError:
        e = sys.exc_info()[1]
        print ("Unable to open URL: " + str(e))
        sys.exit(1)

Example 7

Project: canto-next Source File: fetch.py
    def run(self):

        # Initial load, just feed.index grab from disk.

        if self.fromdisk:
            self.feed.index({"entries" : []})
            return

        self.feed.last_update = time.time()

        # Otherwise, actually try to get an update.

        extra_headers = { 'User-Agent' :\
                'Canto/0.9.0 + http://codezen.org/canto-ng'}

        try:
            result = None
            # Passworded Feed
            if self.feed.username or self.feed.password:
                domain = urllib.parse.urlparse(self.feed.URL)[1]
                man = urllib.request.HTTPPasswordMgrWithDefaultRealm()
                auth = urllib.request.HTTPBasicAuthHandler(man)
                auth.handler_order = 490
                auth.add_password(None, domain, self.feed.username,
                        self.feed.password)

                try:
                    result = feedparser.parse(self.feed.URL, handlers=[auth],
                            request_headers = extra_headers)
                except:
                    # And, failing that, Digest Authentication
                    man = urllib.request.HTTPPasswordMgrWithDefaultRealm()
                    auth = urllib.request.HTTPDigestAuthHandler(man)
                    auth.handler_order = 490
                    auth.add_password(None, domain, self.feed.username,
                            self.feed.password)
                    result = feedparser.parse(self.feed.URL, handlers=[auth],
                            request_headers = extra_headers)

            # No password
            else:
                result = feedparser.parse(self.feed.URL,
                        request_headers = extra_headers)

            update_contents = result
        except Exception as e:
            log.error("ERROR: try to parse %s, got %s" % (self.feed.URL, e))
            return

        # Interpret feedparser's bozo_exception, if there was an
        # error that resulted in no content, it's the same as
        # any other broken feed.

        if "bozo_exception" in update_contents:
            if update_contents["bozo_exception"] == urllib.error.URLError:
                log.error("ERROR: couldn't grab %s : %s" %\
                        (self.feed.URL,\
                        update_contents["bozo_exception"].reason))
                return
            elif len(update_contents["entries"]) == 0:
                log.error("No content in %s: %s" %\
                        (self.feed.URL,\
                        update_contents["bozo_exception"]))
                return

            # Replace it if we ignore it, since exceptions
            # are not pickle-able.

            update_contents["bozo_exception"] = None

        # Update timestamp
        update_contents["canto_update"] = self.feed.last_update

        update_contents = json.loads(json.dumps(update_contents))

        log.debug("Parsed %s", self.feed.URL)

        # Allow DaemonFetchThreadPlugins to do any sort of fetch stuff
        # before the thread is marked as complete.

        for attr in list(self.plugin_attrs.keys()):
            if not attr.startswith("fetch_"):
                continue

            try:
                a = getattr(self, attr)
                a(feed = self.feed, newcontent = update_contents)
            except:
                log.error("Error running fetch thread plugin")
                log.error(traceback.format_exc())

        log.debug("Plugins complete.")

        # This handles it's own locking
        self.feed.index(update_contents)

Example 8

Project: canto-next Source File: test-feed-password.py
    def check(self):
        # First make sure that feedparser hasn't been broken

        domain = urllib.parse.urlparse(TEST_URL)[1]
        man = urllib.request.HTTPPasswordMgrWithDefaultRealm()
        auth = urllib.request.HTTPBasicAuthHandler(man)
        auth.handler_order = 490 # Workaround feedparser issue #283
        auth.add_password(None, domain, USER, PASS)

        f = feedparser.parse(TEST_URL, handlers=[auth])

        if f["bozo"] == 1:
            raise Exception("feedparser is broken!")

        test_shelf = {}
        test_feed = CantoFeed(test_shelf, "Passworded Feed", TEST_URL, 10, 86400, False,
                password=PASS, username=USER)

        thread = CantoFetchThread(test_feed, False)
        thread.start()
        thread.join()

        if TEST_URL not in test_shelf:
            raise Exception("Canto failed to get passworded feed")

        return True

Example 9

Project: Sick-Beard Source File: autoProcessTV.py
def processEpisode(dir_to_process, org_NZB_name=None):

    # Default values
    host = "localhost"
    port = "8081"
    username = ""
    password = ""
    ssl = 0
    web_root = "/"

    default_url = host + ":" + port + web_root
    if ssl:
        default_url = "https://" + default_url
    else:
        default_url = "http://" + default_url

    # Get values from config_file
    config = configparser.RawConfigParser()
    config_filename = os.path.join(os.path.dirname(sys.argv[0]), "autoProcessTV.cfg")

    if not os.path.isfile(config_filename):
        print ("ERROR: " + config_filename + " doesn\'t exist")
        print ("copy /rename " + config_filename + ".sample and edit\n")
        print ("Trying default url: " + default_url + "\n")

    else:
        try:
            print ("Loading config from " + config_filename + "\n")

            with open(config_filename, "r") as fp:
                config.readfp(fp)

            # Replace default values with config_file values
            host = config.get("SickBeard", "host")
            port = config.get("SickBeard", "port")
            username = config.get("SickBeard", "username")
            password = config.get("SickBeard", "password")

            try:
                ssl = int(config.get("SickBeard", "ssl"))

            except (configparser.NoOptionError, ValueError):
                pass

            try:
                web_root = config.get("SickBeard", "web_root")
                if not web_root.startswith("/"):
                    web_root = "/" + web_root

                if not web_root.endswith("/"):
                    web_root = web_root + "/"

            except configparser.NoOptionError:
                pass

        except EnvironmentError:
            e = sys.exc_info()[1]
            print ("Could not read configuration file: " + str(e))
            # There was a config_file, don't use default values but exit
            sys.exit(1)

    params = {}

    params['quiet'] = 1

    params['dir'] = dir_to_process
    if org_NZB_name != None:
        params['nzbName'] = org_NZB_name

    if ssl:
        protocol = "https://"
    else:
        protocol = "http://"

    url = protocol + host + ":" + port + web_root + "home/postprocess/processEpisode?" + urlencode(params)

    print ("Opening URL: " + url)

    try:
        password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
        password_mgr.add_password(None, url, username, password)
        handler = HTTPBasicAuthHandler(password_mgr)
        opener = urllib2.build_opener(handler)
        urllib2.install_opener(opener)

        result = opener.open(url).readlines()

        for line in result:
            if line:
                print (line.strip())

    except IOError:
        e = sys.exc_info()[1]
        print ("Unable to open URL: " + str(e))
        sys.exit(1)

Example 10

Project: git-repo Source File: main.py
def init_http():
  handlers = [_UserAgentHandler()]

  mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
  try:
    n = netrc.netrc()
    for host in n.hosts:
      p = n.hosts[host]
      mgr.add_password(p[1], 'http://%s/'  % host, p[0], p[2])
      mgr.add_password(p[1], 'https://%s/' % host, p[0], p[2])
  except netrc.NetrcParseError:
    pass
  except IOError:
    pass
  handlers.append(_BasicAuthHandler(mgr))
  handlers.append(_DigestAuthHandler(mgr))
  if kerberos:
    handlers.append(_KerberosAuthHandler())

  if 'http_proxy' in os.environ:
    url = os.environ['http_proxy']
    handlers.append(urllib.request.ProxyHandler({'http': url, 'https': url}))
  if 'REPO_CURL_VERBOSE' in os.environ:
    handlers.append(urllib.request.HTTPHandler(debuglevel=1))
    handlers.append(urllib.request.HTTPSHandler(debuglevel=1))
  urllib.request.install_opener(urllib.request.build_opener(*handlers))

Example 11

Project: gpodder-core Source File: util.py
Function: url_open
def urlopen(url, headers=None, data=None, timeout=None):
    """
    An URL opener with the User-agent set to gPodder (with version)
    """
    username, password = username_password_from_url(url)
    if username is not None or password is not None:
        url = url_strip_authentication(url)
        password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
        password_mgr.add_password(None, url, username, password)
        handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
        opener = urllib.request.build_opener(handler)
    else:
        opener = urllib.request.build_opener()

    if headers is None:
        headers = {}
    else:
        headers = dict(headers)

    # Allow the calling code to supply a custom user-agent
    if 'User-agent' not in headers:
        headers['User-agent'] = gpodder.user_agent

    request = urllib.request.Request(url, data=data, headers=headers)
    if timeout is None:
        return opener.open(request)
    else:
        return opener.open(request, timeout=timeout)

Example 12

Project: mygpo Source File: utils.py
def urlopen(url, headers=None, data=None):
    """
    An URL opener with the User-agent set to gPodder (with version)
    """
    username, password = username_password_from_url(url)
    if username is not None or password is not None:
        url = url_strip_authentication(url)
        password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
        password_mgr.add_password(None, url, username, password)
        handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
        opener = urllib.request.build_opener(handler)
    else:
        opener = urllib.request.build_opener()

    if headers is None:
        headers = {}
    else:
        headers = dict(headers)

    headers.update({'User-agent': settings.USER_AGENT})
    request = urllib.request.Request(url, data=data, headers=headers)
    return opener.open(request)

Example 13

Project: tango_with_django_19 Source File: bing_search.py
Function: run_query
def run_query(search_terms):
	
	bing_api_key = read_bing_key()
	if not bing_api_key:
		raise KeyError('Bing Key Not Found')
	
	# Specify the base url and the service (Bing Search API 2.0)
	root_url = 'https://api.datamarket.azure.com/Bing/Search/'
	service = 'Web'

	# Specify how many results we wish to be returned per page.
	# Offset specifies where in the results list to start from.
	# With results_per_page = 10 and offset = 11, this would start from page 2.
	results_per_page = 10
	offset = 0

	# Wrap quotes around our query terms as required by the Bing API.
	# The query we will then use is stored within variable query.
	query = "'{0}'".format(search_terms)
	
	# Turn the query into an HTML encoded string.
	# We use urllib for this - differences exist between Python 2 and 3.
	# The try/except blocks are used to determine which function call works.
	# Replace this try/except block with the relevant import and query assignment.
	try:
		from urllib import parse  # Python 3 import.
		query = parse.quote(query)
	except ImportError:  # If the import above fails, you are running Python 2.7.x.
		from urllib import quote
		query = quote(query)
	
	# Construct the latter part of our request's URL.
	# Sets the format of the response to JSON and sets other properties.
	search_url = "{0}{1}?$format=json&$top={2}&$skip={3}&Query={4}".format(
		root_url,
		service,
		results_per_page,
		offset,
		query)

	# Setup authentication with the Bing servers.
	# The username MUST be a blank string, and put in your API key!
	username = ''

	#headers = {'Authorization' : 'Basic {0}'.format( b64encode(bing_api_key) )}
	# Create a 'password manager' which handles authentication for us.
	
	try:
		from urllib import request  # Python 3 import.
		password_mgr = request.HTTPPasswordMgrWithDefaultRealm()
	except ImportError:  # Running Python 2.7.x - import urllib2 instead.
		import urllib2
		password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
	
	password_mgr.add_password(None, search_url, username, bing_api_key)

	# Create our results list which we'll populate.
	results = []

	try:
		# Prepare for connecting to Bing's servers.
		try:  # Python 3.5 and 3.6
			handler = request.HTTPBasicAuthHandler(password_mgr)
			opener = request.build_opener(handler)
			request.install_opener(opener)
		except UnboundLocalError:  # Python 2.7.x
			handler = urllib2.HTTPBasicAuthHandler(password_mgr)
			opener = urllib2.build_opener(handler)
			urllib2.install_opener(opener)
	
		# Connect to the server and read the response generated.
		try:  # Python 3.5 or 3.6
			response = request.urlopen(search_url).read()
			response = response.decode('utf-8')
		except UnboundLocalError:  # Python 2.7.x
			response = urllib2.urlopen(search_url).read()
	
		# Convert the string response to a Python dictionary object.
		json_response = json.loads(response)
	
		# Loop through each page returned, populating out results list.
		for result in json_response['d']['results']:
			results.append({
				'title': result['Title'],
				'link': result['Url'],
				'summary': result['Description']})
	except:
		print("Error when querying the Bing API")
	
	# Return the list of results to the calling function.
	return results