Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/python
- import socket
- import sys
- import time
- def readline(fd):
- line = fd.readline()
- if line == None:
- raise EOFError()
- #sys.stdout.write(line)
- return line.rstrip() #remove trailing CRLF
- def checkOK(fd):
- line = readline(fd)
- if line[0] == '-':
- raise RuntimeError("server errored")
- whitespace = " \t"
- pop3_host = "pop3.shellmix.com"
- pop3_port = 110
- pop3_user = ""
- pop3_pass = ""
- sock_fd = None
- fd = None
- try:
- sock_fd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock_fd.connect((pop3_host, pop3_port))
- fd = sock_fd.makefile()
- print("connected")
- checkOK(fd)
- sock_fd.send("USER " + pop3_user + "\r\n")
- checkOK(fd)
- sock_fd.send("PASS " + pop3_pass + "\r\n")
- checkOK(fd)
- sock_fd.send("LIST\r\n")
- checkOK(fd)
- message_list = []
- while True:
- line = readline(fd)
- if len(line) == 1 and line[0] == ".":
- break
- message_list.append(line.split(" ")[0])
- finished = False
- for msg in message_list:
- sock_fd.send("RETR " + msg + "\r\n")
- checkOK(fd)
- email_finished = False
- #loop for email headers
- while True:
- line = readline(fd)
- if len(line) == 1 and line[0] == ".":
- email_finished = True
- break
- if len(line) == 0: #end of headers
- break
- if whitespace.find(line[0]) != -1: #the first character is whitespace
- print("-> " + line.strip())
- else:
- keyval = line.split(":")
- keyval[0] = keyval[0].strip()
- keyval[1] = keyval[1].strip()
- print("key=\"" + keyval[0] + "\" val=\"" + keyval[1] + "\"")
- #loop for email body
- while not email_finished:
- line = readline(fd)
- if len(line) == 1 and line[0] == ".":
- break
- sock_fd.send("QUIT\r\n")
- checkOK(fd)
- finally:
- time.sleep(1)
- print("cleaning up")
- if fd != None:
- fd.close()
- fd = None
- if sock_fd != None:
- sock_fd.shutdown(socket.SHUT_RDWR)
- sock_fd.close()
- sock_fd = None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement