简体   繁体   English

Python CGI脚本未在本地目录中打开html文件

[英]Python cgi script not opening a html file in local directory

I have a simple python module that generates a web page containing a list of links. 我有一个简单的python模块,该模块生成一个包含链接列表的网页。 This page is automatically opened in a browser through webbrowser.open('file://' + file.name) : no problems here. 通过webbrowser.open('file://' + file.name)在浏览器中自动打开此页面:此处没有问题。
From this web page, when clicking on on a link (which has a href like http://localhost/cgi-bin/script.py?param1=Something ) it lunches a cgi script that performs some operations with the passed value and at the end generates a new web page, stored locally in my machine (eg in a path like /home/user/web/out/ ) in a folder where I correctly set all rwx permissions. 在此网页上,单击链接(其href类似于http://localhost/cgi-bin/script.py?param1=Something )时,它会午餐一个cgi脚本,该脚本使用传递的值执行一些操作,并在最后生成一个新的网页,该网页存储在我的计算机本地(例如,类似/home/user/web/out/的路径中)的一个文件夹中,在该文件夹中我正确设置了所有rwx权限。
Well, I've been trying to automatically open this new page in the browser for two days now, attempting with all the solutions I found searching through documentations and forums. 好吧,我一直在尝试在浏览器中自动打开新页面两天,尝试使用在文档和论坛中搜索到的所有解决方案。 I tried with webbrowser.open() again, but then I realized that I can't use it because from the web server I can't open a new browser window itself. 我再次尝试使用webbrowser.open() ,但是后来我意识到我无法使用它,因为从Web服务器本身无法打开新的浏览器窗口。 Then I tried with redirection, printing first a header ( print "Content-type: text/html\\n\\n" ) and then 然后我尝试了重定向,首先打印了一个标题( print "Content-type: text/html\\n\\n" ),然后

print "Status: 302 Moved"
print "Location: file:///home/user/web/out/abc.html\n\n"`

but this just shows a plain blank page. 但这只是显示一个空白页。 I gave a try to other redirecting solutions, like 我尝试了其他重定向解决方案,例如

print "<meta http-equiv='refresh' content='0' url='file:///home/user/web/out/abc.html'>"
print "<script type="text/javascript">location.href='file:///home/user/web/out/abc.html';</script>"
print "<script type="text/javascript">windows.open('file:///home/user/web/out/abc.html');</script>"

I even tried inserting a Button with a POST method that triggers the opening of the new page, without success: I keep getting a plain blank page. 我什至尝试用POST方法插入一个Button来触发新页面的打开,但没有成功:我一直得到一个普通的空白页面。
It is worth to say that, if I manually open this abc.html page in the browser, it is properly shown. 值得一提的是,如果我在浏览器中手动打开此abc.html页面,则可以正确显示该页面。

I think this has to do with the fact that the html page I'm opening from the web server is in locally stored, but I don't know how to solve this. 我认为这与我从Web服务器打开的html页面位于本地存储这一事实有关,但是我不知道如何解决该问题。 Can somebody point me to the right direction? 有人可以指出我正确的方向吗?

A CGI-Script can not redirect I remember. 我记得一个CGI脚本不能重定向。

Note that status code 200 is sent prior to execution of a CGI script, so
scripts cannot send other status codes such as 302 (redirect).

This is some code: 这是一些代码:

self.send_response(200, "Script output follows")

module: http.server (Python 3) 模组:http.server(Python 3)

But you can just open the file and use sys.stdout.write or shutil to copy its content to stdout. 但是您可以打开文件,然后使用sys.stdout.writeshutil将其内容复制到stdout。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM