By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,866 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,866 IT Pros & Developers. It's quick & easy.

String comparison problem

P: n/a
I am printing these information.

print string.lower(info_res[2])
print string.lower(md5sum(f_md5))
print len(string.lower(info_res[2]))
print len(string.lower(md5sum(f_md5)))
print str(string.lower(md5sum(f_md5)) == string.lower(info_res[2]))

and the output are

01b7ebfc27437a90cc421c50df8f9ac5
01b7ebfc27437a90cc421c50df8f9ac5
32
32
False

I was wondering why the last print statement is returning false when two
strings are identical?

Senthoor

__________________________________________________ _______________
Tired of spam? Get advanced junk mail protection with MSN 8.
http://join.msn.com/?page=features/junkmail

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Senthoorkumaran Punniamoorthy wrote:
I am printing these information.

print string.lower(info_res[2])
print string.lower(md5sum(f_md5))
print len(string.lower(info_res[2]))
print len(string.lower(md5sum(f_md5)))
print str(string.lower(md5sum(f_md5)) == string.lower(info_res[2]))

and the output are

01b7ebfc27437a90cc421c50df8f9ac5
01b7ebfc27437a90cc421c50df8f9ac5
32
32
False

I was wondering why the last print statement is returning false when two
strings are identical?

Senthoor

__________________________________________________ _______________
Tired of spam? Get advanced junk mail protection with MSN 8.
http://join.msn.com/?page=features/junkmail
import urllib2
import sre
import string
import sys
import md5
import socket
import logging
import getopt
import os
from property import property

md5_bufsize=8096
bufsize=8096
'''
Change History
--------------
1.01g:
i. file close from file_writer() removed so that caller can close the file
ii. exception handling introduced for mail sending to handle missing
SMTP config
iii.file_Dled() method is replaced
iv. The program doesn't exit if it can't find the logfile anymore. It
just downloads again.
v. small bug fixes
vi. grab_info was made little less complicated by removing the
compilation of regex

1.01h:
i. If the MD5 check sum doesn't match, trying n number of times
'''
version="1.01h"

# MD5 checksum program taken from python_home/tools/scripts/md5sum.py
# That was a stand alone script with many options
# Just took the function which did the checksumming, and reduced it.

def md5sum(fp):
m = md5.new()
try:
while 1:
data = fp.read(md5_bufsize)
if not data:
break
m.update(data)
except IOError, msg:
log.error('I/O error: '+ str(msg))
log.error('System Abort')
sys.exit(1)

return m.hexdigest()

'''Reads the file from fp_in and writes it to fp_out. fp_in normally is
the file being
downloaded and fp_out is the file being written to the local disk'''
def file_writer(fp_in, fp_out):
try:
log.info('File Downloading in Progress.',)
while 1:
data = fp_in.read(bufsize)
if not data:
break
fp_out.write(data)
fp_out.flush()
#fp_out.write('senthoor')
#fp_out.flush()
except IOError, msg:
log.error('Error resolving address: '+ str(msg))
log.error('System Abort')
sys.exit(1)
except socket.error, msg:
log.error('Soket error: '+ str(msg))
log.error('System Abort')
sys.exit(1)

def send_mail(mailserver,from_addr, to_addr, file_name):
msg = 'Greetings all, \n The lastest virus definition file' +
file_name + ' has been down loaded'
server = smtplib.SMTP(mailserver)
server.set_debuglevel(1)
server.sendmail(from_addr, to_addr, msg)
server.quit()

'''Takes a list of regular expressions and a Source(String) and searchs
source for
match and returns a list with all the matches'''
def grab_info(listRegEx,source):
#this needs improvement. The compilation is unnecessary and will be
removed
return map(lambda x:x.group(1),map(lambda x: sre.search(x,
source),listRegEx))

def show_usage():
print 'under construnction'

'''Processes the command line argument'''
def process_args(a):
try:
args = getopt.getopt(a[1:],'dv')
for x in args[0]:
if x[0] == '-d':
log.setLevel(logging.DEBUG)
if x[0] == '-v':
print 'theRetriever '+ version +', MotleyPythons'
sys.exit(0)
except getopt.GetoptError,msg:
show_usage()
sys.exit(1)

'''Checks the log file whether the file name exsits. If the file name
exsits then no need to download the file since its been already
downloaded'''
def file_Dled(file,fileName):
if os.path.exists(file):
return sre.search(fileName,open(file).read())

'''When the file is downloaded and written to the local disk the
log file updated with the file name'''
def file_DlOK(file,fileName):
exc = 0
try:
fp = open(file,'a')
fp.write(fileName + '\n')
fp.close
except IOError,msg:
log.error('I/O error: '+ str(msg))
log.error('System Abort')
sys.exit(1)

#Logging Initialised
logging.basicConfig()
log = logging.getLogger('Retriever')
log.setLevel(logging.INFO)
process_args(sys.argv)
# Get information from config file
config = property('config.cfg')
log.debug('Config Loaded')
dllog=config.get_safe('LOCATION','')+'dledfile.lst '
no_of_trys = 0

#this is being introduced so that file be downloaded if the it was
#not download failed and we are going to have a time testing this:-)
md5_error=1 #at the begining no file so md5_error is true:-)
log.debug('Retries :' + str(config.get_safe('RETRIES',0)))
while md5_error and (no_of_trys<=int(config.get_safe('RETRIES',0))):
no_of_trys +=1

#until its proven md5 mismatch exit we assume it doesn't
#this helps us only to repeat downloaing in the case of mismatch
#and if the downloading stops for other reasons
md5_error = 0
try:
f1 = urllib2.urlopen(config.get('URL'))
log.debug(config.get('URL') + 'has been read')
except urllib2.URLError, msg:
log.error('Error Reading ' + config.get('URL') + '\n' + str(msg))
log.error('System Abort')
sys.exit(1)
htmlsource = f1.read()
f1.close()

info_regex =
['href="(.*2\d{7}-\d{3}-i32.exe)"','href=".*(2\d{7}-\d{3}-i32.exe)"','MD5</a>:\s(.*)\s<a']

info_res = grab_info(info_regex,htmlsource)
log.debug('retrieved info \n' + str(info_res))
# Exit if file already loaded
if file_Dled(dllog,info_res[1]):
log.info('File already downloaded. Process Stopping',)
sys.exit(0)

#reading and writing to a local file
virusdeflocal=open(config.get_safe('LOCATION','')+ info_res[1],'wb')
authinfo = urllib2.Request(config.get('URL'))
virusdefserver =
urllib2.urlopen('http://'+authinfo.get_host()+info_res[0])
file_writer(virusdefserver,virusdeflocal)
virusdeflocal.close()
virusdefserver.close()

f_md5=open(config.get_safe('LOCATION','')+info_res[1],'rb')
if string.lower(md5sum(f_md5)) == string.lower(info_res[2]):
log.debug("Yipee !!!, it works!!")
# Have to copy the file to the common share
# Must have location in config file

# Put entry in logfile
file_DlOK(dllog,info_res[1])
try:

send_mail(config.get('SMTP'),config.get('FROM_ADDR '),config.get('TO_ADDR'),filename)

except KeyError, msg:
log.error('Mail not sent: ' + str(msg))

else:
log.debug("Bummer")
md5_error=1

Senthoorkumaran Punniamoorthy,
s1='01b7ebfc27437a90cc421c50df8f9ac5'
s2=s1
import string
print str(string.lower(s1))==string.lower(s2)

True

????????????????????????/
wes

Jul 18 '05 #2

P: n/a
>>>>> "Senthoorkumaran Punniamoorthy" <so***********@hotmail.com> (SP) wrote:

SP> I am printing these information.
SP> print string.lower(info_res[2])
SP> print string.lower(md5sum(f_md5))
SP> print len(string.lower(info_res[2]))
SP> print len(string.lower(md5sum(f_md5)))
SP> print str(string.lower(md5sum(f_md5)) == string.lower(info_res[2]))

SP> and the output are

SP> 01b7ebfc27437a90cc421c50df8f9ac5
SP> 01b7ebfc27437a90cc421c50df8f9ac5
SP> 32
SP> 32
SP> False

SP> I was wondering why the last print statement is returning false when two
SP> strings are identical?

They are not identical, because md5sum is reading from the file.
--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.***********@hccnet.nl
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.