__init__.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:lantern-detection 作者: gongxijun 项目源码 文件源码
def _ssl_wrap_method(method, is_reader=False):
    """Wrap the given method with SSL error-trapping.

    is_reader: if False (the default), EOF errors will be raised.
        If True, EOF errors will return "" (to emulate normal sockets).
    """
    def ssl_method_wrapper(self, *args, **kwargs):
##        print (id(self), method, args, kwargs)
        start = time.time()
        while True:
            try:
                return method(self, *args, **kwargs)
            except (SSL.WantReadError, SSL.WantWriteError):
                # Sleep and try again. This is dangerous, because it means
                # the rest of the stack has no way of differentiating
                # between a "new handshake" error and "client dropped".
                # Note this isn't an endless loop: there's a timeout below.
                time.sleep(self.ssl_retry)
            except SSL.SysCallError, e:
                if is_reader and e.args == (-1, 'Unexpected EOF'):
                    return ""

                errno = e.args[0]
                if is_reader and errno in socket_errors_to_ignore:
                    return ""
                raise socket.error(errno)
            except SSL.Error, e:
                if is_reader and e.args == (-1, 'Unexpected EOF'):
                    return ""

                thirdarg = None
                try:
                    thirdarg = e.args[0][0][2]
                except IndexError:
                    pass

                if is_reader and thirdarg == 'ssl handshake failure':
                    return ""
                if thirdarg == 'http request':
                    # The client is talking HTTP to an HTTPS server.
                    raise NoSSLError()
                raise
            if time.time() - start > self.ssl_timeout:
                raise socket.timeout("timed out")
    return ssl_method_wrapper
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号