2016-12-09 19 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。