Exemple #1
0
 def request(self, url, query, headers, timeout):
     request = Request(url, query.encode('utf-8'), headers)
     try:
         if (sys.version_info[0] == 2
                 and sys.version_info[1] > 5) or sys.version_info[0] > 2:
             response = self.http_opener.open(request, timeout=timeout)
         else:
             response = self.http_opener.open(request)
     except HTTPError as error:
         if error.fp is None:
             raise HTTPHandlerError(error.filename, error.code, error.msg,
                                    dict(error.hdrs))
         else:
             raise HTTPHandlerError(error.filename, error.code, error.msg,
                                    dict(error.hdrs), error.read())
     except URLError as error:
         # urllib2.URLError documentation is horrendous!
         # Try to get the tuple arguments of URLError
         if hasattr(error.reason, 'args') and isinstance(
                 error.reason.args, tuple) and len(error.reason.args) == 2:
             raise HTTPHandlerError(httpcode=error.reason.args[0],
                                    httpmsg=error.reason.args[1])
         else:
             raise HTTPHandlerError(httpmsg='urllib2.URLError: %s' %
                                    (error.reason))
     except BadStatusLine as error:
         raise HTTPHandlerError(httpmsg='httplib.BadStatusLine: %s' %
                                (error.line))
     return response.read().decode('utf-8')
class DefaultHTTPHandler(HTTPHandler):
    """
    The default HTTP handler provided with transmissionrpc.
    """
    def __init__(self):
        HTTPHandler.__init__(self)

    def set_authentication(self, uri, login, password):
        password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
        password_manager.add_password(realm=None, uri=uri, user=login, passwd=password)
        opener = urllib2.build_opener(
            urllib2.HTTPBasicAuthHandler(password_manager)
            , urllib2.HTTPDigestAuthHandler(password_manager)
            )
        urllib2.install_opener(opener)

    def request(self, url, query, headers, timeout):
        request = urllib2.Request(url, query, headers)
        try:
            if (sys.version_info[0] == 2 and sys.version_info[1] > 5) or sys.version_info[0] > 2:
                response = urllib2.urlopen(request, timeout=timeout)
            else:
                response = urllib2.urlopen(request)
        except urllib2.HTTPError, error:
            if error.fp is None:
                raise HTTPHandlerError(error.filename, error.code, error.msg, dict(error.hdrs))
            else:
                raise HTTPHandlerError(error.filename, error.code, error.msg, dict(error.hdrs), error.read())
        except urllib2.URLError, error:
            # urllib2.URLError documentation is horrendous!
            # Try to get the tuple arguments of URLError
            if hasattr(error.reason, 'args') and isinstance(error.reason.args, tuple) and len(error.reason.args) == 2:
                raise HTTPHandlerError(httpcode=error.reason.args[0], httpmsg=error.reason.args[1])
            else:
                raise HTTPHandlerError(httpmsg='urllib2.URLError: %s' % (error.reason))
 def request(self, url, query, headers, timeout):
     request = urllib2.Request(url, query, headers)
     try:
         if (sys.version_info[0] == 2 and sys.version_info[1] > 5) or sys.version_info[0] > 2:
             response = urllib2.urlopen(request, timeout=timeout)
         else:
             response = urllib2.urlopen(request)
     except urllib2.HTTPError, error:
         if error.fp is None:
             raise HTTPHandlerError(error.filename, error.code, error.msg, dict(error.hdrs))
         else:
             raise HTTPHandlerError(error.filename, error.code, error.msg, dict(error.hdrs), error.read())
Exemple #4
0
    def request(self, url, query, headers, timeout):
        request_headers = ['{}: {}'.format(k, v) for k, v in headers.items()]

        if self._authorization:
            request_headers += [
                'Authorization: Basic {}'.format(self._authorization)
            ]

        buf1, buf2 = BytesIO(), BytesIO()
        c = pycurl.Curl()
        c.setopt(pycurl.URL, url)
        c.setopt(pycurl.TIMEOUT, int(timeout))
        c.setopt(pycurl.POSTFIELDS, query)
        c.setopt(pycurl.HTTPHEADER, request_headers)
        c.setopt(pycurl.WRITEFUNCTION, buf1.write)
        c.setopt(pycurl.HEADERFUNCTION, buf2.write)
        c.perform()

        response_code = c.getinfo(pycurl.RESPONSE_CODE)
        c.close()
        response_headers = self._response_headers(buf2)
        encoding = self._response_encoding(response_headers)
        response_body = buf1.getvalue().decode(encoding)

        if response_code == 200:
            return response_body
        else:
            raise HTTPHandlerError(url, response_code, None, response_headers,
                                   response_body)
Exemple #5
0
 def request(self, url: str, query: str, headers: Dict[Any, Any],
             timeout: int) -> str:
     headers = {**self.auth, **headers}
     request = Request(url, query.encode('utf-8'), headers)
     try:
         if (sys.version_info[0] == 2
                 and sys.version_info[1] > 5) or sys.version_info[0] > 2:
             response = self.http_opener.open(request, timeout=timeout)
         else:
             response = self.http_opener.open(request)
     except HTTPError as http_error:
         if http_error.fp is None:  # type: ignore
             raise HTTPHandlerError(http_error.filename,
                                    http_error.code, http_error.msg,
                                    dict(http_error.hdrs))  # type: ignore
         else:
             raise HTTPHandlerError(
                 http_error.filename,
                 http_error.code,
                 http_error.msg,  # type: ignore
                 dict(http_error.hdrs),
                 http_error.read())  # type: ignore
     except URLError as url_error:
         # urllib2.URLError documentation is horrendous!
         # Try to get the tuple arguments of URLError
         if hasattr(url_error.reason, 'args') and isinstance(
                 url_error.reason.args, tuple) and len(
                     url_error.reason.args) == 2:  # type: ignore
             raise HTTPHandlerError(
                 httpcode=url_error.reason.args[0],
                 httpmsg=url_error.reason.args[1])  # type: ignore
         else:
             raise HTTPHandlerError(httpmsg='urllib2.URLError: %s' %
                                    url_error.reason)  # type: ignore
     except BadStatusLine as line_error:
         raise HTTPHandlerError(httpmsg='httplib.BadStatusLine: %s' %
                                line_error.line)  # type: ignore
     return response.read().decode('utf-8')
Exemple #6
0
    def request(self, url, query, headers, timeout):
        request = urllib2.Request(url, query, headers)
        try:
            if (sys.version_info[0] == 2
                    and sys.version_info[1] > 5) or sys.version_info[0] > 2:
                response = urllib2.urlopen(request, timeout=timeout)
            else:
                response = urllib2.urlopen(request)
        except urllib2.HTTPError, error:
            if error.fp == None:
                raise HTTPHandlerError(error.filename, error.code, error.msg,
                                       dict(error.hdrs))
            else:
                raise HTTPHandlerError(error.filename, error.code, error.msg,
                                       dict(error.hdrs), error.read())
        except urllib2.URLError, error:
            # urllib2.URLError documentation is absymal!
            # Try to get the tuple arguments of URLError
            if hasattr(error.reason, 'args') and isinstance(
                    error.reason.args, tuple) and len(error.reason.args) == 2:
                raise HTTPHandlerError(httpcode=error.reason.args[0],
                                       httpmsg=error.reason.args[1])
            else:
                raise HTTPHandlerError(httpmsg='urllib2.URLError: %s' %
                                       (error.reason))
        except httplib.BadStatusLine, error:
            raise HTTPHandlerError(httpmsg='httplib.BadStatusLine: %s' %
                                   (error.line))
        return response.read()