[英]Encoding on Apache with Python CGI using Zipfile.extract
作为社区的新成员,我直接希望加入一个问题。...当我发现许多帮助和令人困惑的问题和答案后,我现在处于一个甚至无法在Google内找到我们相关问题的帮助的水平。
但是回到主题:
在我要进一步处理的zip文件上传后,我遇到了与正在运行的CGI python脚本有关的编码问题:
def unzip_dir(zipname):
zfile = zipfile.ZipFile(zipdir + zipname)
for name in zfile.namelist():
(dirname, filename) = os.path.split(name)
if filename.lower().endswith('.json'):
if not os.path.exists(unzipdir):
os.makedirs(unzipdir)
#LASTCHANGE - IDENTIFIED PROBLEM WITH UTF 8 and ZIPfiles in APACHE (Not reproduceable in any terminal)
zfile.extract(name, unzipdir)
shutil.move(wdir+'//'+unzipdir+'//'+name, unzipdir)
通过ssh在Unix系统上运行此程序非常正常。 但是,如果我通过CGI调用从Web启动此操作,则会遇到以下错误:
<type 'exceptions.UnicodeEncodeError'>: 'ascii' codec can't encode character u'\xe4' in position 95: ordinal not in range(128)
args = ('ascii', u'analysis/Searchqueries-201650215T114640Z/Suchanf...nfragen/2007-01-01 Januar 2007 bis M\xe4rz 2007.json', 95, 96, 'ordinal not in range(128)')
encoding = 'ascii'
end = 96
message = ''
object = u'analysis/Searchqueries-201650215T114640Z/Suchanf...nfragen/2007-01-01 Januar 2007 bis M\xe4rz 2007.json'
reason = 'ordinal not in range(128)'
start = 95
位于此处:
zfile.extract(name, unzipdir)
据我从以前的报告和问答中了解到,该问题与Apache使用Python处理CGI的方式有关。 所有尝试更改python级别(系统输入和输出信息)上的编码的尝试均未成功。
我还尝试使用各种编码方式在关键行中对名称变量进行编码,但这主要是导致以下事实:同一行再也无法在zip文件中找到相应的条目:
<type 'exceptions.KeyError'>: "There is no item named 'Suchanfragen/Suchanfragen/2007-01-01 Januar 2007 bis M\\x84rz 2007.json' in the archive"
args = (r"There is no item named 'Suchanfragen/Suchanfrage...Januar 2007 bis M\x84rz 2007.json' in the archive",)
message = r"There is no item named 'Suchanfragen/Suchanfrage...Januar 2007 bis M\x84rz 2007.json' in the archive"
我还检查了Web服务器的httpd.conf,但这领先于UTF-8。
有人可以帮我吗? 我认为这不是一个琐碎的问题,但是我的想法受到限制。
更新:我现在开始一些不知道自己在做什么的事情,我担心我会因为愚蠢而使整个系统崩溃。 现在,我尝试在某些虚拟主机区域的httpd.conf文件中添加一个名为PassEnv的值,重启后没有成功。
更新2:我尝试了
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
但说实话...不知道在zip文件的上下文中该怎么做。
似乎我找到了解决实际问题的方法。 到目前为止,我对此表示满意:
我通过通过Shell解压缩文件来避免zipfile方法的编码问题。 我认为我会在此过程中增加风险,但实际上我宁愿在此早期阶段冒险,而不是没有取得任何进展,这使我发疯。
这是更改的方法供您参考:
def unzip_dir_v2(zipname):
if not os.path.exists(unzipdir):
os.makedirs(unzipdir)
syntaxer = 'unzip -o -j -q -d' + wdir + '/' + unzipdir + ' ' + wdir + '/' + zipdir + '' + zipname
p = subprocess.Popen(syntaxer, shell=True)
p.communicate() #now wait
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.