運行空字符串我的Python 2.7.8在CentOS 6.8在我的服務器時的Apache2 + WSGI建。我的應用程序應該使用http POST處理收到的日期,然後根據從本地.xml文件獲取的XML模板創建指令。最後,它必須以200 OK響應發送xml指令。應用程序的邏輯似乎工作正常,我可以看到我更新的XML樹:Python的LXML etree.tostring()返回在mod_wsgi的
print etree.tostring(root, pretty_print=True, xml_declaration-True, encoding='UTF-8')
的問題出現在我的代碼,我正在做同樣的操作下一行要發生的,但試圖分配輸出變量:
xml_body = etree.tostring(root, pretty_print=True, xml_declaration-True, encoding='UTF-8')
打印xml_body
輸出爲空字符串,這樣我的應用程序,然後返回一無所獲到Apache。
我的環境信息可能會有所幫助:
==For bug report ===
Python : sys.version_info(major=2, minor=7, micro=8, releaselevel='final', serial=0)
lxml.etree : (3, 6, 4, 0)
libxml used : (2, 7, 6)
libxml compiled : (2, 9, 4)
libxslt used : (1, 1, 26)
libxslt compiled : (1, 1, 29)
它類似於這樣bug report然而,筆者提到的它並沒有在所有的工作。還有一個request其中包含類似的問題,但它仍然沒有解決。 我已經簽出我可能會成功地在Python CLI播放相同的場景:
Python 2.7.8 (default, May 15 2016, 12:46:09)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from lxml import etree
>>> foo = etree.Element('foo')
>>> foo
<Element foo at 0x7f5097c4fc20>
>>> foo.tag
'foo'
>>> foo.text = 'barrisimo'
>>> xmlb = etree.tostring(foo, pretty_print=True, xml_declaration=True, encoding='UTF-8')
>>> print xmlb
<?xml version='1.0' encoding='UTF-8'?>
<foo>barrisimo</foo>1
曾有人遇到過同樣的問題?我在死路一條,我會很感激任何幫助,想法或有用的鏈接。
似乎在'mod_wsgi'中'tostring()'有問題,仍然沒有解決方案。 – furas
你能修復你的代碼片斷嗎?這甚至不是有效的Python。你不能像在''xml_body = print etree.tostring(...)''中那樣將'print''的結果賦值給一個變量。 –
您是否注意到鏈接帖子中的建議,其中討論了使用舊版本的libxml和libxslt而不是最初編譯的lxml?你的情況似乎完全匹配,並提到,可能會導致問題。你確定你升級了這些庫的系統包嗎? –