首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从cgi脚本调用cgi浏览器?

如何从cgi脚本调用cgi浏览器?
EN

Stack Overflow用户
提问于 2014-07-09 03:40:48
回答 1查看 193关注 0票数 0

在具有Firefox和Chrome浏览器的Linux系统上,我的cgi脚本如下所示:假设它只是一个PDF文件。我还需要在浏览器中显示图像文件。无法追踪窃听器。

代码语言:javascript
复制
#! /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>"
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-09 09:54:11

我编辑了我的代码以这样的方式工作:

代码语言:javascript
复制
#! /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"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24645018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档