Client pop pour faibles connections

Soyez le premier à donner votre avis sur cette source.

Vue 6 854 fois - Téléchargée 360 fois

Description

ce code permet par voie graphique de se connecter a un serveur POP et de recuperer la liste
des 10 messages les plus volumineux (enfin , juste le sujet , qui vous l a envoyé et la taille),et en cliquant ,on peut avoir un extrait du corps du message.Mais surtout , l interet est de pouvoir effacer les gros messages du serveur pop par voie graphique .
c est fonctionnel mais tres tres loin d etre parfait.

Source / Exemple :


import socket
from  poplib import *
import gtk
import gtk.glade

#define constant to represent how many messages will be displayed in listbox
MESSAGE_NB=9
#define constant to define how many lines of mail are displayed when selecting one in list
LINES_NB=50
ui = gtk.glade.XML("pophelper.glade")

#define default connection icone
ui.get_widget("icone").set_from_file("notconnected.png")
#define slots

#connect button clicked:make the connections and get biggest messages
def on_connect(source=None, event=None):
 #first make a connection
 global conn
 global goodlist	
 
 #inform user we re trying to connect
 ui.get_widget("connectionstate").set_text("connecting to server...please wait") 
 server= ui.get_widget("server").get_text()
 user= ui.get_widget("user").get_text()
 password= ui.get_widget("password").get_text()
 try:
  conn=POP3(server)
 except socket.error:
  ui.get_widget("connectionstate").set_text("problem with connection")
  ##return 1 
 
 #if we are here ,we can connect 
 try:
  conn.user(user)
  conn.pass_(password)
 except: 
 #user/password is incorrect or malformated
  conn.quit()
  
 #after connection, get 10 messages with biggest size and construct listmessages treeview
 liste=conn.list()[1:2]
 tube=["liste des tailles des mails"]
 for i in range(len(liste[0])):
  tube.append(int(liste[0][i].split()[1]))
 #now we have list , we need to get MESSAGE_NB most voluminous
 #create the list where well store all that
 goodlist=[]
 for i in range(MESSAGE_NB):
  goodlist.append((maxetindice(tube)[0],maxetindice(tube)[1]))
  tube[maxetindice(tube)[0]]=0
 ##print goodlist
 #ui.get_widget("connectionstate").set_text("connected to:"+ui.get_widget("server").get_text())
 ui.get_widget("icone").set_from_file("connected.png")
 ui.get_widget("totalsize").set_text("espace total occupe:"+str(taille_msg(goodlist))+"ko")
 #help function that actually fills the listbox with the messages
 construire_arbre()
 ui.get_widget("connectionstate").set_text("connected to:"+ui.get_widget("server").get_text())

 #delete button cliked:delete selected message 
def on_delete_cliked(source=None, event=None):
 ui.get_widget("connectionstate").set_text("deleting selected message") 
 #get selected item from listmessages and erase it
 global goodlist
 ##print goodlist[ui.get_widget("listmessages").get_cursor()[0][0]][0]
 conn.dele(goodlist[ui.get_widget("listmessages").get_cursor()[0][0]][0])
  
 #we should also blank the textview,the line in the treeview,and maybe notify the
 #the action done on the bottom of the application
 #first calculate our goodlist again...
 liste=conn.list()[1:2]
 tube=["liste des tailles des mails"]
 for i in range(len(liste[0])):
  tube.append(int(liste[0][i].split()[1]))
 #now we have list , we need to get MESSAGE_NB most voluminous
 #create the list where well store all that
 goodlist=[]
 for i in range(MESSAGE_NB):
  goodlist.append((maxetindice(tube)[0],maxetindice(tube)[1]))
  tube[maxetindice(tube)[0]]=0
 ##print goodlist

 #quickest way to get tree logically filled again
 construire_arbre()
 #quickest way to blank the textview
 buffer=ui.get_widget("messagebody").get_buffer()
 buffer.set_text("")
 
 #calculate new total size
 ui.get_widget("totalsize").set_text("espace total occupe:"+str(taille_msg(goodlist))+"ko")
 #inform that we have deleted
 ui.get_widget("connectionstate").set_text("message deleted")

#quit button cliked:stop current connection(if any)
def on_quit(source=None, event=None):
 global conn
 try:
  conn.quit()
 except: 
  ui.get_widget("connectionstate").set_text("conn died before...sorry")
 #clear all info about last connection 
 ui.get_widget("connectionstate").set_text("not connected")
 ui.get_widget("server").set_text("")
 ui.get_widget("user").set_text("")
 ui.get_widget("password").set_text("")
 #ui.get_widget("messagesnb").set_text("")
 ui.get_widget("icone").set_from_file("notconnected.png")
 #call construire_arbre with blank parameter set to 1
 construire_arbre(1)
 #and clear text area
 buffer=ui.get_widget("messagebody").get_buffer()
 buffer.set_text("")
 #and clear total size label
 ui.get_widget("totalsize").set_text("espace total occupe:")

#cursor on given message:print body of selected message in list
def on_listmessages_cursor(source=None, event=None):
 #get index in listmessages
 res=ui.get_widget("listmessages").get_cursor()
 goodnumber=res[0][0]
 ##print goodnumber
 ##print goodlist[goodnumber][0]
 buffer=ui.get_widget("messagebody").get_buffer()
 
 #first get real place where the text body begins...
 
 nb_firstline=len(conn.top(goodlist[goodnumber][0],1)[1:2][0])-1
 if not (conn.top(goodlist[goodnumber][0],1)[1:2][0][nb_firstline].startswith("--") or conn.top(goodlist[goodnumber][0],1)[1:2][0][nb_firstline].startswith("This is")):
  mail=conn.top(goodlist[goodnumber][0],LINES_NB)[1:2][0][nb_firstline:]
  mytexte=""
  for text in mail:
   ##print text
   mytexte +="\n"+text
  buffer.set_text(mytexte)               
 else:             
  buffer.set_text("mail is mainly an attachment")
            
#help function that actually fills the listbox with the messages
              
def construire_arbre(clear=0):
 #goodlist contains the message numbers and their size 
 global goodlist
 global treestore
 #creater store with one column
 treestore = gtk.ListStore(str)
  
 ui.get_widget("listmessages").set_model(treestore)
 cell0 = gtk.CellRendererText()
 #definition de la colonne 0
 Col0 = gtk.TreeViewColumn("From"+" "*15+"Subject"+" "*30+"size ", cell0, text=0)
 ui.get_widget("listmessages").append_column(Col0)

 if not clear==1:  #if clear==1 we wont fill the tree,just use to clear
  #get 10 messages from: , subject:
  for i in range(len(goodlist)):
   #we get the text right....
   goodlist[i][0]
   rowbrute= conn.top(goodlist[i][0],1)[1:2][0]
   fromtxt=""
   subject=""
   for j in rowbrute:
    if j.startswith("From:"):
     fromtxt=j[5:]
    if j.startswith("Subject:"):
     subjecttxt=j[8:]
  # Add lines corresponding to messages
   row1= ([fromtxt+"\t"+"\t"+subjecttxt+"\t"+str(int(goodlist[i][1]/1000))+"Ko"])
   treestore.append(row1)
  
 
#help function to  get max and  index in list message
def maxetindice(list):
 liste=list[1:]
 max=0
 indice=0
 for i in range(len(liste)):
    if max<liste[i]:
     max=liste[i]
     indice=i 
 return indice+1,max

#help function to get total size of display messages
def taille_msg(liste):
 taille=0
 for i in liste:
  taille += i[1]
 return taille
 
#2-without class:how to connect slots to our functions
ui.signal_autoconnect(globals())
#main loop waiting for gui events....
gtk.main()

Conclusion :


ce serait bien sur plus simple a coder (voire plus efficace) en ligne de commandes mais mon interet etait surtout de reutiliser glade,donc glade requis pour faire marcher le tout...

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.