![](/img/trans.png)
[英]How do I get the XML format of Bugzilla given a bug ID using python and XML-RPC?
[英]How do I handle Python XML-RPC output and exceptions?
我已經創建了一個簡單的Python XML-RPC實現,主要基於這些示例。
但是,它發送如下輸出:
foo.bar.com - - [13/Feb/2010 17:55:47] "POST /RPC2 HTTP/1.0" 200 -
...到終端,即使我使用>>
或>
將標准輸出和標准錯誤重定向到文件。 我正在使用以下行:
python foobar 2>&1 >> foobar.log
看起來它似乎不是標准輸出,而是其他地方。
此外,當收到請求時發生異常時,整個應用程序崩潰時出現此錯誤:
----------------------------------------
Exception happened during processing of request from ('1.2.3.4', 51284)
我該如何處理這個異常? 我需要優雅地恢復,只記錄異常消息而不是服務器崩潰。
我猜您正在使用示例中的SimpleXMLRPCServer
類。 在這種情況下,只需在創建時提供參數logRequests
:
server = SimpleXMLRPCServer(("localhost", 8000), logRequests = False)
這將抑制請求記錄。
至於異常,他們登錄BaseServer
(參見“SocketServer.py”的源代碼):
def handle_error(self, request, client_address):
"""Handle an error gracefully. May be overridden.
The default is to print a traceback and continue.
"""
print '-'*40
print 'Exception happened during processing of request from',
print client_address
import traceback
traceback.print_exc() # XXX But this goes to stderr!
print '-'*40
如您所見,第一部分寫入stdout,這就是為什么&2>1
不能完全運行的原因。 如果要禁止它們,請覆蓋或覆蓋該方法。
這實際上是一個shell重定向問題,而不是python問題。 當你說
python foobar 2>&1 >> foobar.log
你是第一個重定向錯誤輸出到標准輸出,然后第二重定向標准輸出到foobar.log。 stderr不會自動重定向到foobar.log。 stderr仍然指向原來的標准輸出。
所以改為:
python foobar >> foobar.log 2>&1
另見http://bash-hackers.org/wiki/doku.php/syntax/redirection (搜索“多重重定向”)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.