Python 如何从cgi脚本调用webbrowser?

Python 如何从cgi脚本调用webbrowser?,python,linux,cgi,webbrowser-control,Python,Linux,Cgi,Webbrowser Control,在使用Firefox和Chrome浏览器的Linux系统上,我的cgi脚本如下:假设它只是一个PDF文件。我还需要在浏览器中显示图像文件。无法跟踪错误 #! /usr/bin/env python ################################################### # File should be searched on server and displayed # in the browser ################################

在使用Firefox和Chrome浏览器的Linux系统上,我的cgi脚本如下:假设它只是一个PDF文件。我还需要在浏览器中显示图像文件。无法跟踪错误

#! /usr/bin/env python
###################################################
# File should be searched on server and displayed
# in the browser
###################################################
import cgi
import cgitb
cgitb.enable()
import StringIO
import sys
import os,glob
import subprocess
import webbrowser

def check_file_extension(display_file):
     input_file = display_file
     nm,file_extension = os.path.splitext(display_file)
     return file_extension

print "Content-type: text/html\n"
responseStr = "<html> %s </html>"
print "<pre>"

form = cgi.FieldStorage()
webbrowser.open_new_tab("file:///home/Documents/postgresql.pdf")       

file_nm = ''
nm =''
path = '/home/Documents'
not_found = 0

if form.has_key("file1"):
     file_nm = form["file1"].value

for f in next(os.walk(path))[2]:
     if str(f) == str(file_nm).strip():
         not_found = 0
         print 'Found file: ' + f
         type_of_file = check_file_extension(f) #TODO: Based on extension, change  content type headers for display
         absolute_path_of_file = os.path.join(path, f)
         file_url = 'file://'+absolute_path_of_file
         print '<a href='+file_url+'>'+ absolute_path_of_file+'</a>'
         try:
            pdf1 = open(absolute_path_of_file,'rb').read()
            print "Content-type: application/pdf\n"
            print pdf1 
            break
         except Exception,e:
            print e 
     else:
         not_found = 1

if not_found == 1:
     print "%s" % str(file_nm) + " not found"

print "%s" % file_nm
print "</pre>"
#/usr/bin/env python
###################################################
#应在服务器上搜索并显示文件
#在浏览器中
###################################################
导入cgi
进口cgitb
cgib.enable()
导入StringIO
导入系统
导入操作系统,全局
导入子流程
导入网络浏览器
def检查文件扩展名(显示文件):
输入文件=显示文件
nm,文件扩展名=os.path.splitext(显示文件)
返回文件扩展名
打印“内容类型:文本/html\n”
responsest=“%s”
打印“”
form=cgi.FieldStorage()
webbrowser.打开新选项卡(“file:///home/Documents/postgresql.pdf")       
文件\u nm=“”
nm=“”
路径='/home/Documents'
未找到=0
如果form.has_key(“file1”):
file_nm=表单[“file1”]。值
对于next(os.walk(path))[2]中的f:
如果str(f)=str(文件\u nm).strip():
未找到=0
打印“找到的文件:”+f
type_of_file=检查_file_扩展名(f)#TODO:根据扩展名,更改要显示的内容类型标题
文件的绝对路径=os.path.join(路径,f)
文件url='file://'+文件的绝对路径
打印“
尝试:
pdf1=打开(文件的绝对路径,'rb')。读取()
打印“内容类型:应用程序/pdf\n”
打印pdf1
打破
除例外情况外,e:
打印e
其他:
未找到=1
如果未找到,则=1:
打印“%s”%str(文件\u nm)+未找到
打印“%s”文件\u nm
打印“”

我编辑代码的方式如下:

#! /usr/bin/env python

import os
import cgi
import cgitb 
cgitb.enable()
import sys

def check_file_extension(display_file):
    input_file = display_file
    nm,file_extension = os.path.splitext(display_file)
    return file_extension

form = cgi.FieldStorage()

type_of_file =''
file_nm = ''
nm =''
path = '/home/shantala/Documents'
not_found = 0

if form.has_key("file1"):
    file_nm = form["file1"].value

for f in next(os.walk(path))[2]:
    if str(f) == str(file_nm).strip():
        not_found = 0
        absolute_path_of_file = os.path.join(path, f)
        type_of_file = check_file_extension(absolute_path_of_file)
        if type_of_file == '.pdf':
            file_read = file(absolute_path_of_file,'rb').read()
            print  'Content-type: application/pdf\n'
            print file_read
        if type_of_file == '.txt':
            file_read = file(absolute_path_of_file,'rb').read()
            print  'Content-type: text/html\n'
            print file_read
        if type_of_file == '.png':
            file_read = file(absolute_path_of_file,'rb').read()
            print  'Content-type: image/png\n'
            print file_read
        if type_of_file == '.pdf':
            file_read = file(absolute_path_of_file,'rb').read()
            print  'Content-type: application/pdf\n'
            print file_read
        if type_of_file == '.JPG':
            file_read = file(absolute_path_of_file,'rb').read()
            print  'Content-type: image/jpg\n'
            print file_read
        if type_of_file == '.bmp':
            file_read = file(absolute_path_of_file,'rb').read()
            print  'Content-type: image/bmp\n'
            print file_read

        break
    else:
        not_found = 1

if not_found == 1:
    pass
    # print "%s" % str(file_nm) + " not found"