在python cgi中执行shell命令的问题

时间:2019-03-06 03:27:55

标签: python cgi

python版本:Python 2.6.6 操作系统。 CENTOS 6

我正在使用python脚本(cgi-bin),该脚本使用os.system执行shell命令:

该脚本允许上载KML file并将其存储在临时/tmp/文件夹中,此过程很好。然后,通过使用os.systemogr2ogr => sudo ogr2ogr -f 'ESRI Shapefile' /tmp/output.shp /tmp/poly.kml ,我想将该kml文件转换为shapefile格式。使用python-shell 我的Python脚本是否存在错误??因为output.shp文件夹中未生成/tmp/文件。我验证了该命令即使在/tmp/文件夹中也可以正常工作。 但在cgi中,脚本未显示预期结果:

sudo ogr2ogr -f 'ESRI Shapefile' /tmp/output.shp /tmp/poly.kml

-rw-r--r-- 1 root   root    258 Mar  5 21:10 output.dbf
-rw-r--r-- 1 root   root    143 Mar  5 21:10 output.prj
-rw-r--r-- 1 root   root   1084 Mar  5 21:10 output.shp
-rw-r--r-- 1 root   root    108 Mar  5 21:10 output.shx
-rw-r--r-- 1 apache apache 3780 Mar  5 21:09 poly.kml

El脚本称为save_file.py,密码当然不是真正的密码。通过下一个链接,您可以download the kml file usedhere is the web page在我上传文件的地方。

-rwx---r-x 1 root root 818 Mar 5 21:09 save_file.py

脚本更新-已解决!

#!/usr/bin/python
import cgi, os
import cgitb; cgitb.enable()
import subprocess
form = cgi.FieldStorage()
# Get filename here.
fileitem = form['filename']
# Test if the file was uploaded
if fileitem.filename:
   # strip leading path from file name to avoid
   # directory traversal attacks
   fn = os.path.basename(fileitem.filename)
   open('/tmp/' + fn, 'wb').write(fileitem.file.read())
   message = 'The file "' + fn + '" was uploaded successfully'

else:
   message = 'No file was uploaded'

message2="Now we are converting...." +" " + fn + " " + " to shapefile";  
os.chdir("/tmp/")
subprocess.Popen(["sh","kml2shp.sh","poly.kml"])

print """\
Content-Type: text/html\n
<html>
<body>
   <p>%s</p> <br>
   <p>%s</p>
</body>
</html>
""" % (message,message2,)

bash脚本kml2shp.sh:

#!/bin/bash

ogr2ogr -f 'ESRI Shapefile' output.shp $1

0 个答案:

没有答案