String comparison

rzed Dick.Zantow at lexisnexis.com
Wed Aug 21 22:26:29 CEST 2002


"David Iungerich" <david.iungerich at kwe.com> wrote in message
news:mailman.1029948751.3285.python-list at python.org...
> I'm new to Python.  I'm most comfortable with Java, but have done
work in several other languages.  So far Python has proven rather
annoying to deal with.  I'm trying to do a simple string comparison,
but have had problems thus far.  Essentially, I have code that does an
http POST.  I want to compare the response string with a copy of it
that is in a file (previous request).  This is essentially a test to
see if a server is up and retrieving data correctly.  If not, I'll be
sending an e-mail to an admin.  The actual string comparison is
eluding me, though.  I've tried the following.  Any help would be
appreciated.
>
> Obviously, this code is not complete.  I'll be replacing several
values with attibutes pulled from an XML file.  I'm just hardcoding
things to test the process right now.
>
> postReply = urllib.urlopen("http://css.kwe.com/web.forte",
postdata).read()
>  print postReply
>
>  f = open("D:\PythonServerMonitor\KWE1144589.dat","r")
>  controlData = f.read()
>  f.close()
>
>  if (str(postReply) == str(controlData)):       #Here is the
problem.
>   response = "<Result>Server Response - OK</Result>"
>  else:
>   response = "<Result>Server Not Responding</Result>"
>     # e-mail appropriate party.
>
>  return response
>
> I've also tried...
>  if (strcmp(postReply, controlData) == 0):
>
> In Java, I would simply use the .equals( ) method available to
string objects.
>
> Thoughts?



I think you basically have it right. I snapped up your code, read the
URL and wrote the reply to a file. I then read the file and compared
it to the reply. It matched. Imagine that!

I don't know what you have in your comparison file, or how it got
there, but I suspect there's some extra nonprintable characters
involved somewhere.

--
rzed




import urllib

postdata = ""

postReply = urllib.urlopen("http://css.kwe.com/web.forte",
postdata).read()

print "postReply=[%s]" % postReply

ofp = open( "c:/holdit.xx", "w" )
ofp.write( postReply )
ofp.close()

f = open("c:/holdit.xx","r")
# f = open("D:\PythonServerMonitor\KWE1144589.dat","r")
controlData = f.read()
f.close()

print "controlData=[%s]" % controlData

if (str(postReply) == str(controlData)):       #Here is the problem.
    response = "<Result>Server Response - OK</Result>"
else:
    response = "<Result>Server Not Responding</Result>"
# e-mail appropriate party.

print response





More information about the Python-list mailing list