2016-12-09 17 views
3

運行空字符串我的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 

曾有人遇到過同樣的問題?我在死路一條,我會很感激任何幫助,想法或有用的鏈接。

+0

似乎在'mod_wsgi'中'tostring()'有問題,仍然沒有解決方案。 – furas

+1

你能修復你的代碼片斷嗎?這甚至不是有效的Python。你不能像在''xml_body = print etree.tostring(...)''中那樣將'print''的結果賦值給一個變量。 –

+0

您是否注意到鏈接帖子中的建議,其中討論了使用舊版本的libxml和libxslt而不是最初編譯的lxml?你的情況似乎完全匹配,並提到,可能會導致問題。你確定你升級了這些庫的系統包嗎? –

回答

1

有同樣的問題。

我的測試文件test.py

from lxml import etree 


def application(env, start_response): 
    foo = etree.Element("foo") 
    foo.text = "bar" 
    out = etree.tostring(foo) 

    print('-------------') 
    print(foo) 
    print('=============') 
    print(out) 
    print('-------------') 

    if start_response: 
     start_response('200 OK', [('Content-Type', 'text/html')]) 

    return [b"TEST"] 


if __name__ == "__main__": 
    application(None, None) 

當在命令行模式下運行:

python test.py 

它返回:

------------- 
<Element foo at 0x7f5b80671488> 
============= 
<foo>bar</foo> 
------------- 

但是,如果我在WSGI模式下運行,返回None :

uwsgi --http :9090 --wsgi-file test.py 

然後接收

------------- 
<Element foo at 0x7f8f84aa7638> 
============= 

------------- 

匹格:

lxml==3.7.2 
uWSGI==2.0.14 

庫版本:

libxml used   : (2, 9, 3) 
libxml compiled  : (2, 9, 3) 
libxslt used  : (1, 1, 29) 
libxslt compiled : (1, 1, 29) 

問題的核心原因是未知的,但問題是通過除去包裝libxml2-dev並重新安裝uwsgi解決:

aptitude remove libxml2-dev 
pip uninstall uwsgi 
pip install --no-cache-dir uwsgi 

所以,不知何故包的libxml2-dev的是不兼容uwsgi。

1

萬一libxml版本在運行時從編譯版本不同就意味着有安裝在您的系統中有多個版本libxml

libxml used   : (2, 7, 8) 
libxml compiled  : (2, 9, 3) 

在我的情況mod_php5是問題。我通過重新編譯沒有PHP的Apache來解決這個問題。事實證明,PHP使用的是版本爲2.7.8的libxmlmod_wsgi中使用的python解釋器指向的是相同的xmllib,而不是使用python。