Skip to main content
 首页 » 编程设计

python之subprocess.Popen 命令(antiword)在 shell 和 web 应用程序中产生不同的输出

2024年08月29日42shangdawei

我让 Django 在标准的 WSGI/Apache httpd 组合上运行。

我注意到在 shell 和浏览器中运行代码时文件输出不同。我已经隔离了其他所有内容,但仍然遇到同样的问题。

代码如下:

def test_antiword(filename): 
    import subprocess 
    with open(filename, 'w') as writefile: 
        subprocess.Popen(["antiword", '/tmp/test.doc'], stdout=writefile) 
    p = subprocess.Popen(["antiword", '/tmp/test.doc'], stdout=subprocess.PIPE) 
    out, _ = p.communicate() 
    ords = [] 
    for kk in out: 
        ords.append(ord(kk)) 
    return out, ords 
 
def test_antiword_view(request): 
    import HttpResponse 
    return HttpResponse(repr(test_antiword('/tmp/web.txt'))) 

当在浏览器中打开 url 时,这是输出:

('\n"I said good day sir. Good day!" shouted Sh\xe9rlo\xe7k H\xf8lme\xa3.\n\n "Why not Zoidberg?" queried Zoidberg.\n', [10, 34, 73, 32, 115, 97, 105, 100, 32, 103, 111, 111, 100, 32, 100, 97, 121, 32, 115, 105, 114, 46, 32, 71, 111, 111, 100, 32, 100, 97, 121, 33, 34, 32, 115, 104, 111, 117, 116, 101, 100, 32, 83, 104, 233, 114, 108, 111, 231, 107, 32, 72, 248, 108, 109, 101, 163, 46, 10, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 34, 87, 104, 121, 32, 110, 111, 116, 32, 90, 111, 105, 100, 98, 101, 114, 103, 63, 34, 32, 113, 117, 101, 114, 105, 101, 100, 32, 90, 111, 105, 100, 98, 101, 114, 103, 46, 10])

这是我在 shell 中调用 test_antiword('/tmp/shell.txt') 时的相应输出:

('\n\xe2\x80\x9cI said good day sir. Good day!\xe2\x80\x9d shouted Sh\xc3\xa9rlo\xc3\xa7k H\xc3\xb8lme\xc2\xa3.\n\n \xe2\x80\x9cWhy not Zoidberg?\xe2\x80\x9d queried Zoidberg.\n', [10, 226, 128, 156, 73, 32, 115, 97, 105, 100, 32, 103, 111, 111, 100, 32, 100, 97, 121, 32, 115, 105, 114, 46, 32, 71, 111, 111, 100, 32, 100, 97, 121, 33, 226, 128, 157, 32, 115, 104, 111, 117, 116, 101, 100, 32, 83, 104, 195, 169, 114, 108, 111, 195, 167, 107, 32, 72, 195, 184, 108, 109, 101, 194, 163, 46, 10, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 226, 128, 156, 87, 104, 121, 32, 110, 111, 116, 32, 90, 111, 105, 100, 98, 101, 114, 103, 63, 226, 128, 157, 32, 113, 117, 101, 114, 105, 101, 100, 32, 90, 111, 105, 100, 98, 101, 114, 103, 46, 10])

如您所见,输出非常不同。一方面,shell 输出保留了原始文件中的空白;它在网络版本中丢失了。

正如您在代码中看到的,我还将文档输出到文件中。生成的输出如下:

web.txt

"I said good day sir. Good day!" shouted Sh?rlo?k H?lme?. 
 
             "Why not Zoidberg?" queried Zoidberg. 

shell.txt

“I said good day sir. Good day!” shouted Shérloçk Hølme£. 
 
             “Why not Zoidberg?” queried Zoidberg. 

网页版无法识别字符,编码被file识别为ISO-8859。在 shell 版本中,字符显示正确,编码被 file 识别为 UTF-8。

我不知道为什么会发生这种情况。我已经检查过,两个进程都使用相同版本的 antiword。此外,我已验证它们都对 subprocess 使用相同的 python 模块文件。两种情况下使用的 Python 版本也完全匹配。

谁能解释一下可能发生了什么?

请您参考如下方法:

差异可能是由于环境变量造成的。根据man page :

Antiword uses the environment variables LC_ALL, LC_CTYPE and LANG (in that order) to get the current locale and uses this information to select the default mapping file.

我怀疑发生的事情是,当您从 shell 运行它时,您的 shell 是 UTF-8 语言环境,但是当您从 Django 运行它时,它是不同的语言环境,并且它无法正确转换Unicode 字符。像这样运行子进程时尝试切换到 UTF-8 语言环境:

new_env = dict(os.environ)  # Copy current environment 
new_env['LANG'] = 'en_US.UTF-8' 
p = subprocess.Popen(..., env=new_env)