Archive

Archive for the ‘Python’ Category

Working with TAR and ZIP files in Python

January 7th, 2010

>>>import os,tarfile

>>> tarfile=tarfile.open(’/data/archive-1-1-1.tgz’,'r’)
>>> for x in tarfile:
…     print x

<TarInfo ‘./tmp/’ at 0xb7d9588c>
<TarInfo ‘./tmp/biteoff/’ at 0xb7d9558c>
<TarInfo ‘./tmp/biteoff/bite_offload.tgz’ at 0xb7d959cc>
<TarInfo ‘./tmp/biteoff/aesid.log’ at 0xb7d95a2c>
<TarInfo ‘./tmp/biteoff/lruid.xml’ at 0xb7d95a8c>
<TarInfo ‘./tmp/biteoff/BITEdump.sql’ at 0xb7d95aec>
<TarInfo ‘./tmp/biteoff/offload.cfg’ at 0xb7d95b4c>

ZIP

>>> import os, zipfile
>>> zipfile=zipfile.ZipFile(’/home/jesterj/foo.zip’,'r’)
>>> for x in zipfile.namelist():
…     print x

index.php

Python

Return diff in list or set.

June 18th, 2009

There is no way to do a function diff on a list. However you can do a loop.

list1 = [1, 2, 3, 5, 7,  11]
list2 = [1, 2, 4, 8, 16, 32]
diff_list = []
for item in list1:
    if item not in list2:
        diff_list.append(item)

print diff_list
[3, 5, 7, 11]

Or use sets….

from sets import Set
set1=Set(['a','b','c'])
set2=Set(['d','b','c'])
diff=set1-set2
print diff
 

Set(['a'])

for x in diff:
    print x

a

Or conver list to a set…

list1=['a','b']
list2=['b','c']

set1=set(list1)
set2=set(list2)

diff=set1-set2

Python

Read text file with ‘\xeff’ garbage data to utf-8

May 8th, 2009

import codecs
someFile=”donkeys.txt”
fileObj = codecs.open(someFile, “r”, “utf-8″ )
u = fileObj.read()
print u

Python

python: get mp3 meta info

April 26th, 2009

apt-get install python-mutagen

#!/usr/bin/python
from mutagen.mp3 import MP3
import os

for root, dirs, files in os.walk('/mnt/music'):
for name in files:
joined=os.path.join(root, name)
audio=MP3(joined)
if audio.info.bitrate < 128000:
print joined
print audio.info.bitrate

				

Python

Python: Escaping input for db insertion

March 16th, 2009
import MySQLdb
data="jeremiah's new laptop"
data=MySQLdb.escape_string(data)
cursor.execut("INSERT INTO table1 name VALUES (data);")

Python

Access MySQL from Python

March 10th, 2009
import MySQLdb #python-mysqldb
conn = MySQLdb.connect (host = "localhost", user = "root", passwd = mysql_pass, db = "")
cursor = conn.cursor ()
cursor.execute("use database")

Python

How to parse a CVS File : Python

March 10th, 2009
#!/usr/bin/python
import csv
import MySQLdb

spamReader = csv.reader(open('eggs.csv'), delimiter=' ', quotechar='|')
for row in spamReader:
print ' '.join(row)

Python

Python: Working with Lists

December 29th, 2008

LIST ARE DYNAMIC.

def list():
items=[]
i=0
while i<=5:
items.append(i)
i+=1
return items

a=list()

print a

$ python test.py
[0, 1, 2, 3, 4, 5]

Python

Python: Return variables from a function example.

December 26th, 2008
#!/usr/bin/python
def test():
      a="hello"
      b="no"
      return a,b
print test()
>['hello','no']
a=test()
print a[0]
>hello
print a[1]
>no

Python

Sending mail in Python

December 5th, 2008
#Sample function to send mail in python

import smtplib
from email.mime.text import MIMEText #need this for subject line in email

def send_mail(mail_message):

        smtpserver='smtpserver.com'
        host = socket.gethostname()

        RECIPIENTS = ['email@host.com']
        SENDER = ‘root@%s.host.com’ % host
        MESSAGE = “”"Subject: [Nagios] Loadscript Errors
From: nagios@%s.host.com

%s
“”" % (host, mail_message)

        session = smtplib.SMTP(smtpserver)
        smtpresult = session.sendmail(SENDER, RECIPIENTS, MESSAGE)

        session.close()

Python

Python remove duplicate files script (based on md5 hash)

December 2nd, 2008
#!/usr/bin/python
# This script compares the file contents of two directories
# using and md5 hash and deletes any duplicate files from the
# target directory

import os
import md5
import sys
import base64

source={}
target={}

source_path="/home/jesterj/source"
target_path="/home/jesterj/target"

def gather_files(dir,dict):
	os.chdir(dir)
	names=os.listdir(dir)
	for filename in names:
		#read file
		f = open(filename)
		file_contents = f.read()

		#create new hash
		hash=md5.new()
		hash.update(file_contents)
		hex=hash.hexdigest()

		#add to dictionary
		dict[hex] = filename

def remove_matches():
	has_dupes=0
	for hash,file in source.iteritems():
		if target.has_key(hash):
			print “%s checksum %s exists in both dirs.” % (file, hash)
			#remove_target_file(file)
			has_dupes=True
	if has_dupes==False:
		print “No duplicate files found.”

def remove_target_file(a):
	os.chdir(target_path)
	os.system(”rm ” + a)
	print “File ” + target_path+”/”+a + ” removed”

def main():
	try:
		gather_files(source_path,source)
		gather_files(target_path,target)
		remove_matches()
	except:
		print “error”

main()

Python

Parse error log and send email

November 4th, 2008

This post gives an example of how I wrote a script that parsed an error log looking for ‘Traceback’ errors and emailing them to the admin on a daily basis. The cool thing about this script is that it keeps a byte track of each time it reads the log file, stores that byte count in a temp file then picks up at the byte count the next day so you don’t get duplicate emails from tracebacks on a previous day…

let’s call this file tracebacks.py

#!/usr/bin/python
import os
import sys
import time
import smtplib
import socket #required to get host name
from email.mime.text import MIMEText #need this for subject line in email

def main():
	line=""
	line_num=0
	last_byte_read=""
	byte_num=""
	stored_byte_num=""
	traceback_count=0

	log_file=open("/var/log/loadscript/<logfilename here>","r")
	tmp_file="/tmp/loadscript.tmp"

	###############################
	#check if required file exists
	###############################
	if os.path.exists(tmp_file):
		writefile = open(tmp_file,'r')
		stored_byte_num=writefile.read()
	else:
		writefile = open(tmp_file,'w')
		writefile.write("")
		writefile.close()

	writefile = open(tmp_file,'r')			 

	################################
	# find last byte searched in file
	################################
	if len(stored_byte_num)>0:
		stored_byte_num=int(stored_byte_num)
		log_file.seek(stored_byte_num)

	data = log_file.readlines()
	was_found=False

	#loop through file
	for x in data:
		#print x.strip()
		line_num=line_num + 1
		if x.startswith('Traceback'):
			traceback_count += 1
			line += "\n+++++++++++++++++++++++++++++++++++++++"
			#########################################################"
			line += "\nError on Line: %s\n" % line_num
			line += "\n"+x
			was_found=True
		if was_found:
			if x.find('File')==2:
				line += x
		else:
			was_found = False

	# record last byte read in tmp file
	last_byte_read=log_file.tell()
	last_byte_read=str(last_byte_read)
	handle=open(tmp_file,'w')
	handle.write(last_byte_read)
	handle.close()

	#print last_byte_read

	#send mail summary
	if was_found==False:
		mail_message = "\nDid not find any Tracebacks!\n"
	else:
		mail_message = "SCRIPT SUMMARY\n"
	       	mail_message += "================================================\n"
		mail_message += "Tracebacks found: %s\n" % traceback_count
		mail_message += "Last byte checked: %s\n" % last_byte_read
		mail_message += "Script will start at this number next search!\n"
       		mail_message += "================================================\n"
		mail_message += line

		send_mail(mail_message)

        print "Errors found and sent to pse-admin"
        #print "%s" % mail_message

def send_mail(mail_message):

	smtpserver='smtp.example.com'
	host = socket.gethostname()

	RECIPIENTS = ['username@domain.com']
	SENDER = ‘root@%s.mascorp.com’ % host
	MESSAGE = “”"Subject: [Nagios] Loadscript Errors
From: nagios@%s.mascorp.com

%s
“”" % (host, mail_message)

	session = smtplib.SMTP(smtpserver)
	smtpresult = session.sendmail(SENDER, RECIPIENTS, MESSAGE)

	session.close()

try:
	main()

except:
        health = ‘UNKNOWN’
        result = 3
        print “Error: Check script”
        sys.exit(1)

Python ,