snippets / al9orsan / 

All al9orsan's snippets (4)

  1. Convertir une archive RAR en ZIP

    Convertit chacune des archives RAR données en entrée en archives Zip. Nécessite l'installation du programme "rar".

     1 import sys
    2 import os
    3 import mimetypes
    4 from string import join
    5 from zipfile import ZipFile
    6
    7
    8 class RarToZipConverter:
    9 def __init__(self, input_file):
    10 mime_type = mimetypes.guess_type(input_file)[0]
    11
    12 if mime_type == 'application/rar':
    13 self.rarfile = input_file
    14 basename = input_file.split('.')[:-1]
    15 basename.append("zip")
    16 self.zipfile = join(basename,'.')
    17 else:
    18 self.rarfile = ""
    19 self.zipfile = ""
    20
    21 def list_rarfile_content(self):
    22 command = "rar v \"%s\""%(self.rarfile)
    23
    24 results = os.popen(command).readlines()
    25
    26 line = results.pop()
    27 while line != '-------------------------------------------------------------------------------\n':
    28 line = results.pop()
    29
    30 line = results.pop()
    31 self.file_list = []
    32 while line != '-------------------------------------------------------------------------------\n':
    33 line = results.pop()
    34 self.file_list.append(line.replace("\n","")[1:])
    35 line = results.pop()
    36
    37
    38 def open_rarfile(self):
    39 self.files_content = {}
    40 for filename in self.file_list:
    41 command = "rar P -inul \"%s\" \"%s\""%(self.rarfile,filename)
    42 content = os.popen(command,'rb').read()
    43 self.files_content[filename] = content
    44
    45
    46 def save_zipfile(self):
    47 out_zipfile = ZipFile(self.zipfile,'w')
    48 for filename in self.files_content:
    49 out_zipfile.writestr(filename,self.files_content[filename])
    50
    51 def convert(self):
    52 if self.rarfile != "":
    53 self.list_rarfile_content()
    54 self.open_rarfile()
    55 self.save_zipfile()
    56
    57
    58 if __name__ == "__main__":
    59 if len(sys.argv) > 1:
    60 for filename in sys.argv[1:]:
    61 print "Processing %s..."%(filename)
    62 Converter = RarToZipConverter(filename)
    63 Converter.convert()
    64 else:
    65 print "Usage : %s [rar archives]"%(sys.argv[0])
    first posted by zebra3 to python zip rar ... saved by 3 persons ... 0 comments ... 1 year, 2 months
  2. Création d'un mail html en python

    Cette classe permet d'envoyer un mail au format HTML en python. Très pratique pour de l'encoi de mail en masse.

     1 CLASSE MAIL.PY
    2
    3 import smtplib
    4 import MimeWriter
    5 import mimetools
    6 import StringIO
    7
    8 def sendHtmlMail(to,frommail,text,html):
    9 encoding = "base64"
    10 charset = "iso-8859-15"
    11
    12 #déclaration des buffers
    13 out = StringIO.StringIO()
    14 htmlin = StringIO.StringIO(html)
    15 txtin = StringIO.StringIO(html)
    16
    17 #déclaration et initialisation du writer
    18 writer = MimeWriter.MimeWriter(out)
    19 writer.addheader("Subject", text)
    20 writer.addheader("MIME-Version", "1.0")
    21 writer.addheader("From", frommail)
    22 writer.addheader("To", to)
    23 writer.startmultipartbody("alternative")
    24 writer.flushheaders()
    25
    26 #ajout de la partie text
    27 textPart = writer.nextpart()
    28 textPart.addheader("Content-Transfer-Encoding", encoding)
    29 pout = textPart.startbody("text/plain", [("charset", charset)])
    30 mimetools.encode(txtin, pout, encoding)
    31 txtin.close()
    32
    33 #On ajoute la partie html
    34 htmlPart = writer.nextpart()
    35 htmlPart.addheader("Content-Transfer-Encoding", encoding)
    36 pout = htmlPart.startbody("text/html", [("charset", charset)])
    37 mimetools.encode(htmlin, pout, encoding)
    38 htmlin.close()
    39
    40 #on clot le mail
    41 writer.lastpart()
    42 mail = out.getvalue()
    43 out.close()
    44 smtp = smtplib.SMTP("localhost")
    45 #smtp.connect()
    46 smtp.sendmail(frommail, [to], mail)
    47 smtp.close()
    first posted by Fufu to python python mail html ... saved by 2 persons ... 0 comments ... 1 year, 2 months
  3. Envoyer un mail html avec python

    Comment envoyer un mail au format html en python. Pratique pour de gros envois de mail.

     1 import smtplib
    2 import MimeWriter
    3 import mimetools
    4 import StringIO
    5
    6 def sendHtmlMail(to,frommail,text,html):
    7 encoding = "base64"
    8 charset = "iso-8859-15"
    9
    10 #déclaration des buffers
    11 out = StringIO.StringIO()
    12 htmlin = StringIO.StringIO(html)
    13 txtin = StringIO.StringIO(html)
    14
    15 #déclaration et initialisation du writer
    16 writer = MimeWriter.MimeWriter(out)
    17 writer.addheader("Subject", text)
    18 writer.addheader("MIME-Version", "1.0")
    19 writer.addheader("From", frommail)
    20 writer.addheader("To", to)
    21 writer.startmultipartbody("alternative")
    22 writer.flushheaders()
    23
    24 #ajout de la partie text
    25 textPart = writer.nextpart()
    26 textPart.addheader("Content-Transfer-Encoding", encoding)
    27 pout = textPart.startbody("text/plain", [("charset", charset)])
    28 mimetools.encode(txtin, pout, encoding)
    29 txtin.close()
    30
    31 #On ajoute la partie html
    32 htmlPart = writer.nextpart()
    33 htmlPart.addheader("Content-Transfer-Encoding", encoding)
    34 pout = htmlPart.startbody("text/html", [("charset", charset)])
    35 mimetools.encode(htmlin, pout, encoding)
    36 htmlin.close()
    37
    38 #on clot le mail
    39 writer.lastpart()
    40 mail = out.getvalue()
    41 out.close()
    42 smtp = smtplib.SMTP("localhost")
    43 #smtp.connect()
    44 smtp.sendmail(frommail, [to], mail)
    45 smtp.close()
    first posted by Fufu to python python mail ... saved by 7 persons ... 0 comments ... 1 year, 2 months
  4. connection a une base de donnée MySQL en C

    Code en C permettant l'accès a une base de donnée MySQL C code wich permit MySQL access

     1 /*********************************************************
    2 **Attention il est impératif de compiler ce code avec l'option de compilation -lmysqlclient
    3 **********************************************************/
    4
    5 #include <stdio.h>
    6 #include <errno.h>
    7 #include <stdlib.h>
    8 #include <iostream.h>
    9 #include <fstream>
    10 #include <mysql/mysql.h>
    11 #include <sys/types.h>
    12 #include <sys/wait.h>
    13
    14 #define MY_SERVER_HOST "localhost"
    15 #define MY_SERVER_PORT 0
    16 #define MY_ACCOUNT "root"
    17 #define MY_PASS "test"
    18 #define MY_DB_NAME "itest"
    19 #define MY_TABLE_NAME "test_tbl"
    20 #define MY_UX_SOCK NULL
    21 #define MY_CLIENT_FLAG 0
    22
    23 void main()
    24 {
    25 MYSQL *mysql;
    26 MYSQL_RES *res;
    27 MYSQL_ROW row;
    28 MYSQL_ROW rowchamps;
    29 const char *query;
    30 string qutmp;
    31
    32 int t,f,tt,ff;
    33
    34 mysql=mysql_init(NULL);
    35
    36 if (!mysql_real_connect(mysql,MY_SERVER_HOST,MY_ACCOUNT,MY_PASS,bd,MY_SERVER_PORT,MY_UX_SOCK,MY_CLIENT_FLAG)) {
    37 printf( "Erreur de connexion : %s\n",mysql_error(mysql));
    38 }
    39 else {
    40 qutmp = "SHOW FULL COLUMNS FROM TABLE ";
    41 query = qutmp.c_str();
    42 t=mysql_real_query(mysql, query, (unsigned int) strlen(query));
    43 if((res=mysql_use_result(mysql))) {
    44 f=mysql_num_fields(res);
    45 while((row=mysql_fetch_row(res))) {
    46 for(f=0;f<t;f++){
    47 cout<<row[f]<<endl;
    48 }
    49 }
    50 mysql_free_result(res);
    51 }
    52 }
    53 mysql_close(mysql);
    54 }
    first posted by cybersax to c c c++ ... saved by 7 persons ... 0 comments ... 1 year, 2 months
showing 10, 25, 50 items per pages

Pages : 1