Python3 IMap4 socket sample
IMAP4rev1 sample for python3.2
USER_NAME = 'your name'
USER_PASSWORD = 'password'
#!/usr/bin/env python3
# IMAP4rev1
# samples in python3.
# select.select
# LOGIN, LOGOUT, CREATE, LIST, LSUB
# 11/22/2011 macken
import getpass
import socket
import select
import sys
import fcntl
import os
HOST = 'your imap4 host'
USER_NAME = 'your name'
USER_PASSWORD = 'password'
PORT = 143
BUFF_LEN = 1024
QUOTE = '"'
BLANK = ' '
QUOTE_STAR_QUOTE = QUOTE+'*'+QUOTE
QUOTE_QUOTE = QUOTE+QUOTE
CRLF = '\r\n'
current_index = 0
def next_index():
# For to use the GLOBAL version of current_index.
global current_index
index = current_index
current_index+=1
return repr(index) + BLANK
def create_folder(s, folder_name):
str0 = next_index() + 'CREATE ' + QUOTE + folder_name + QUOTE + CRLF;
data_send = bytearray(str0.encode());
print('data_send = ' + data_send.decode(), end='')
s.send(data_send)
line = s.recv(BUFF_LEN)
print("ListFolders Recieve:", repr(line))
# execute LSUB
def list_folders(s, chkShowAllFolders):
FolderList = []
if chkShowAllFolders:
str0 = next_index() + 'LSUB ' + QUOTE_QUOTE + BLANK + QUOTE_STAR_QUOTE + CRLF;
data_send = bytearray(str0.encode());
print('data_send = ' + data_send.decode(), end='')
intStatus = s.send(data_send)
FolderList.append('INBOX')
else:
str0 = next_index() + 'LIST ' + QUOTE_QUOTE + BLANK + QUOTE_STAR_QUOTE + CRLF;
data_send = bytearray(str0.encode());
print('data_send = ' + data_send.decode(), end='')
intStatus = s.send(data_send)
line = s.recv(BUFF_LEN)
print("ListFolders Recieve:", repr(line))
# Simply change the host and port values
def login():
current_index = 0
host = HOST
port = PORT
s = None
for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except socket.error as msg:
s = None
continue
try:
s.connect(sa)
# fl = fcntl.fcntl(s, fcntl.F_GETFL)
# fcntl.fcntl(s, fcntl.F_SETFL, fl | os.O_NONBLOCK)
print("Success connecting to ", end='')
print(host, "on port:", repr(port))
except socket.error as msg:
s.close()
s = None
print("Cannot connect to ", end='')
print(host, "on port: ", repr(port))
print(msg)
continue
break
if s is None:
print('could not open socket')
sys.exit(1)
line = s.recv(BUFF_LEN)
print("Recieve:", repr(line))
# password = getpass.getpass()
password = USER_PASSWORD
data_send = bytearray(next_index() + 'LOGIN ' + QUOTE + USER_NAME +
QUOTE + ' ' + QUOTE + password + QUOTE + CRLF, 'utf-8')
print('data_send = ' + data_send.decode(), end='')
s.send(data_send)
inputsentence = []
while True:
# Limit 5sec
rlist, wlist, elist = select.select([s], [], [], 5.0)
if [rlist, wlist, elist] == [[], [], []]:
print("Over time.");
sys.exit(1)
if s in rlist:
# read line from input and strip off newline
line = s.recv(1024)
inputsentence.append(line)
print("Recieve:", repr(inputsentence))
break
return s
def logout(s):
str0 = next_index() + 'LOGOUT' + CRLF;
data_send = bytearray(str0.encode());
print('data_send = ' + data_send.decode(), end='')
s.send(data_send)
line = s.recv(BUFF_LEN)
print("logout Recieve:", repr(line))
def main():
s = login()
if s == None:
print("fail login", end='')
sys.exit(1)
create_folder(s, 'test_folder')
list_folders(s, True)
list_folders(s, False)
logout(s)
s.close()
if __name__ == "__main__":
main()
コメント
コメントを投稿