Permalink
Browse files

Update examples to reflect recent changes

  • Loading branch information...
NicolasLM committed Aug 31, 2017
1 parent 6330970 commit 826ab8e134293ab7eae81bfe6e4ccc305595c43b
Showing with 58 additions and 61 deletions.
  1. +4 −6 examples/example.py
  2. +27 −29 examples/idle_example.py
  3. +4 −11 examples/oauth2_example.py
  4. +10 −6 examples/tls_cacert.py
  5. +13 −9 examples/tls_no_checks.py
View
@@ -2,28 +2,26 @@
# Released subject to the New BSD License
# Please see http://en.wikipedia.org/wiki/BSD_licenses
from __future__ import unicode_literals
from imapclient import IMAPClient
HOST = 'imap.host.com'
USERNAME = 'someuser'
PASSWORD = 'secret'
ssl = False
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server = IMAPClient(HOST)
server.login(USERNAME, PASSWORD)
select_info = server.select_folder('INBOX')
print('%d messages in INBOX' % select_info[b'EXISTS'])
messages = server.search(['NOT', 'DELETED'])
print("%d messages that aren't deleted" % len(messages))
print("%d messages that aren't deleted\n" % len(messages))
print()
print("Messages:")
response = server.fetch(messages, ['FLAGS', 'RFC822.SIZE'])
for msgid, data in response.items():
print(' ID %d: %d bytes, flags=%s' % (msgid,
data[b'RFC822.SIZE'],
data[b'FLAGS']))
server.logout()
View
@@ -1,29 +1,27 @@
# This example is a lot more interesting if you have an active client
# connected to the same IMAP account!
from __future__ import unicode_literals
from imapclient import IMAPClient
HOST = 'imap.host.com'
USERNAME = 'someuser'
PASSWORD = 'password'
ssl = True
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(USERNAME, PASSWORD)
server.select_folder('INBOX')
# Start IDLE mode
server.idle()
# Wait for up to 30 seconds for an IDLE response
responses = server.idle_check(timeout=30)
print(responses)
# Come out of IDLE mode
text, responses = server.idle_done()
print('IDLE done. Server said %r' % text)
print('Final responses: ', responses)
print(server.logout())
# Open a connection in IDLE mode and wait for notifications from the server
from imapclient import IMAPClient
HOST = 'imap.host.com'
USERNAME = 'someuser'
PASSWORD = 'password'
server = IMAPClient(HOST)
server.login(USERNAME, PASSWORD)
server.select_folder('INBOX')
# Start IDLE mode
server.idle()
print("Connection is now in IDLE mode, send yourself an email or quit with ^c")
while True:
try:
# Wait for up to 30 seconds for an IDLE response
responses = server.idle_check(timeout=30)
print("Server sent:", responses if responses else "nothing")
except KeyboardInterrupt:
break
server.idle_done()
print("\nIDLE mode done")
server.logout()
View
@@ -1,4 +1,4 @@
from __future__ import unicode_literals
# Login using OAUTH2
from imapclient import IMAPClient
@@ -8,14 +8,7 @@
HOST = 'imap.host.com'
URL = "https://somedomain.com/someuser/imap/"
ssl = True
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
resp = server.oauth2_login(URL, OAUTH2_USER, OAUTH2_ACCESS_TOKEN)
print(resp)
select_info = server.select_folder('INBOX')
print(select_info)
server.logout()
with IMAPClient(HOST) as server:
server.oauth2_login(URL, OAUTH2_USER, OAUTH2_ACCESS_TOKEN)
# ...do something...
View
@@ -1,16 +1,20 @@
# Copyright (c) 2015, Menno Smits
# Released subject to the New BSD License
# Please see http://en.wikipedia.org/wiki/BSD_licenses
#
# Establish a secure connection to a server that does not have a certificate
# signed by a trusted authority.
from __future__ import unicode_literals
import imapclient
import ssl
from imapclient import IMAPClient
HOST = 'imap.host.com'
USERNAME = 'someuser'
PASSWORD = 'secret'
context = imapclient.create_default_context(cafile="/path/to/cacert.pem")
ssl_context = ssl.create_default_context(cafile="/path/to/cacert.pem")
server = imapclient.IMAPClient(HOST, use_uid=True, ssl=True, ssl_context=context)
server.login(USERNAME, PASSWORD)
# ...
with IMAPClient(HOST, ssl_context=ssl_context) as server:
server.login(USERNAME, PASSWORD)
# ...do something...
View
@@ -1,23 +1,27 @@
# Copyright (c) 2015, Menno Smits
# Released subject to the New BSD License
# Please see http://en.wikipedia.org/wiki/BSD_licenses
#
# Establish an encrypted connection to a server without checking its
# certificate. This setup is insecure, DO NOT USE to connect to servers
# over the Internet.
from __future__ import unicode_literals
import imapclient
from backports import ssl
import ssl
from imapclient import IMAPClient
HOST = 'imap.host.com'
USERNAME = 'someuser'
PASSWORD = 'secret'
context = imapclient.create_default_context()
ssl_context = ssl.create_default_context()
# don't check if certificate hostname doesn't match target hostname
context.check_hostname = False
ssl_context.check_hostname = False
# don't check if the certificate is trusted by a certificate authority
context.verify_mode = ssl.CERT_NONE
ssl_context.verify_mode = ssl.CERT_NONE
server = imapclient.IMAPClient(HOST, use_uid=True, ssl=True, ssl_context=context)
server.login(USERNAME, PASSWORD)
# ...
with IMAPClient(HOST, ssl_context=ssl_context) as server:
server.login(USERNAME, PASSWORD)
# ...do something...

0 comments on commit 826ab8e

Please sign in to comment.