Python lxml etree.tostring() 返回在 mod_wsgi 上运行的空字符串 [英] Python lxml etree.tostring() returns empty string running on mod_wsgi

查看:33
本文介绍了Python lxml etree.tostring() 返回在 mod_wsgi 上运行的空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 CentOS 6.8 上安装了 Python 2.7.8,我的服务器基于 Apache2 + WSGI 构建.我的应用程序应该处理通过 http POST 收到的日期,然后根据从本地 .xml 文件获取的 XML 模板创建指令.最后,它必须用 200 OK 响应发回 xml 指令.应用程序的逻辑似乎工作正常,我可能会看到我更新的 xml 树:

I have Python 2.7.8 on CentOS 6.8 where my server is built upon Apache2 + WSGI. My application should handle the date received with http POST, then create an instruction based on XML template gotten from local .xml file. Finally, it must send xml instruction back with 200 OK response. The logic of the application is seems working Ok and I may see my updated xml tree with:

print etree.tostring(root, pretty_print=True, xml_declaration-True, encoding='UTF-8')

该问题似乎发生在我正在执行相同操作但尝试将输出分配给变量的代码的下一行:

The issue appears to be happened at next row of my code where I'm doing the same operation but trying assign the output to variable:

xml_body = etree.tostring(root, pretty_print=True, xml_declaration-True, encoding='UTF-8')

打印 xml_body

print xml_body

输出是空字符串,因此我的应用程序不会向 Apache 返回任何内容.

The output is empty string, thus my application then returns nothing back to Apache.

我的环境信息可能会有所帮助:

My environment info may be helpful:

==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)

它看起来类似于这个错误报告 但是,作者提到它没有根本行不通.还有另一个请求包含类似的问题,但仍未解决.我已经检查出我可能会在 Python cli 中成功播放相同的场景:

It looks similar to this bug report However, the author mentioned it doesn't work at all. There is another one request which contains similar issue, but it still isn't resolved. I've checked out I might successfully play the same scenario in 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

有人遇到过同样的问题吗?我处于死胡同,如果您提供任何帮助、想法或有用的链接,我将不胜感激.

Have somebody come across the same issue ever? I'm in dead end and I'd be appreciated for any help, ideas or helpful links.

推荐答案

有同样的问题.

我的测试文件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)

在 cli 模式下运行时:

When run in cli mode:

python test.py

它返回:

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

但是如果我在 wsgi 模式下运行,它会返回 None:

But if I run in wsgi mode, it returns None:

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

然后接收

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

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

点冻结:

lxml==3.7.2
uWSGI==2.0.14

Lib 版本:

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

问题的核心原因未知,但通过删除包libxml2-dev并重新安装uwsgi解决了问题:

Core reason of issue is unknown, but problem was solved by removing package libxml2-dev and reinstalling uwsgi:

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

所以,不知何故,包 libxml2-dev 与 uwsgi 不兼容.

So, somehow package libxml2-dev is not compatible with uwsgi.

这篇关于Python lxml etree.tostring() 返回在 mod_wsgi 上运行的空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆