Skip to content

Commit

Permalink
Update examples to reflect recent changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Le Manchet committed Aug 31, 2017
1 parent 6330970 commit 826ab8e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 61 deletions.
10 changes: 4 additions & 6 deletions examples/example.py
Expand Up @@ -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()
56 changes: 27 additions & 29 deletions examples/idle_example.py
@@ -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()
15 changes: 4 additions & 11 deletions examples/oauth2_example.py
@@ -1,4 +1,4 @@
from __future__ import unicode_literals
# Login using OAUTH2

from imapclient import IMAPClient

Expand All @@ -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...
16 changes: 10 additions & 6 deletions examples/tls_cacert.py
@@ -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...
22 changes: 13 additions & 9 deletions examples/tls_no_checks.py
@@ -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.