本文整理汇总了Python中imaplib.IMAP4属性的典型用法代码示例。如果您正苦于以下问题:Python imaplib.IMAP4属性的具体用法?Python imaplib.IMAP4怎么用?Python imaplib.IMAP4使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类imaplib
的用法示例。
在下文中一共展示了imaplib.IMAP4属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: fetch_email
# 需要导入模块: import imaplib [as 别名]
# 或者: from imaplib import IMAP4 [as 别名]
def fetch_email(receiver):
global IMAP_SERVER
global IMAP_SERVER_PORT
server = imaplib.IMAP4(IMAP_SERVER, IMAP_SERVER_PORT)
server.login(receiver["address"],receiver["password"])
server.select("INBOX")
typ, data = server.search(None, "ALL")
index = data[0].split()
typ, data = server.fetch(index[-1], '(RFC822)')
try:
message = email.message_from_string(data[0][1].decode("utf-8"))
except UnicodeEncodeError:
logging.warning("failed to decode the email because of UnicodeEncodeError")
server.close()
server.logout()
return message
示例2: test_aborted_authentication
# 需要导入模块: import imaplib [as 别名]
# 或者: from imaplib import IMAP4 [as 别名]
def test_aborted_authentication(self):
class MyServer(SimpleIMAPHandler):
def cmd_AUTHENTICATE(self, tag, args):
self._send_textline('+')
self.response = yield
if self.response == b'*\r\n':
self._send_tagged(tag, 'NO', '[AUTHENTICATIONFAILED] aborted')
else:
self._send_tagged(tag, 'OK', 'MYAUTH successful')
with self.reaped_pair(MyServer) as (server, client):
with self.assertRaises(imaplib.IMAP4.error):
code, data = client.authenticate('MYAUTH', lambda x: None)
示例3: test_aborted_authentication
# 需要导入模块: import imaplib [as 别名]
# 或者: from imaplib import IMAP4 [as 别名]
def test_aborted_authentication(self):
class MyServer(SimpleIMAPHandler):
def cmd_AUTHENTICATE(self, tag, args):
self._send_textline('+')
self.response = yield
if self.response == b'*\r\n':
self._send_tagged(
tag,
'NO',
'[AUTHENTICATIONFAILED] aborted')
else:
self._send_tagged(tag, 'OK', 'MYAUTH successful')
client, _ = self._setup(MyServer)
with self.assertRaisesRegex(imaplib.IMAP4.error,
r'\[AUTHENTICATIONFAILED\] aborted'):
client.authenticate('MYAUTH', lambda x: None)
示例4: get_imap_server
# 需要导入模块: import imaplib [as 别名]
# 或者: from imaplib import IMAP4 [as 别名]
def get_imap_server(cls):
import imaplib
conf = cls.conf()
if not conf.get('imap_host'):
raise ValueError("No IMAP server defined - check your configuration")
if conf.get('imap_ssl'):
server = imaplib.IMAP4_SSL(conf['imap_host'])
else:
server = imaplib.IMAP4(conf['imap_host'])
server.login(conf['imap_user'], conf['imap_password'])
server.select()
return server
示例5: __init__
# 需要导入模块: import imaplib [as 别名]
# 或者: from imaplib import IMAP4 [as 别名]
def __init__(self, target):
# Target comes as protocol://target:port
self.target = target
proto, host, port = target.split(':')
host = host[2:]
if int(port) == 993 or proto.upper() == 'IMAPS':
self.session = imaplib.IMAP4_SSL(host,int(port))
else:
#assume non-ssl IMAP
self.session = imaplib.IMAP4(host,port)
if 'AUTH=NTLM' not in self.session.capabilities:
logging.error('IMAP server does not support NTLM authentication!')
return False
self.authtag = self.session._new_tag()
self.lastresult = None
示例6: test_issue5949
# 需要导入模块: import imaplib [as 别名]
# 或者: from imaplib import IMAP4 [as 别名]
def test_issue5949(self):
class EOFHandler(SocketServer.StreamRequestHandler):
def handle(self):
# EOF without sending a complete welcome message.
self.wfile.write('* OK')
with self.reaped_server(EOFHandler) as server:
self.assertRaises(imaplib.IMAP4.abort,
self.imap_class, *server.server_address)
示例7: test_linetoolong
# 需要导入模块: import imaplib [as 别名]
# 或者: from imaplib import IMAP4 [as 别名]
def test_linetoolong(self):
maxline = 10
class TooLongHandler(SimpleIMAPHandler):
def handle(self):
# Send a very long response line
self.wfile.write('* OK ' + maxline * 'x' + '\r\n')
with self.reaped_server(TooLongHandler) as server, \
support.swap_attr(imaplib, '_MAXLINE', maxline):
with self.assertRaisesRegexp(imaplib.IMAP4.error,
'got more than 10 bytes'):
self.imap_class(*server.server_address)
示例8: test_LOGIN
# 需要导入模块: import imaplib [as 别名]
# 或者: from imaplib import IMAP4 [as 别名]
def test_LOGIN(self):
"""Testing different login combinations using simple login auth mechanism."""
def imap_login():
login_sequences = [('kajoj_admin', 'thebestpassword'),
('\"kajoj_admin\"', 'the best password')]
imap_obj = imaplib.IMAP4('127.0.0.1', port=8888)
for sequence in login_sequences:
with self.assertRaises(imaplib.IMAP4.error) as error:
imap_obj.login(sequence[0], sequence[1])
imap_exception = error.exception
self.assertEqual(imap_exception.args[0], b'Authentication failed')
imap_obj.logout()
options = {
'enabled': 'True',
'port': 143,
'timeout': 30,
'protocol_specific_data': {
'max_attempts': 3,
'banner': '* OK IMAP4rev1 Server Ready'
}
}
capability = Imap(options, self.loop)
server_coro = asyncio.start_server(
capability.handle_session, '0.0.0.0', 8888, loop=self.loop)
self.server = self.loop.run_until_complete(server_coro)
imap_task = self.loop.run_in_executor(None, imap_login)
self.loop.run_until_complete(imap_task)
示例9: test_linetoolong
# 需要导入模块: import imaplib [as 别名]
# 或者: from imaplib import IMAP4 [as 别名]
def test_linetoolong(self):
class TooLongHandler(SimpleIMAPHandler):
def handle(self):
# Send a very long response line
self.wfile.write('* OK ' + imaplib._MAXLINE*'x' + '\r\n')
with self.reaped_server(TooLongHandler) as server:
self.assertRaises(imaplib.IMAP4.error,
self.imap_class, *server.server_address)
示例10: __init__
# 需要导入模块: import imaplib [as 别名]
# 或者: from imaplib import IMAP4 [as 别名]
def __init__(self, host, port, timeout=None):
self._timeout = timeout
imaplib.IMAP4.__init__(self, host, port)
示例11: run_mailbox
# 需要导入模块: import imaplib [as 别名]
# 或者: from imaplib import IMAP4 [as 别名]
def run_mailbox(self, min_delay=5.0, max_delay=60.0):
mailbox = None
try:
while True:
item = yield idiokit.next()
while True:
delay = min(min_delay, max_delay)
while mailbox is None:
try:
mailbox = yield idiokit.thread(self.connect)
except (imaplib.IMAP4.abort, socket.error) as error:
self.log.error("Failed IMAP connection ({0})".format(utils.format_exception(error)))
else:
break
self.log.info("Retrying connection in {0:.2f} seconds".format(delay))
yield idiokit.sleep(delay)
delay = min(2 * delay, max_delay)
event, name, args, keys = item
if event.result().unsafe_is_set():
break
try:
method = getattr(mailbox, name)
result = yield idiokit.thread(method, *args, **keys)
except (imaplib.IMAP4.abort, socket.error) as error:
yield idiokit.thread(self.disconnect, mailbox)
self.log.error("Lost IMAP connection ({0})".format(utils.format_exception(error)))
mailbox = None
except imaplib.IMAP4.error as error:
event.fail(type(error), error, None)
break
else:
event.succeed(result)
break
finally:
if mailbox is not None:
yield idiokit.thread(self.disconnect, mailbox)
示例12: connect
# 需要导入模块: import imaplib [as 别名]
# 或者: from imaplib import IMAP4 [as 别名]
def connect(self):
self.log.info("Connecting to IMAP server {0!r} port {1}".format(
self.mail_server, self.mail_port))
if self.mail_disable_ssl:
mailbox = _IMAP4(
self.mail_server,
self.mail_port,
timeout=self.mail_connection_timeout
)
else:
mailbox = _IMAP4_SSL(
self.mail_server,
self.mail_port,
timeout=self.mail_connection_timeout,
ca_certs=self.mail_ca_certs
)
self.log.info("Logging in to IMAP server {0!r} port {1}".format(
self.mail_server, self.mail_port))
mailbox.login(self.mail_user, self.mail_password)
try:
status, msgs = mailbox.select(self.mail_box, readonly=False)
if status != "OK":
for msg in msgs:
raise imaplib.IMAP4.abort(msg)
except:
mailbox.logout()
raise
self.log.info("Logged in to IMAP server {0!r} port {1}".format(
self.mail_server, self.mail_port))
return mailbox
示例13: disconnect
# 需要导入模块: import imaplib [as 别名]
# 或者: from imaplib import IMAP4 [as 别名]
def disconnect(self, mailbox):
try:
mailbox.close()
except (imaplib.IMAP4.error, socket.error):
pass
try:
mailbox.logout()
except (imaplib.IMAP4.error, socket.error):
pass
示例14: get_inbox
# 需要导入模块: import imaplib [as 别名]
# 或者: from imaplib import IMAP4 [as 别名]
def get_inbox(self):
mailbox = imaplib.IMAP4(self.host)
mailbox.login(
bytes(self.username, "utf8"), bytes(self.password, "utf8")
)
mailbox.select()
x, data = mailbox.search(None, "ALL")
messages = []
for num in data[0].split():
x, message = mailbox.fetch(num, "(RFC822)")
messages.append(message[0][1])
return messages
开发者ID:PacktPublishing,项目名称:Python-3-Object-Oriented-Programming-Third-Edition,代码行数:14,代码来源:email_facade.py
示例15: main
# 需要导入模块: import imaplib [as 别名]
# 或者: from imaplib import IMAP4 [as 别名]
def main():
source = {"name": "longz", "address": "source@foo.bar", "password": "xxxxxx"}
imap_server = "xxx.xxx.xxx.xxx"
imap_server_port = 143
folder_name = "dataset"
# create a folder to save all the emails
if not os.path.exists(folder_name): os.system("mkdir '%s'"%(folder_name))
server = imaplib.IMAP4(imap_server, imap_server_port)
server.login(source["address"],source["password"])
server.select("INBOX")
typ, data = server.search(None, "ALL")
index = data[0].split()
for i in index:
logging.info("fetching email with index %s"%i)
typ, data = server.fetch(i, '(RFC822)')
try:
message = email.message_from_string(data[0][1].decode("utf-8"))
filepath = "./%s/%s.msg"%(folder_name, i)
save_to_file(filepath, message)
logging.info("email saved to %s"%filepath)
except UnicodeEncodeError:
logging.warning("skip to save this email because of UnicodeEncodeError")
server.close()
server.logout()