String comparison problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Senthoorkumaran Punniamoorthy

    String comparison problem

    I am printing these information.

    print string.lower(in fo_res[2])
    print string.lower(md 5sum(f_md5))
    print len(string.lowe r(info_res[2]))
    print len(string.lowe r(md5sum(f_md5) ))
    print str(string.lowe r(md5sum(f_md5) ) == string.lower(in fo_res[2]))

    and the output are

    01b7ebfc27437a9 0cc421c50df8f9a c5
    01b7ebfc27437a9 0cc421c50df8f9a c5
    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.


  • wes weston

    #2
    Re: String comparison problem

    Senthoorkumaran Punniamoorthy wrote:[color=blue]
    > I am printing these information.
    >
    > print string.lower(in fo_res[2])
    > print string.lower(md 5sum(f_md5))
    > print len(string.lowe r(info_res[2]))
    > print len(string.lowe r(md5sum(f_md5) ))
    > print str(string.lowe r(md5sum(f_md5) ) == string.lower(in fo_res[2]))
    >
    > and the output are
    >
    > 01b7ebfc27437a9 0cc421c50df8f9a c5
    > 01b7ebfc27437a9 0cc421c50df8f9a c5
    > 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=809 6
    > 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_buf size)
    > if not data:
    > break
    > m.update(data)
    > except IOError, msg:
    > log.error('I/O error: '+ str(msg))
    > log.error('Syst em 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(bufs ize)
    > if not data:
    > break
    > fp_out.write(da ta)
    > fp_out.flush()
    > #fp_out.write(' senthoor')
    > #fp_out.flush()
    > except IOError, msg:
    > log.error('Erro r resolving address: '+ str(msg))
    > log.error('Syst em Abort')
    > sys.exit(1)
    > except socket.error, msg:
    > log.error('Soke t error: '+ str(msg))
    > log.error('Syst em Abort')
    > sys.exit(1)
    >
    > def send_mail(mails erver,from_addr , to_addr, file_name):
    > msg = 'Greetings all, \n The lastest virus definition file' +
    > file_name + ' has been down loaded'
    > server = smtplib.SMTP(ma ilserver)
    > server.set_debu glevel(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(listR egEx,source):
    > #this needs improvement. The compilation is unnecessary and will be
    > removed
    > return map(lambda x:x.group(1),ma p(lambda x: sre.search(x,
    > source),listReg Ex))
    >
    > 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(lo gging.DEBUG)
    > if x[0] == '-v':
    > print 'theRetriever '+ version +', MotleyPythons'
    > sys.exit(0)
    > except getopt.GetoptEr ror,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(file Name,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(fileNa me + '\n')
    > fp.close
    > except IOError,msg:
    > log.error('I/O error: '+ str(msg))
    > log.error('Syst em Abort')
    > sys.exit(1)
    >
    > #Logging Initialised
    > logging.basicCo nfig()
    > log = logging.getLogg er('Retriever')
    > log.setLevel(lo gging.INFO)
    > process_args(sy s.argv)
    >
    >
    > # Get information from config file
    > config = property('confi g.cfg')
    > log.debug('Conf ig Loaded')
    > dllog=config.ge t_safe('LOCATIO N','')+'dledfil e.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('Retr ies :' + str(config.get_ safe('RETRIES', 0)))
    > while md5_error and (no_of_trys<=in t(config.get_sa fe('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('UR L'))
    > log.debug(confi g.get('URL') + 'has been read')
    > except urllib2.URLErro r, msg:
    > log.error('Erro r Reading ' + config.get('URL ') + '\n' + str(msg))
    > log.error('Syst em Abort')
    > sys.exit(1)
    > htmlsource = f1.read()
    > f1.close()
    >
    > info_regex =
    > ['href="(.*2\d{7 }-\d{3}-i32.exe)"','hre f=".*(2\d{7}-\d{3}-i32.exe)"','MD5 </a>:\s(.*)\s<a']
    >
    > info_res = grab_info(info_ regex,htmlsourc e)
    > log.debug('retr ieved 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=o pen(config.get_ safe('LOCATION' ,'')+info_res[1],'wb')
    > authinfo = urllib2.Request (config.get('UR L'))
    > virusdefserver =
    > urllib2.urlopen ('http://'+authinfo.get_ host()+info_res[0])
    > file_writer(vir usdefserver,vir usdeflocal)
    > virusdeflocal.c lose()
    > virusdefserver. close()
    >
    > f_md5=open(conf ig.get_safe('LO CATION','')+inf o_res[1],'rb')
    > if string.lower(md 5sum(f_md5)) == string.lower(in fo_res[2]):
    > log.debug("Yipe e !!!, 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(confi g.get('SMTP'),c onfig.get('FROM _ADDR'),config. get('TO_ADDR'), filename)
    >
    > except KeyError, msg:
    > log.error('Mail not sent: ' + str(msg))
    >
    > else:
    > log.debug("Bumm er")
    > md5_error=1[/color]


    Senthoorkumaran Punniamoorthy,
    [color=blue][color=green][color=darkred]
    >>> s1='01b7ebfc274 37a90cc421c50df 8f9ac5'
    >>> s2=s1
    >>> import string
    >>> print str(string.lowe r(s1))==string. lower(s2)[/color][/color][/color]
    True

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

    Comment

    • Piet van Oostrum

      #3
      Re: String comparison problem

      >>>>> "Senthoorkumara n Punniamoorthy" <solution88pro@ hotmail.com> (SP) wrote:

      SP> I am printing these information.
      SP> print string.lower(in fo_res[2])
      SP> print string.lower(md 5sum(f_md5))
      SP> print len(string.lowe r(info_res[2]))
      SP> print len(string.lowe r(md5sum(f_md5) ))
      SP> print str(string.lowe r(md5sum(f_md5) ) == string.lower(in fo_res[2]))

      SP> and the output are

      SP> 01b7ebfc27437a9 0cc421c50df8f9a c5
      SP> 01b7ebfc27437a9 0cc421c50df8f9a c5
      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 <piet@cs.uu.n l>
      URL: http://www.cs.uu.nl/~piet [PGP]
      Private email: P.van.Oostrum@h ccnet.nl

      Comment

      Working...