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


Python NNTP.set_debuglevel方法代码示例

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


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

示例1: Client

# 需要导入模块: from nntplib import NNTP [as 别名]
# 或者: from nntplib.NNTP import set_debuglevel [as 别名]
class Client(Context):
    def __init__(self, log,
    hostname, port=None, username=None, password=None, *,
    debuglevel=None, **timeout):
        self.log = log
        self.hostname = hostname
        self.port = port
        self.username = username
        self.password = password
        self.debuglevel = debuglevel
        self.timeout = timeout
        Context.__init__(self)
        self.connect()
    
    def connect(self):
        address = net.format_addr((self.hostname, self.port))
        self.log.write("Connecting to {}\n".format(address))
        if self.port is None:
            port = ()
        else:
            port = (self.port,)
        self.connect_time = time.monotonic()
        self.nntp = NNTP(self.hostname, *port, **self.timeout)
        with ExitStack() as cleanup:
            cleanup.push(self)
            if self.debuglevel is not None:
                self.nntp.set_debuglevel(self.debuglevel)
            self.log.write("{}\n".format(self.nntp.getwelcome()))
            
            if self.username is not None:
                self.log.write("Logging in as {}\n".format(self.username))
                with self.handle_abort():
                    self.nntp.login(self.username, self.password)
                self.log.write("Logged in\n")
            cleanup.pop_all()
    
    def body(self, id, *pos, **kw):
        id = "<{}>".format(id)
        retry = 0
        while True:
            try:
                with self.handle_abort():
                    self.nntp.body(id, *pos, **kw)
                break
            except failure_responses as err:
                [code, *msg] = err.response.split(maxsplit=1)
                if code == "400":
                    [msg] = msg or (None,)
                    if not msg:
                        msg = "Server shut down connection"
                elif code[1] == "0" and not retry:
                    msg = err.response
                else:
                    raise
            self.log.write(msg + "\n")
            self.log_time()
            if retry >= 60:
                raise TimeoutError()
            self.close()
            time.sleep(retry)
            if not retry:
                start = time.monotonic()
            self.connect()
            if retry:
                retry *= 2
            else:
                retry = time.monotonic() - start
                if retry <= 0:
                    retry = 0.5
    
    def group(self, *pos, **kw):
        with self.handle_abort():
            return self.nntp.group(*pos, **kw)
    def over(self, *pos, **kw):
        with self.handle_abort():
            return self.nntp.over(*pos, **kw)
    def hdr(self, *pos, **kw):
        with self.handle_abort():
            return self.nntp.xhdr(*pos, **kw)
    
    @contextmanager
    def handle_abort(self):
        try:
            yield
        except failure_responses:
            raise  # NNTP connection still intact
        except:
            # Protocol is disrupted so abort the connection straight away
            self.close()
            raise
    
    def close(self):
        if not self.nntp:
            return
        
        # Ignore failure of inappropriate QUIT command
        with suppress(NNTPError), self.nntp:
            pass
        self.nntp = None
    
#.........这里部分代码省略.........
开发者ID:vadmium,项目名称:usenet-downloader,代码行数:103,代码来源:nntp.py


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