2

I have a simple python module that generates a web page containing a list of links. This page is automatically opened in a browser through webbrowser.open('file://' + file.name): no problems here.
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 (e.g. in a path like /home/user/web/out/) in a folder where I correctly set all rwx permissions.
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. Then I tried with redirection, printing first a header (print "Content-type: text/html\n\n") and then

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.
It is worth to say that, if I manually open this abc.html page in the browser, it is properly shown.

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. Can somebody point me to the right direction?

Faabiioo
  • 89
  • 1
  • 10
  • I am just guessing but: if you could redirect to local files then everybody hosting a website would be able to read all content from you computer. So maybe you can not redirect to local files from a website. But you can open the local files with your server and serve the content. – User Oct 09 '13 at 10:56
  • @User Uhm... what do you mean by 'serve the content'? I actually managed to write the new web page to be opened in `cgi-bin/out/abc.html`, but still I don't get the script to open it or even redirect to it – Faabiioo Oct 09 '13 at 15:43
  • A CGI-Script can not redirect I remember. `self.send_response(200, "Script output follows")` in `http.server`. – User Oct 09 '13 at 20:41

1 Answers1

0

A CGI-Script can not redirect I remember.

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)

But you can just open the file and use sys.stdout.write or shutil to copy its content to stdout.

User
  • 14,131
  • 2
  • 40
  • 59
  • copy the content of the html file to `stdout` didn't do the job. I want to try creating a web server and separate the two things, but I hoped it could be faster and easier – Faabiioo Oct 11 '13 at 14:16
  • You can still modify the http server. – User Oct 11 '13 at 16:13