2013-06-19 29 views
0

我一直在嘗試修改Reply類的suds.transport。如何monkeypatch suds.transport.Reply?

我試着用以下方法:

import suds.transport 
old_reply = suds.transport.Reply 

class Reply2: 
    """ 
    A transport reply 
    @ivar code: The http code returned. 
    @type code: int 
    @ivar message: The message to be sent in a POST request. 
    @type message: str 
    @ivar headers: The http headers to be used for the request. 
    @type headers: dict 
    """ 

    def __init__(self, code, headers, message): 
     """ 
     @param code: The http code returned. 
     @type code: int 
     @param headers: The http returned headers. 
     @type headers: dict 
     @param message: The (optional) reply message received. 
     @type message: str 
     """ 
     print 'hello, i patched the class' 
     self.code = code 
     self.headers = headers 
     self.message = message 

    def __str__(self): 
     s = [] 
     s.append('CODE: %s' % self.code) 
     s.append('HEADERS: %s' % self.headers) 
     s.append('MESSAGE:') 
     s.append(self.message) 
     return '\n'.join(s) 

suds.transport.Reply = Reply2 

當執行client請求(如你通常會用肥皂水),默認的回答是用來代替修補之一。

這種方法失敗的原因是什麼?

注意:看來,單獨修補__init__給出更好的結果。但是我需要在課堂上修改更多的行爲。 最後,我想重寫回復得到傳入的附件,比如問on SO和解決方案here

回答

1

suds.transport.http模塊導入Reply與線:

from suds.transport import * 

,並做到這一點前你可以修補它。您還需要更新該參考:

import suds.transport.http 
suds.transport.http.Reply = Reply2 
+0

是的,你說得對!順便說一句:'suds.transport.https'還有'import *',所以我也考慮過修補它。但在我看來,這種說法是不必要的,對吧? 'suds.transport .__ init __。py'中沒有使用任何類。 – taper

+0

@taper:'.https'不使用'Reply'類,否;它只是從'.http.HttpTransport'繼承,它引用了它導入的'Reply'。 –

相關問題