예제 #1
0
 def setUp(self):
     self.temp_dir = self._GenerateTestData()
     multiplexer = event_multiplexer.EventMultiplexer(
         size_guidance=application.DEFAULT_SIZE_GUIDANCE,
         purge_orphaned_data=True)
     plugins = {}
     app = application.TensorBoardWSGIApp(self.temp_dir,
                                          plugins,
                                          multiplexer,
                                          reload_interval=0)
     self._server = serving.BaseWSGIServer('localhost', 0, app)
     # 0 to pick an unused port.
     self._server_thread = threading.Thread(
         target=self._server.serve_forever)
     self._server_thread.daemon = True
     self._server_thread.start()
     self._connection = http_client.HTTPConnection(
         'localhost', self._server.server_address[1])
예제 #2
0
 def testDataPaths_disableAllCaching(self):
   """Test the format of the /data/runs endpoint."""
   for path in ('/data/runs',
                '/data/logdir',
                '/data/scalars?run=run1&tag=simple_values',
                '/data/scalars?run=run1&tag=simple_values&format=csv',
                '/data/images?run=run1&tag=image',
                '/data/individualImage?run=run1&tag=image&index=0',
                '/data/audio?run=run1&tag=audio',
                '/data/run_metadata?run=run1&tag=test%20run'):
     connection = http_client.HTTPConnection(
         'localhost', self._server.server_address[1])
     connection.request('GET', path)
     response = connection.getresponse()
     self.assertEqual(response.status, 200, msg=path)
     self.assertEqual(response.getheader('Expires'), '0', msg=path)
     response.read()
     connection.close()
예제 #3
0
 def __init__(self, uri_or_host, port=None, path=None, customThrift=False):
     """THttpClient supports two different types constructor parameters.
     THttpClient(host, port, path) - deprecated
     THttpClient(uri)
     Only the second supports https.
     """
     if port is not None:
         warnings.warn(
             "Please use the THttpClient('http://host:port/path') syntax",
             DeprecationWarning,
             stacklevel=2
         )
         self.host = uri_or_host
         self.port = port
         assert path
         self.path = path
         self.scheme = 'http'
     else:
         parsed = urllib.parse.urlparse(uri_or_host)
         self.scheme = parsed.scheme
         assert self.scheme in ('http', 'https')
         if self.scheme == 'http':
             self.port = parsed.port or http_client.HTTP_PORT
         elif self.scheme == 'https':
             self.port = parsed.port or http_client.HTTPS_PORT
         self.host = parsed.hostname
         self.path = parsed.path
         if parsed.query:
             self.path += '?%s' % parsed.query
     proxy = None
     self.realhost = self.realport = self.proxy_auth = None
     self.__wbuf = BytesIO()
     if customThrift:
         if self.scheme == 'http':
             self.__http = http_client.HTTPConnection(self.host, self.port)
         elif self.scheme == 'https':
             self.__http = http_client.HTTPSConnection(self.host, self.port)
     else:
         self.__http = None
     self.__http_response = None
     self.__timeout = None
     self.__custom_headers = None
     self.__time = time.time()
     self.__custom_thrift = customThrift
예제 #4
0
 def _CreateDB(self):
   """This method is idempotent. If the DB already exists it will simply
   return a 200 code without re-creating it.
   """
   successful_http_request_codes = [200, 202, 204]
   header = {'Content-type': 'application/x-www-form-urlencoded',
             'Accept': 'text/plain'}
   params = urllib.parse.urlencode(
       {'q': 'CREATE DATABASE ' + self.influx_db_name})
   conn = httplib.HTTPConnection(self.influx_uri)
   conn.request('POST', '/query?' + params, headers=header)
   response = conn.getresponse()
   conn.close()
   if response.status in successful_http_request_codes:
     logging.debug('Success! %s DB Created', self.influx_db_name)
   else:
     logging.error('%d Request could not be completed due to: %s',
                   response.status, response.reason)
     raise httplib.HTTPException
예제 #5
0
    def check(self):
        conn = http_client.HTTPConnection('fw.telldus.com:80')
        try:
            conn.request(
                'GET', "/versions.xml?mac=%s&hw=%s&product=%s" %
                (Board.getMacAddr(), self.hw(), Board.product()))
            response = conn.getresponse()
        except Exception as error:
            logging.warning("Could not get version info: %s", error)
            return False
        parser = xml.parsers.expat.ParserCreate()

        parser.StartElementHandler = self._startElement
        parser.EndElementHandler = self._endElement
        parser.CharacterDataHandler = self._characterDataHandler
        parser.Parse(response.read())
        if self._url != '':
            return True
        return False
예제 #6
0
def _make_http_connection(init, mode, uri=None):
    """
    Creates a HTTPConnection instance if the HTTP server supports
    Range requests, otherwise falls back to a generic InputStream.
    """
    from six.moves import http_client

    parsed = urlparse.urlparse(init)
    connection = http_client.HTTPConnection(parsed.netloc)
    connection.connect()

    block_size = io.DEFAULT_BUFFER_SIZE

    # We request a range of the whole file ("0-") to check if the
    # server understands that header entry, and also to get the
    # size of the entire file
    headers = {'Range': 'bytes=0-'}
    connection.request('GET', parsed.path, headers=headers)
    response = connection.getresponse()
    if response.status // 100 != 2:
        raise IOError("HTTP failed: {0} {1}".format(response.status,
                                                    response.reason))

    # Status 206 means a range was returned.  If it's anything else
    # that indicates the server probably doesn't support Range
    # headers.
    if (response.status != 206
            or response.getheader('accept-ranges', None) != 'bytes'
            or response.getheader('content-range', None) is None
            or response.getheader('content-length', None) is None):
        # Fall back to a regular input stream, but we don't
        # need to open a new connection.
        response.close = connection.close
        return InputStream(response, mode, uri=uri or init, close=True)

    # Since we'll be requesting chunks, we can't read at all with the
    # current request (because we can't abort it), so just close and
    # start over
    size = int(response.getheader('content-length'))
    first_chunk = response.read(block_size)
    response.close()
    return HTTPConnection(connection, size, parsed.path, uri or init,
                          first_chunk)
예제 #7
0
def wait_for_http_server(host, port, sleep_amount=0.1, sleep_tries=150):
    """Wait for an HTTP server to boot up."""
    # Test if the server is up
    for i in range(sleep_tries):
        # directly test the app, not the proxy
        conn = http_client.HTTPConnection(host, port)
        try:
            conn.request("GET", "/")
            response = conn.getresponse()
            if response.status == 200:
                break
        except socket.error as e:
            if e.errno not in [61, 111]:
                raise
        time.sleep(sleep_amount)
    else:
        template = "Test HTTP server on host %s and port %s did not return '200 OK' after 10 tries"
        message = template % (host, port)
        raise Exception(message)
예제 #8
0
def do_http(method, url, body=b""):
    if isinstance(body, bytes):
        body = BytesIO(body)
    elif isinstance(body, six.text_type):
        raise TypeError("do_http body must be a bytestring, not unicode")
    else:
        # We must give a Content-Length header to twisted.web, otherwise it
        # seems to get a zero-length file. I suspect that "chunked-encoding"
        # may fix this.
        assert body.tell
        assert body.seek
        assert body.read
    scheme, host, port, path = parse_url(url)
    if scheme == "http":
        c = http_client.HTTPConnection(host, port)
    elif scheme == "https":
        c = http_client.HTTPSConnection(host, port)
    else:
        raise ValueError("unknown scheme '%s', need http or https" % scheme)
    c.putrequest(method, path)
    c.putheader("Hostname", host)
    c.putheader("User-Agent", allmydata.__full_version__ + " (tahoe-client)")
    c.putheader("Accept", "text/plain, application/octet-stream")
    c.putheader("Connection", "close")

    old = body.tell()
    body.seek(0, os.SEEK_END)
    length = body.tell()
    body.seek(old)
    c.putheader("Content-Length", str(length))

    try:
        c.endheaders()
    except socket_error as err:
        return BadResponse(url, err)

    while True:
        data = body.read(8192)
        if not data:
            break
        c.send(data)

    return c.getresponse()
예제 #9
0
def upload_to(host, filename, port=7777):
    """
    Simple method to upload a file into NGAS
    """
    with contextlib.closing(httplib.HTTPConnection(host, port)) as conn:
        conn.putrequest('POST', '/QARCHIVE?filename=%s' % (
            urlparse.quote(os.path.basename(filename)),))
        conn.putheader('Content-Length', os.stat(filename).st_size)
        conn.endheaders()
        with open(filename) as f:
            for data in iter(functools.partial(f.read, 4096), ''):
                conn.send(data)
        r = conn.getresponse()
        if r.status != httplib.OK:
            raise Exception("Error while QARCHIVE-ing %s to %s:%d:\nStatus: "
                            "%d\n%s\n\n%s" % (filename, conn.host, conn.port,
                                              r.status, r.msg, r.read()))
        else:
            success("{0} successfully archived to {1}!".format(filename, host))
예제 #10
0
def replication_dump(options, args):
    """%(prog)s dump <server:port> <path>

    Dump the contents of a glance instance to local disk.

    server:port: the location of the glance instance.
    path:        a directory on disk to contain the data.
    """

    # Make sure server and path are provided
    if len(args) < 2:
        raise TypeError(_("Too few arguments."))

    path = args.pop()
    server, port = utils.parse_valid_host_port(args.pop())

    imageservice = get_image_service()
    client = imageservice(http_client.HTTPConnection(server, port),
                          options.mastertoken)
    for image in client.get_images():
        LOG.debug('Considering: %s' % image['id'])

        data_path = os.path.join(path, image['id'])
        if not os.path.exists(data_path):
            LOG.info(_LI('Storing: %s') % image['id'])

            # Dump glance information
            with open(data_path, 'w') as f:
                f.write(jsonutils.dumps(image))

            if image['status'] == 'active' and not options.metaonly:
                # Now fetch the image. The metadata returned in headers here
                # is the same as that which we got from the detailed images
                # request earlier, so we can ignore it here. Note that we also
                # only dump active images.
                LOG.debug('Image %s is active' % image['id'])
                image_response = client.get_image(image['id'])
                with open(data_path + '.img', 'wb') as f:
                    while True:
                        chunk = image_response.read(options.chunksize)
                        if not chunk:
                            break
                        f.write(chunk)
예제 #11
0
 def request(self,
             method,
             bucket_name=None,
             url=None,
             headers=None,
             body=None,
             sub_resources=None):
     headers = HeaderDict() if headers is None else headers.copy()
     if 'x-amz-date' not in headers:
         headers['x-amz-date'] = \
             datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S +0000')
     if self.iam_token is not None:
         self._refresh_iam_credentials()
         headers['x-amz-security-token'] = self.iam_token
     headers['Authorization'] = self.sign(method, bucket_name, url, headers,
                                          sub_resources)
     while True:
         try:
             if self.connection is None:
                 self.connection = httplib.HTTPConnection(self.host)
             self.connection.request(method, url, body, headers)
             response = self.connection.getresponse()
             if response.status not in xrange(200, 300):
                 raise S3Error(method, url, body, headers, response)
             headers = HeaderDict(response.getheaders())
             body = response.read()
             return (headers, body)
         except httplib.BadStatusLine as exc:
             logger.warn(exc)
             self.connection = None
         except httplib.CannotSendRequest as exc:
             logger.warn(exc)
             self.connection = None
         except ssl.SSLError as exc:
             logger.warn(exc)
             self.connection = None
         except IOError as exc:
             if exc.errno == errno.ECONNRESET:
                 logger.warn(exc)
                 self.connection = None
             else:
                 raise
예제 #12
0
 def open(self):
     if self.request == 'hyper':
         if self.http2:
             self.__http = hyper.HTTP20Connection(
                 self.host,
                 self.port,
                 proxy_host=self.realhost,
                 proxy_port=self.realport,
                 proxy_headers=self.proxy_headers)
         else:
             self.__http = hyper.HTTPConnection(
                 self.host,
                 self.port,
                 proxy_host=self.realhost,
                 proxy_port=self.realport,
                 proxy_headers=self.proxy_headers)
     elif self.request == 'httpx':
         if self.http2:
             self.__http = httpx.AsyncClient(base_url='%s://%s' %
                                             (self.scheme, self.host),
                                             http2=self.http2)
         else:
             self.__http = httpx.Client(base_url='%s://%s' %
                                        (self.scheme, self.host))
     elif self.request == 'requests':
         self.__http = requests.Session()
         if self.using_proxy():
             self.__http.proxies = urllib.request.getproxies()
     elif self.request == 'requests-futures':
         self.__http = FuturesSession()
         if self.using_proxy():
             self.__http.proxies = urllib.request.getproxies()
     elif self.request == 'httplib2':
         self.__http = httplib2.Http()
     else:
         if self.scheme == 'http':
             self.__http = http_client.HTTPConnection(self.host, self.port)
         elif self.scheme == 'https':
             self.__http = http_client.HTTPSConnection(self.host, self.port)
             if self.using_proxy():
                 self.__http.set_tunnel(self.realhost, self.realport,
                                        self.proxy_headers)
예제 #13
0
    def _connect(self):
        # Host and port for the HTTP(S) connection to the API server.
        if self.ca_certs == 'HTTP':
            api_port = 80
        else:
            api_port = 443
        if self.port is not None:
            api_port = self.port

        # Host and port for outer HTTP(S) connection if proxied.
        if self.proxy_type is None:
            host = self.host
            port = api_port
        elif self.proxy_type == 'CONNECT':
            host = self.proxy_host
            port = self.proxy_port
        else:
            raise NotImplementedError('proxy_type=%s' % (self.proxy_type, ))

        # Create outer HTTP(S) connection.
        if self.ca_certs == 'HTTP':
            conn = http_client.HTTPConnection(host, port)
        elif self.ca_certs == 'DISABLE':
            conn = http_client.HTTPSConnection(host, port)
        else:
            conn = CertValidatingHTTPSConnection(host,
                                                 port,
                                                 ca_certs=self.ca_certs)

        # Override default socket timeout if requested.
        conn.timeout = self.timeout

        # Configure CONNECT proxy tunnel, if any.
        if self.proxy_type == 'CONNECT':
            if hasattr(conn, 'set_tunnel'):  # 2.7+
                conn.set_tunnel(self.host, api_port, self.proxy_headers)
            elif hasattr(conn, '_set_tunnel'):  # 2.6.3+
                # pylint: disable=E1103
                conn._set_tunnel(self.host, api_port, self.proxy_headers)
                # pylint: enable=E1103

        return conn
예제 #14
0
    def request(self, method, path, body=None, headers=None):

        if not headers:
            headers = {}

        headers.update(self.default_headers)

        if not self.port:
            self.port = 443 if self.secure else 80

        if self.secure:
            ctx = ssl.SSLContext(ssl.PROTOCOL_TLS)
            self.http_conn = http_client.HTTPSConnection(self.host,
                                                         self.port,
                                                         timeout=self.timeout,
                                                         context=ctx)
        else:
            self.http_conn = http_client.HTTPConnection(self.host,
                                                        self.port,
                                                        timeout=self.timeout)

        if self.authenticator:
            self.authenticator.add_auth(headers)

        self.http_conn.request(method, self.root + path, body, headers=headers)
        response = self.http_conn.getresponse()
        body = response.read()
        try:
            data = json.loads(body)
        except ValueError:
            data = body

        if response.status in [401, 403]:
            raise errors.AuthenticationError()
        elif response.status == 500:
            raise errors.InternalServerError()
        elif response.status == 400:
            raise errors.BadRequestError(body)
        elif response.status == 404:
            raise errors.NotFoundError()

        return (response, data)
    def _account_request(self, account, method, headers=None):
        if headers is None:
            headers = {}
        headers['X-Auth-Token'] = self.token

        scheme, netloc, path, _, _, _ = urlparse(self.url)
        host, port = netloc.split(':')
        port = int(port)

        conn = http_client.HTTPConnection(host, port)
        conn.request(method, self._account_path(account), headers=headers)
        resp = conn.getresponse()
        if resp.status // 100 != 2:
            raise Exception("Unexpected status %s\n%s" %
                            (resp.status, resp.read()))

        response_headers = dict(resp.getheaders())
        response_body = resp.read()
        resp.close()
        return response_headers, response_body
예제 #16
0
def test_request_info_put(server):
    con = http_client.HTTPConnection("localhost", server.server_port)
    with closing(con):
        content = "it works!"
        con.request("PUT", "/request-info/arg", body=content.encode("utf-8"))
        r = con.getresponse()
        assert r.status == http.OK
        info = json.loads(r.read())

    assert info["method"] == "PUT"
    assert info["uri"] == "/request-info/arg"
    assert info["path"] == "/request-info/arg"
    assert info["arg"] == "arg"
    assert info["query"] == {}
    assert info["version"] == "HTTP/1.1"
    assert info["content_length"] == len(content)
    assert info["content"] == content
    assert info["headers"]["accept-encoding"] == "identity"
    assert info["headers"]["content-length"] == str(len(content))
    assert info["content_range"] is None
예제 #17
0
def test_request_info_get(server):
    con = http_client.HTTPConnection("localhost", server.server_port)
    with closing(con):
        con.request("GET", "/request-info/arg")
        r = con.getresponse()
        assert r.status == http.OK
        info = json.loads(r.read())

    assert info["method"] == "GET"
    assert info["uri"] == "/request-info/arg"
    assert info["path"] == "/request-info/arg"
    assert info["arg"] == "arg"
    assert info["query"] == {}
    assert info["version"] == "HTTP/1.1"
    assert info["content_length"] is None
    assert info["content"] is None
    assert info["headers"]["host"] == "localhost:%d" % server.server_port
    assert info["headers"]["accept-encoding"] == "identity"
    assert info["client_addr"] == "127.0.0.1"
    assert info["range"] is None
예제 #18
0
 def __connect_with_server(self, get_operatoin, server_reuest_type):
     """Function connect with server and downloades avaliable subtitle
     list or avaliable subtitle zip file
     """
     what_is_downloaded = server_reuest_type
     self.XML_String = None
     self.zip_string = None
     try:
         conn = httplib.HTTPConnection(self.NAPISY24_url)
         conn.request("GET", get_operatoin)
         r1 = conn.getresponse()
         print(r1.status, r1.reason)
         if what_is_downloaded == "downloada_subtitle_list_by_film_name" or what_is_downloaded == "downloada_subtitle_list_by_IMDB":
             self.XML_String = r1.read()
         elif what_is_downloaded == "download_subtilte_zip":
             self.zip_string = r1.read()
         return r1.status  #, r1.reason
     except (IOError, OSError) as e:
         print("Napisy24.pl server connection error.", file=sys.stderr)
         time.sleep(0.5)
예제 #19
0
    def __init_connection(self, url=None):
        """Function for initiating connection with remote server

        :param url: The URL of the remote system
        :type url: str

        """
        self.__destroy_connection()

        url = url if url else self.__url
        if url.scheme.upper() == "HTTPS":
            if sys.version_info < (2, 7, 9):
                self._conn = http_client.HTTPSConnection(url.netloc)
            else:
                self._conn = http_client.HTTPSConnection(url.netloc, \
                                    context=ssl._create_unverified_context())
        elif url.scheme.upper() == "HTTP":
            self._conn = http_client.HTTPConnection(url.netloc)
        else:
            pass
예제 #20
0
def main(unused_argv=None):
    target = FLAGS.target
    logdir = FLAGS.logdir
    if not target or not logdir:
        PrintAndLog('Both --target and --logdir are required.',
                    tf.logging.ERROR)
        return -1
    if os.path.exists(target):
        if FLAGS.overwrite:
            if os.path.isdir(target):
                shutil.rmtree(target)
            else:
                os.remove(target)
        else:
            PrintAndLog(
                'Refusing to overwrite target %s without --overwrite' % target,
                tf.logging.ERROR)
            return -2
    path_to_run = server.ParseEventFilesSpec(FLAGS.logdir)

    PrintAndLog('About to load Multiplexer. This may take some time.')
    multiplexer = event_multiplexer.EventMultiplexer(
        size_guidance=server.TENSORBOARD_SIZE_GUIDANCE,
        purge_orphaned_data=FLAGS.purge_orphaned_data)
    server.ReloadMultiplexer(multiplexer, path_to_run)

    PrintAndLog('Multiplexer load finished. Starting TensorBoard server.')
    s = server.BuildServer(multiplexer, 'localhost', 0)
    server_thread = threading.Thread(target=s.serve_forever)
    server_thread.daemon = True
    server_thread.start()
    connection = http_client.HTTPConnection('localhost', s.server_address[1])

    PrintAndLog('Server setup! Downloading data from the server.')
    x = TensorBoardStaticSerializer(connection, target)
    x.Run()

    PrintAndLog('Done downloading data.')
    connection.close()
    s.shutdown()
    s.server_close()
    def _check_reload(self, server_name, ip, port):
        manager = Manager([server_name])
        manager.start()

        starting_pids = set(pid for server in manager.servers
                            for (_, pid) in server.iter_pid_files())

        body = 'test' * 10
        conn = httplib.HTTPConnection('%s:%s' % (ip, port))

        # sanity request
        putrequest(conn, 'PUT', 'blah', headers={'Content-Length': len(body)})
        conn.send(body)
        resp = conn.getresponse()
        self.assertEqual(resp.status // 100, 4)
        resp.read()

        manager.reload()

        post_reload_pids = set(pid for server in manager.servers
                               for (_, pid) in server.iter_pid_files())

        # none of the pids we started with are being tracked after reload
        msg = 'expected all pids from %r to have died, but found %r' % (
            starting_pids, post_reload_pids)
        self.assertFalse(starting_pids & post_reload_pids, msg)

        # ... and yet we can keep using the same connection!
        putrequest(conn, 'PUT', 'blah', headers={'Content-Length': len(body)})
        conn.send(body)
        resp = conn.getresponse()
        self.assertEqual(resp.status // 100, 4)
        resp.read()

        # close our connection
        conn.close()

        # sanity
        post_close_pids = set(pid for server in manager.servers
                              for (_, pid) in server.iter_pid_files())
        self.assertEqual(post_reload_pids, post_close_pids)
예제 #22
0
    def _connect(self):
        ''' Initialize an HTTP connection with chunked Transfer-Encoding
        to server:port with optional headers.
        '''
        server = self._server
        port = self._port
        headers = self._headers
        self._conn = http_client.HTTPConnection(server, port)

        self._conn.putrequest('POST', self._url)
        self._conn.putheader('Transfer-Encoding', 'chunked')
        for header in headers:
            self._conn.putheader(header, headers[header])
        self._conn.endheaders()

        # Set blocking to False prevents recv
        # from blocking while waiting for a response.
        self._conn.sock.setblocking(False)
        self._bytes = six.b('')
        self._reset_retries()
        time.sleep(0.5)
예제 #23
0
def get_appliance_template(request, pk):
    logger.info('Getting and displaying YAML template for appliance')

    appliance = Appliance.objects.filter(pk=pk).get()

    # send message to GA
    if settings.GOOGLE_ANALYTICS_PROPERTY_ID:
        try:
            params = urllib.parse.urlencode({'v': 1,
                                             'tid': settings.GOOGLE_ANALYTICS_PROPERTY_ID,
                                             'cid': uuid.uuid1(),
                                             't': 'event',
                                             'ec': 'heat_template',
                                             'ea': 'download',
                                             'el': '{}-{}'.format(pk, appliance.name),
                                             'ev': 0})
            conn = http_client.HTTPConnection('www.google-analytics.com')
            conn.request('POST', '/collect', params)
        except:
            logger.exception("Failed to report to Google Analytics")
    return HttpResponse(appliance.template, content_type="text/yaml")
예제 #24
0
def get_public_ip():
    """Return the IP address that outgoing network connections appear to come
    from.

    This is also the IP address to use for inbound access. Note that in
    addition, an supplied service with the "external" flag set needs to be
    defined in Ravello.
    """
    # This uses ipify.org. See www.ipify.org. This is a free service running
    # open source code deployed on Heroku. ON the web site the author says he
    # intends to keep it around for years to come.
    conn = http_client.HTTPConnection('api.ipify.org', 80)
    try:
        conn.request('GET', '/')
        resp = conn.getresponse()
        if resp.status != http_client.OK:
            raise RuntimeError('api.ipify.org status {}'.format(resp.status))
        body = resp.read()
    finally:
        conn.close()
    return body
예제 #25
0
def __GetElementTree(protocol, server, port, path, sslContext, httpProxyHost=None, httpProxyPort=None):
   """
   Private method that returns a root from ElementTree for a remote XML document.

   @param protocol: What protocol to use for the connection (e.g. https or http).
   @type  protocol: string
   @param server: Which server to connect to.
   @type  server: string
   @param port: Port
   @type  port: int
   @param path: Path
   @type  path: string
   @param sslContext: SSL Context describing the various SSL options. It is only
                      supported in Python 2.7.9 or higher.
   @type  sslContext: SSL.Context
   """

   if httpProxyHost:
      kwargs = {"context": sslContext} if sslContext else {}
      conn = http_client.HTTPSConnection(httpProxyHost, port=httpProxyPort, **kwargs)
      conn.set_tunnel(server, port)
   elif protocol == "https":
      kwargs = {"context": sslContext} if sslContext else {}
      # IPv6 requires None as the port
      if port:
         server = server + ":" + str(port)
      conn = http_client.HTTPSConnection(server, None, **kwargs)
   elif protocol == "http":
      conn = http_client.HTTPConnection(server, port=port)
   else:
      raise Exception("Protocol " + protocol + " not supported.")
   conn.request("GET", path)
   response = conn.getresponse()
   if response.status == 200:
      try:
         tree = ElementTree.fromstring(response.read())
         return tree
      except ExpatError:
         pass
   return None
예제 #26
0
파일: restutils.py 프로젝트: koniolog/dfms
    def _request(self, url, method, content=None, headers={}):

        # Do the HTTP stuff...
        logger.debug("Sending %s request to %s:%d%s", method, self.host,
                     self.port, url)

        if not utils.portIsOpen(self.host, self.port, self.timeout):
            raise RestClientException(
                "Cannot connect to %s:%d after %.2f [s]" %
                (self.host, self.port, self.timeout))

        if content and hasattr(content, 'read'):
            headers['Transfer-Encoding'] = 'chunked'
            content = chunked(content)

        self._conn = httplib.HTTPConnection(self.host, self.port)
        self._conn.request(method, url, content, headers)
        self._resp = self._conn.getresponse()

        # Server errors are encoded in the body as json content
        if self._resp.status != httplib.OK:

            msg = 'Error on remote %s@%s:%s%s (status %d): ' % \
                  (method, self.host, self.port, url, self._resp.status)

            try:
                error = json.loads(self._resp.read().decode('utf-8'))
                etype = getattr(exceptions, error['type'])
                eargs = error['args']
                ex = etype(*eargs)
                if hasattr(ex, 'msg'):
                    ex.msg = msg + ex.msg
            except Exception:
                ex = RestClientException(msg + "Unknown")

            raise ex

        if not self._resp.length:
            return None
        return codecs.getreader('utf-8')(self._resp)
예제 #27
0
파일: util.py 프로젝트: zoritle/chromium
def DoesUrlExist(url):
    """Determines whether a resource exists at the given URL.

  Args:
    url: URL to be verified.

  Returns:
    True if url exists, otherwise False.
  """
    parsed = urllib.parse.urlparse(url)
    try:
        conn = http_client.HTTPConnection(parsed.netloc)
        conn.request('HEAD', parsed.path)
        response = conn.getresponse()
    except http_client.HTTPException:
        return False
    finally:
        conn.close()
    # Follow both permanent (301) and temporary (302) redirects.
    if response.status == 302 or response.status == 301:
        return DoesUrlExist(response.getheader('location'))
    return response.status == 200
예제 #28
0
def make_request(conn,
                 method,
                 resource,
                 parameters=None,
                 sign_parameters=False,
                 extra_parameters=None):
    """generic request sending to pubsub radogw
    should cover: topics, notificatios and subscriptions
    """
    url_params = ''
    if parameters is not None:
        url_params = urlparse.urlencode(parameters)
        # remove 'None' from keys with no values
        url_params = url_params.replace('=None', '')
        url_params = '?' + url_params
    if extra_parameters is not None:
        url_params = url_params + '&' + extra_parameters
    string_date = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
    string_to_sign = method + '\n\n\n' + string_date + '\n' + resource
    if sign_parameters:
        string_to_sign += url_params
    signature = base64.b64encode(
        hmac.new(conn.aws_secret_access_key.encode('utf-8'),
                 string_to_sign.encode('utf-8'),
                 hashlib.sha1).digest()).decode('ascii')
    headers = {
        'Authorization': 'AWS ' + conn.aws_access_key_id + ':' + signature,
        'Date': string_date,
        'Host': conn.host + ':' + str(conn.port)
    }
    http_conn = http_client.HTTPConnection(conn.host, conn.port)
    if log.getEffectiveLevel() <= 10:
        http_conn.set_debuglevel(5)
    http_conn.request(method, resource + url_params, NO_HTTP_BODY, headers)
    response = http_conn.getresponse()
    data = response.read()
    status = response.status
    http_conn.close()
    return data.decode('utf-8'), status
예제 #29
0
 def __init__(self, url, user_credentials, ca_file=None):
     parsedurl = urlparse.urlparse(url)
     self._url = parsedurl.geturl()
     self._netloc = parsedurl.netloc
     self._ca_file = ca_file
     if parsedurl.scheme == 'https':
         if self._ca_file:
             self._connection = HTTPSConnectionWithCaVerification(
                 self._netloc, ca_file=self._ca_file.name)
         else:
             self._connection = http_client.HTTPSConnection(self._netloc)
             LOG.warning(
                 _LW("Will not verify the server certificate of the API service"
                     " because the CA certificate is not available."))
     else:
         self._connection = http_client.HTTPConnection(self._netloc)
     self._id = 0
     self._fail_fast = True
     self._credentials = BasicAuthCredentials(user_credentials[0],
                                              user_credentials[1])
     self._require_cert_verify = self._ca_file is not None
     self._disabled_cert_verification = False
예제 #30
0
 def setUp(self):
   self.temp_dir = self._GenerateTestData()
   multiplexer = event_multiplexer.EventMultiplexer(
       size_guidance=application.DEFAULT_SIZE_GUIDANCE,
       purge_orphaned_data=True)
   plugins = [
       FakePlugin(plugin_name='foo', is_active_value=True, routes_mapping={}),
       FakePlugin(plugin_name='bar', is_active_value=False, routes_mapping={})
   ]
   app = application.TensorBoardWSGIApp(
       self.temp_dir, plugins, multiplexer, reload_interval=0)
   try:
     self._server = serving.BaseWSGIServer('localhost', 0, app)
     # 0 to pick an unused port.
   except IOError:
     # BaseWSGIServer has a preference for IPv4. If that didn't work, try again
     # with an explicit IPv6 address.
     self._server = serving.BaseWSGIServer('::1', 0, app)
   self._server_thread = threading.Thread(target=self._server.serve_forever)
   self._server_thread.daemon = True
   self._server_thread.start()
   self._connection = http_client.HTTPConnection(
       'localhost', self._server.server_address[1])