How Can I overide a function in an existing class

E

Eddie

Hi all,

I am using ZSI SOAP library for a web service. My service runs fine,
but it outputs a line

localhost - - [14/Sep/2004 20:21:55] "POST / HTTP/1.1" 200 -

every time the web service is accessed. I traced it down to the
BaseHTTPServer.log_message function that the ZSI Soap library uses for
the web server.

I want to turn this service into a windows NT service and I need to
stop the program from outputing the line.

I cannot make a new class derived from BaseHTTPServer and override the
log_message function because I can't change the web server object used
by ZSI.

I also can't redirect stderr because the service messes with stdout
and error already.

I have changed the BaseHTTPServer.py file to do what I want and
included it in the directory with my service. This works, but there
has to be a better way.

Is there some other way to override the function?

Thanks in advance for any help

Eddie
 
P

Peter Hansen

Eddie said:
I have changed the BaseHTTPServer.py file to do what I want and
included it in the directory with my service. This works, but there
has to be a better way.

Is there some other way to override the function?

Often I've found it possible and relatively "clean" to write
code that modifies the class involved dynamically, often
just wrapping the existing method in another one that does
what I want (or undoes it, perhaps), or just by sticking
in a replacement.

Trivialized example that I hope gives you the idea:

# file BaseHTTPServer.py
class SomeClass:
def someMethod(self):
log_message(somemsg) # undesirable call
# other stuff that is good


# in your own file

import BaseHTTPServer
def replacementMethod(self):
# only the good stuff

BaseHTTPServer.SomeClass.someMethod = replacementMethod

# followed by the rest of your existing code that
# actually instantiates a SomeClass, even if indirectly

Any help?

-Peter
 
E

Eddie

Trivialized example that I hope gives you the idea:
# in your own file

import BaseHTTPServer
def replacementMethod(self):
# only the good stuff

BaseHTTPServer.SomeClass.someMethod = replacementMethod

Any help?

-Peter

Thanks Peter. This is exactly what I was looking for. It works
perfectly and is very simple.

Thanks again
Eddie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top