本文整理汇总了Python中nntplib.NNTP.login方法的典型用法代码示例。如果您正苦于以下问题:Python NNTP.login方法的具体用法?Python NNTP.login怎么用?Python NNTP.login使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nntplib.NNTP
的用法示例。
在下文中一共展示了NNTP.login方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Client
# 需要导入模块: from nntplib import NNTP [as 别名]
# 或者: from nntplib.NNTP import login [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
#.........这里部分代码省略.........