-
-
Save fossilet/3742679 to your computer and use it in GitHub Desktop.
Check the quota and usage of the IMAP server.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- encoding: utf-8 -*- | |
'''Get quota from IMAP server | |
''' | |
import argparse | |
import getpass | |
import imaplib | |
import re | |
def getArgs(): | |
"""show argpase snippets | |
""" | |
parser = argparse.ArgumentParser() | |
parser.add_argument('-u', '--username', required=True) | |
parser.add_argument('-i', '--imap', nargs='?', default='imap.gmail.com') | |
parser.add_argument('-p', '--password', nargs='?') | |
parser.add_argument('-P', '--port', nargs='?', type=int, default=993) | |
return parser.parse_args() | |
if __name__ == '__main__': | |
args = getArgs() | |
# Email suffix is usually the server domain name excluding the first part. | |
# E.g., @gmail.com for imap.gmail.com | |
email_suffix = '.'.join(args.imap.split('.')[1:]) | |
email_addr = '%s@%s' % (args.username, email_suffix) | |
password = args.password or getpass.getpass() | |
M = imaplib.IMAP4_SSL(args.imap, args.port) | |
M.login(email_addr, password) | |
quotaStr = M.getquotaroot("INBOX")[1][1][0] | |
p = re.compile('\d+') | |
r = p.findall(quotaStr) or [0, 0] | |
print "Quota for %s:" % email_addr | |
print 'Allotted: %.2f MB' % (float(r[1]) / 1024) | |
print 'Used: %.2f MB' % (float(r[0]) / 1024) | |
M.logout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment