python类HTTPSConnection()的实例源码

slack.py 文件源码 项目:msgiver 作者: kitaro-tn 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def connect(self):
        try:
            self.__connect = http_client.HTTPSConnection("slack.com")
            return self.__connect
        except Exception as e:
            raise ConnectionError("Failed connection.")
vdi_handler.py 文件源码 项目:os-xenapi 作者: openstack 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def _vhd_stream_to_vdi(self, vhd_file_parser, vdi_ref, file_size):

        headers = {'Content-Type': 'application/octet-stream',
                   'Content-Length': '%s' % file_size}

        if self.host_url.scheme == 'http':
            conn = httplib.HTTPConnection(self.host_url.netloc)
        elif self.host_url.scheme == 'https':
            conn = httplib.HTTPSConnection(self.host_url.netloc)

        vdi_import_path = utils.get_vdi_import_path(
            self.session, self.task_ref, vdi_ref)
        try:
            conn.connect()
        except Exception:
            LOG.error('Failed connecting to host: %s', self.host_url.netloc)
            raise exception.HostConnectionFailure(
                host_netloc=self.host_url.netloc)

        try:
            conn.request('PUT', vdi_import_path, headers=headers)
            # Send the data already processed by vhd file parser firstly;
            # then send the remaining data from the stream.
            conn.send(vhd_file_parser.cached_buff)
            remain_size = file_size - len(vhd_file_parser.cached_buff)
            file_obj = vhd_file_parser.src_file
            while remain_size >= CHUNK_SIZE:
                chunk = file_obj.read(CHUNK_SIZE)
                remain_size -= CHUNK_SIZE
                conn.send(chunk)
            if remain_size != 0:
                chunk = file_obj.read(remain_size)
                conn.send(chunk)
        except Exception:
            LOG.error('Failed importing VDI from VHD stream - vdi_ref:%s',
                      vdi_ref)
            raise exception.VdiImportFailure(vdi_ref=vdi_ref)
        finally:
            resp = conn.getresponse()
            LOG.debug("Connection response status/reason is "
                      "%(status)s:%(reason)s",
                      {'status': resp.status, 'reason': resp.reason})
            conn.close()
chunked_request.py 文件源码 项目:lddmm-ot 作者: jeanfeydy 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def _connect(self):
        ''' Initialize an HTTP/HTTPS connection with chunked Transfer-Encoding
        to server:port with optional headers.
        '''
        server = self._server
        port = self._port
        headers = self._headers
        ssl_enabled = self._ssl_enabled
        proxy_server, proxy_port = self._get_proxy_config()

        if (proxy_server and proxy_port):
            if ssl_enabled:
                context = self._get_ssl_context()
                self._conn = http_client.HTTPSConnection(
                    proxy_server, proxy_port, context=context
                )
            else:
                self._conn = http_client.HTTPConnection(
                    proxy_server, proxy_port
                )
            self._conn.set_tunnel(server, port)
        else:
            if ssl_enabled:
                context = self._get_ssl_context()
                self._conn = http_client.HTTPSConnection(
                    server, port, context=context
                )
            else:
                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)


问题


面经


文章

微信
公众号

扫码关注公众号