当前位置: 首页>>代码示例>>Python>>正文


Python HTTPResponse.length方法代码示例

本文整理汇总了Python中httplib.HTTPResponse.length方法的典型用法代码示例。如果您正苦于以下问题:Python HTTPResponse.length方法的具体用法?Python HTTPResponse.length怎么用?Python HTTPResponse.length使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在httplib.HTTPResponse的用法示例。


在下文中一共展示了HTTPResponse.length方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _do_handshake

# 需要导入模块: from httplib import HTTPResponse [as 别名]
# 或者: from httplib.HTTPResponse import length [as 别名]
    def _do_handshake(self, url, version=0, origin=None, cookie=None):
        """http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-00"""
        fields = [
                "Upgrade: WebSocket",
                "Connection: Upgrade",
                "Host: %s" % (self.host,),
                "Sec-WebSocket-Draft: 2",
            ]

        if origin is None:
            origin = self.host
        fields.append("Origin: %s" % (origin,))

        if cookie is not None:
            fields.append(cookie.output(header="Cookie:"))

        # Authenticate if required
        if self.username is not None:
            fields.append("Authorization: Basic %s"
                    % b64encode('%s:%s' % (self.username, self.password or '')))

        # What idiot invented this algorithm for key construction?
        spaces_1  = random.randint(1, 12)
        spaces_2  = random.randint(1, 12)
        max_1     = min(2**32-1 / spaces_1, 2**32-1)
        max_2     = min(2**32-1 / spaces_2, 2**32-1)
        number_1  = random.randint(0, max_1)
        number_2  = random.randint(0, max_2)
        product_1 = number_1 * spaces_1
        product_2 = number_2 * spaces_2
        key_1     = list(str(product_1))
        key_2     = list(str(product_2))

        ran_range = ''.join(map(chr, range(0x21, 0x30) + range(0x3a, 0x7f)))
        for n in xrange(random.randint(1, 12)):
            key_1.insert(random.randint(0, len(key_1)), random.choice(ran_range))
        for n in xrange(random.randint(1, 12)):
            key_2.insert(random.randint(0, len(key_1)), random.choice(ran_range))

        for n in xrange(spaces_1):
            key_1.insert(random.randint(1, len(key_1) - 1), ' ')
        for n in xrange(spaces_2):
            key_2.insert(random.randint(1, len(key_1) - 1), ' ')

        key_1 = ''.join(key_1)
        key_2 = ''.join(key_2)

        fields += [
                "Sec-WebSocket-Key1: %s" % (key_1,),
                "Sec-WebSocket-Key2: %s" % (key_2,),
            ]

        key_3 = urandom(8)
        fields.append("Content-Length: %d" % (len(key_3),))

        random.shuffle(fields)

        urlParts = urlparse.urlparse(url)

        # Finish request
        request = "GET %s HTTP/1.1\r\n" \
                  "%s\r\n" \
                  "\r\n" \
                  "%s" \
                % (urlParts.path, '\r\n'.join(fields), key_3)
        if _debug:
            print >> sys.stderr, '\x1B[D\x1B[31m%s\x1B[m' % (request,),
        self.socket.sendall(request)

        response = HTTPResponse(self.socket)
        response.begin()

        if _debug:
            print >> sys.stderr, '\x1B[D\x1B[34m%s' % ({9: 'HTTP/0.9', 10: 'HTTP/1.0', 11: 'HTTP/1.1'}[response.version],), response.status, response.reason
            print >> sys.stderr, '%s\x1B[m' % (response.msg,)

        if response.status != 101:
            self.socket.close()
            raise RuntimeError("WebSocket upgrade failed: %d %s" % (response.status, response.reason))

        # Hack to override httplib's setting it to 0 for 101's
        response.length = None

        expected_headers = ('upgrade', 'connection', 'sec-websocket-origin', 'sec-websocket-location')
        for header in expected_headers:
            if header not in response.msg:
                raise RuntimeError, "Expected WebSocket header not present: %s" % (header,)
        if response.msg['upgrade'].lower().strip() != 'websocket':
            raise RuntimeError, "Upgraded to wrong protocol, WebSocket expected: %s" % (response.msg['upgrade'],)
        if response.msg['connection'].lower().strip() != 'upgrade':
            raise RuntimeError, "Bad Connection header: %s" % (response.msg['connection'],)
        if response.msg['sec-websocket-origin'].lower().strip() != origin:
            raise RuntimeError, "Wrong WebSocket origin: %s" % (response.msg['sec-websocket-origin'],)

        if response.msg['sec-websocket-location'].lower().strip() != urlparse.urlunparse((self.scheme, self.host, urlParts.path, urlParts.params, urlParts.query, urlParts.fragment)):
            raise RuntimeError, "Bad WebSocket location: %s" % (response.msg['sec-websocket-location'],)

        assert 0 <= number_1 <= 4294967295
        assert 0 <= number_2 <= 4294967295
        challenge = struct.pack('>LL8s', number_1, number_2, key_3)
#.........这里部分代码省略.........
开发者ID:bitkapp,项目名称:traidor,代码行数:103,代码来源:__init__.py


注:本文中的httplib.HTTPResponse.length方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。