Python urllib.response() Examples

The following are 11 code examples of urllib.response(). 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 urllib , or try the search function .
Example #1
Source File: jenkins.py    From bob with GNU General Public License v3.0 6 votes vote down vote up
def checkPlugins(self):
        try:
            with self._send("GET", "pluginManager/api/python?depth=1") as response:
                plugins =  ast.literal_eval(response.read().decode("utf8"))["plugins"]
                required = set(requiredPlugins.keys())
                for p in plugins:
                    if p["shortName"] not in required: continue
                    if not p["active"] or not p["enabled"]:
                        raise BuildError("Plugin not enabled: " + requiredPlugins[p["shortName"]])
                    required.remove(p["shortName"])
                if required:
                    raise BuildError("Missing plugin(s): " + ", ".join(
                        requiredPlugins[p] for p in required))
        except BuildError:
            raise
        except urllib.error.HTTPError as e:
            print("Warning: could not verify plugins: HTTP error: {} {}"
                    .format(e.code, e.reason),
                file=sys.stderr)
        except:
            raise BuildError("Malformed Jenkins response while checking plugins!") 
Example #2
Source File: jenkins.py    From bob with GNU General Public License v3.0 5 votes vote down vote up
def fetchConfig(self, name):
        try:
            with self._send("GET", "job/" + name + "/config.xml") as response:
                return response.read()
        except urllib.error.HTTPError as e:
            if e.code == 404:
                return None
            raise BuildError("Warning: could not download '{}' job config: HTTP error: {} {}"
                    .format(name, e.code, e.reason)) 
Example #3
Source File: test_standard_library.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 5 votes vote down vote up
def test_urllib_imports_moves(self):
        import future.moves.urllib
        import future.moves.urllib.parse
        import future.moves.urllib.request
        import future.moves.urllib.robotparser
        import future.moves.urllib.error
        import future.moves.urllib.response
        self.assertTrue(True) 
Example #4
Source File: test_standard_library.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 5 votes vote down vote up
def test_urllib_imports_install_aliases(self):
        with standard_library.suspend_hooks():
            standard_library.install_aliases()
            import urllib
            import urllib.parse
            import urllib.request
            import urllib.robotparser
            import urllib.error
            import urllib.response
            self.assertTrue(True) 
Example #5
Source File: test_standard_library.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 5 votes vote down vote up
def test_urllib_imports_cm(self):
        with standard_library.hooks():
            import urllib
            import urllib.parse
            import urllib.request
            import urllib.robotparser
            import urllib.error
            import urllib.response
        self.assertTrue(True) 
Example #6
Source File: test_standard_library.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 5 votes vote down vote up
def test_urllib_imports_install_hooks(self):
        standard_library.remove_hooks()
        standard_library.install_hooks()
        import urllib
        import urllib.parse
        import urllib.request
        import urllib.robotparser
        import urllib.error
        import urllib.response
        self.assertTrue(True) 
Example #7
Source File: test_standard_library.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 5 votes vote down vote up
def test_install_aliases(self):
        """
        Does the install_aliases() interface monkey-patch urllib etc. successfully?
        """
        from future.standard_library import remove_hooks, install_aliases
        remove_hooks()
        install_aliases()

        from collections import Counter, OrderedDict   # backported to Py2.6
        from collections import UserDict, UserList, UserString

        # Requires Python dbm support:
        # import dbm
        # import dbm.dumb
        # import dbm.gnu
        # import dbm.ndbm

        from itertools import filterfalse, zip_longest

        from subprocess import check_output    # backported to Py2.6
        from subprocess import getoutput, getstatusoutput

        from sys import intern

        # test_support may not be available (e.g. on Anaconda Py2.6):
        # import test.support

        import urllib.error
        import urllib.parse
        import urllib.request
        import urllib.response
        import urllib.robotparser

        self.assertTrue('urlopen' in dir(urllib.request)) 
Example #8
Source File: test_imports_urllib.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 5 votes vote down vote up
def test_urllib(self):
        """
        Tests that urllib isn't changed from under our feet. (This might not
        even be a problem?)
        """
        from future import standard_library
        import urllib
        orig_file = urllib.__file__
        with standard_library.hooks():
            import urllib.response
        self.assertEqual(orig_file, urllib.__file__) 
Example #9
Source File: navigation.py    From addon with GNU General Public License v3.0 5 votes vote down vote up
def _json(url):
    with closing(urllib2.urlopen(url)) as response:
        if response.code >= 300 and response.code <= 307:
            # Pause currently playing Quasar file to avoid doubling requests
            if xbmc.Player().isPlaying() and ADDON_ID in xbmc.Player().getPlayingFile():
                xbmc.Player().pause()
            _infoLabels = InfoLabels(getInfoLabels())
            
            item = xbmcgui.ListItem(
                    path=response.geturl(),
                    label=_infoLabels["label"],
                    label2=_infoLabels["label2"])

            item.setArt({
                "thumb": _infoLabels["artthumb"],
                "poster": _infoLabels["artposter"],
                "tvshowposter": _infoLabels["arttvshowposter"],
                "banner": _infoLabels["artbanner"],
                "fanart": _infoLabels["artfanart"],
                "clearart": _infoLabels["artclearart"],
                "clearlogo": _infoLabels["artclearlogo"],
                "landscape": _infoLabels["artlandscape"],
                "icon": _infoLabels["articon"]
            })

            item.setInfo(type='Video', infoLabels=_infoLabels)
            xbmcplugin.setResolvedUrl(HANDLE, True, item)
            return

        payload = response.read()

        try:
            if payload:
                return json.loads(payload)
        except:
            raise Exception(payload) 
Example #10
Source File: jenkins.py    From bob with GNU General Public License v3.0 4 votes vote down vote up
def __init__(self, config, sslVerify):
        self.__config = config
        url = self.__config["url"]
        self.__headers = { "Content-Type": "application/xml" }
        self.__root = "{}://{}{}{}".format(url['scheme'], url['server'],
            ":{}".format(url['port']) if url.get('port') else "", url['path'])

        handlers = []

        # Handle cookies
        cookies = http.cookiejar.CookieJar()
        handlers.append(urllib.request.HTTPCookieProcessor(cookies))

        # Optionally disable SSL certificate checks
        if not sslVerify:
            handlers.append(urllib.request.HTTPSHandler(
                context=ssl.SSLContext(ssl.PROTOCOL_SSLv23)))

        # handle authorization
        username = url.get("username")
        if username is not None:
            username = urllib.parse.unquote(username)
            passwd = url.get("password")
            if passwd is None:
                passwd = getpass.getpass()
            else:
                passwd = urllib.parse.unquote(passwd)
            userPass = username + ":" + passwd
            self.__headers['Authorization'] = 'Basic ' + base64.b64encode(
                userPass.encode("utf-8")).decode("ascii")

        # remember basic settings
        self.__opener = urllib.request.build_opener(*handlers)

        # get CSRF token
        try:
            with self._send("GET", "crumbIssuer/api/xml") as response:
                resp = xml.etree.ElementTree.fromstring(response.read())
                crumb = resp.find("crumb").text
                field = resp.find("crumbRequestField").text
                self.__headers[field] = crumb
        except urllib.error.HTTPError:
            pass 
Example #11
Source File: navigation.py    From addon with GNU General Public License v3.0 4 votes vote down vote up
def getInfoLabels():
    id_list = [int(s) for s in sys.argv[0].split("/") if s.isdigit()]
    tmdb_id = id_list[0] if id_list else None
                                   
    if not tmdb_id:
        parsed_url = urlparse.urlparse(sys.argv[0] + sys.argv[2])
        query = urlparse.parse_qs(parsed_url.query)
        log.debug("Parsed URL: %s, Query: %s", repr(parsed_url), repr(query))
        if 'tmdb' in query and 'type' in query and query['type'][0] == 'movie':
            tmdb_id = query['tmdb'][0]
            url = "%s/movie/%s/infolabels" % (QUASARD_HOST, tmdb_id)
        elif 'show' in query:
            tmdb_id = query['show'][0]
            if 'season' in query and 'episode' in query:
                url = "%s/show/%s/season/%s/episode/%s/infolabels" % (QUASARD_HOST, tmdb_id, query['season'][0], query['episode'][0])
            else:
                url = "%s/show/%s/infolabels" % (QUASARD_HOST, tmdb_id)
        else:
            url = "%s/infolabels" % (QUASARD_HOST)
    elif 'movie' in sys.argv[0]:
        url = "%s/movie/%s/infolabels" % (QUASARD_HOST, tmdb_id)
    elif ('episode' in sys.argv[0] or 'show' in sys.argv[0]) and len(id_list) > 2:
        url = "%s/show/%s/season/%s/episode/%s/infolabels" % (QUASARD_HOST, tmdb_id, id_list[1], id_list[2])
    elif 'show' in sys.argv[0] and len(id_list) == 2:
        url = "%s/show/%s/season/%s/episode/%s/infolabels" % (QUASARD_HOST, tmdb_id, id_list[1], 1)
    else:
        url = "%s/infolabels" % (QUASARD_HOST)

    log.debug("Resolving TMDB item by calling %s for %s" % (url, repr(sys.argv)))

    try:
        with closing(urllib2.urlopen(url)) as response:
            resolved = json.loads(response.read(), parse_int=str)
            if not resolved:
                return {}

            if 'info' in resolved and resolved['info']:
                resolved.update(resolved['info'])

            if 'art' in resolved and resolved['art']:
                resolved['artbanner'] = ''
                for k, v in list(resolved['art'].items()):
                    resolved['art' + k] = v

            if 'info' in resolved:
                del resolved['info']
            if 'art' in resolved:
                del resolved['art']
            if 'stream_info' in resolved:
                del resolved['stream_info']

            if 'dbtype' not in resolved:
                resolved['dbtype'] = 'video'
            if 'mediatype' not in resolved or resolved['mediatype'] == '':
                resolved['Mediatype'] = resolved['dbtype']

            return resolved
    except:
        log.debug("Could not resolve TMDB item: %s" % tmdb_id)
        return {}