PDA

View Full Version : Why Python Rocks


file13
05-28-2002, 04:39 PM
since it's the Python forum, thought i'd share this one with you. the other day i had a problem with a majorly poplocked server (50000+ messages) and since deleting them manually with telnet would take years and the admin was too annoying to harass, it's Python to the rescue!

http://www.qlippoth.com/pop_unlocker.py


#!/usr/bin/env python
#
# pop_unlocker.py
# file13@qlippoth.zzn.com
#
# handy dandy pop3 mass deleter.
# perfect for removing poplocks.
#
##################################

import poplib, getpass, socket, sys

def connect(pop_object):
try:
pop_object.getwelcome()
pop_object.user(raw_input("Username: "))
pop_object.pass_(getpass.getpass())
print "\n* Logged in sucessfully *\n"
except poplib.error_proto:
print "Error connecting, check user + pass...."
quit(pop_object)

def messageNumber(pop_object):
try:
print "Getting number of messages..."
numMessages = len(pop_object.list()[1])
print "Messages: ", numMessages
except poplib.error_proto:
print "Error listing messages"

def deleteMessages(pop_object):
answer = raw_input("Do you want to delete messages? (y/N)")
if answer == "y":
while 1:
try:
answer = input("How many to delete? (Messages 1 - ?) ")
break
except ValueError:
print "Invalid number!"
except NameError:
print "Invalid number!"
except SyntaxError:
print "Invalid number!"
counter = 1
try:
while counter <= answer:
print "Deleting ", counter
pop_object.dele(counter)
counter = counter + 1
else:
print "Quitting"
except poplib.error_proto:
print "Error deleting messages. Quitting"

def quit(pop_object):
try:
raw_input("Done, press enter to quit")
pop_object.quit()
sys.exit(0)

except poplib.error_proto:
print "Error quitting pop server"

if __name__ == "__main__":
try:
x = poplib.POP3(raw_input("Name of pop server: "))
connect(x)
messageNumber(x)
deleteMessages(x)
quit(x)
except poplib.error_proto:
print "Unable to connect to host. Quitting"
except socket.gaierror:
print "Unable to connect to host. Quitting"
except socket.error:
print "Unable to connect to host. Quitting"


the original of course wasen't so reboust, but a after a little effort ta da! that's the beauty of Python. perfect for quick hacks and large programs.

if only Lisp would get libs like Python....sigh.... :(

Strike
05-28-2002, 04:43 PM
It'd be uber cool if you made a PopObject class and used that :)

OO is good...

file13
05-28-2002, 04:48 PM
originally i did. but it was too annoying to pass around since "poplib.POP3" shoots back an object too. made my brain hurt. :(

functions seemed simpiler and more straight forward.... :)

inkedmn
05-29-2002, 12:19 PM
Originally posted by Strike

OO is good...

oh, indeed it is.

~praise OO (now that i'm able to use it ;))