Python cgi script not opening a html file in local directory - python

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?

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.

Related

Any way to run web scripts from html, that browser runs automaticaly on page download?

I'm practicing in parsing web pages with python. So what I do is
ans = requests.get(link)
Then I use re to extract some information from html, that is stored in
ans.content
What I faced is that some sites use scripts, that are automatically executed in a browser, but not when I try to download a page using requests. For example, instead of getting a page with information I get something like
scripts_to_get_info.run()
in html code
Browser is installed on my computer, so as a program that I wrote, this means that, theoretically, I should have a way to run this script and to get information while running python code to parse then.
Is it possible? Any suggestion?
(idea, that this is doable, came from the fact, that when I tried to inspect page in google, I saw real html file without any trashy scripts)

Downloading a file from a html? url with python 3

I've been searching for hours on how to download a file the documentation shows me how to do this; but cygwin is horrible and an annoyance to use and I'm trying to implement this in Python 3 for a program. I've tried to use urllib, requests, wget(in python), httplib and some other. But it only fetched the redirected page (as you would get if you paste the link in the url bar with the properly formatted url.)
Though when I inspect a page and I trigger the download link that has the same address that I tried, it works properly and provide me with a download pop-up. Here is an example page the link is triggered by clicking "Download data"
I don't get how any python package is unable to send the proper get request and that I would need to implement this program in linux only to be able to use 'wget'.
Anyone has a clue on how to properly call the url?
You need to add &submit=Download+Data to the end of your URL to download the data. You can see this with the network tab of inspect element in google chrome. Hope I helped!
I think
from subprocess import call
def download(URL)
CMD = ['curl',url]
call(CMD)
to run this:
download('www.download.com/blah/bah/blah')
if you want to use this from the interpreter:
save as module.py
python -i /path/to/module.py
>>>download('www.download.com/blah/bah/blah')
p.s. if this works i'll prob use this in my shell program
EDIT: my comment:
I tried this and got "malformed url" error
from subprocess import call
def download(FILE,URL):
#FILE = file to save to
#URL - download from here
CMD = ['curl','-o',FILE,URL]
call(CMD)
this is what i do for all system commands from python so its something to do with curl specifically.

Return value from python script via CGI

I work on a raspberry Pi. I did a small html page with 2 buttons calling executables via CGI and doing things. I would like to add a temperature display on the same page.
I have a small python script that reads the temperature of a sensor and I can successfully display it on console or via direct CGI (typing the url, the only thing displayed is the temperature). I would like to see the temperature directly on my html page, and be able to refresh the value by clicking a button.
I can't find any answer that might help me returning a value from the python script and displaying it in a textbox or something. Do you have any track for me to follow ?
You can write out a complete html page with CGI
A simple CGI script would be
temp=100
print("Content-Type: text/html") # HTML is following
print() # blank line, end of headers
print("<!doctype html>")
print("<html><head><title>Example</title></head><body>")
print("<p>Temperature is:",temp)
print("</p>")
peint('<a href="#">Reload<a>')
print("</body></html>")
you can also use javascript in a page to (re)load the value with Ajax - there are examples on the page (only replace the php-script with your python-cgi)

Having trouble getting my http server to recognize and run python scripts

I've created a local address http webserver that will hopefully be able to open both HTML webpages and python scripts that create HTML webpages, but I keep getting a 501 error because I can't figure out how to get the webserver to recognize the code correctly. Another note: I'm coding on Windows, I'm not using Cygwin (I don't know anything about Cygwin, so if it's the recommended method here tips on how to get started with it would be appreciated.)
Anyways, here's what I've got.
I have created a folder in C:\ called server_test, and inside this folder is where I have been putting all my relevant HTML and python files. I've been editing my .py files in PyDev in eclipse (C:\workspace) and then copying the files over and putting them in C:\server_test.
To get my server running, I navigate to \server_test in cmd, open python, then create a webserver using HTTPServer, CGIRequestHandler, the current directory, address 127.0.0.1, and a port of my choosing (usually 9090). Once I've done this, I can go to my web browser and type in the address for one of my HTML pages and it runs perfectly fine. However, I currently have an HTML page meant to call a python script (also located in \server_test) that will create another HTML page, but I can't get it to work.
My HTML code looks like this:
<html>
<title>Debug Page</title>
<body
<h1>This is a test file. </h1>
<form method=POST action="my_code_2.py">
<P><input type=submit>
</form>
</body></html>
And then my 'my_code_2.py' looks like this:
#!C:\Python35-32\python.exe
import cgi
import cgitb; cgitb.enable()
print("Content-type: text/html\r\n\r\n")
print('<html>')
print('<h1>')
print('<title>This is a second test.</title>')
print('</h1>')
print('</body')
print('</html>')
From what I've read about shebang lines, it appears Windows native doesn't support them? So how can I make sure that my computer knows it's supposed to run the code as Python? At the moment, when I press the button on my first HTML page, the page http://127.0.0.1:port/my_code_2.py is merely a white page with my python code printed on it.
Try changing the extension to .cgi on your python file and see if that helps. You'll probably want to use something like bottle.py or django tho if you're running python on a webserver. Bottle is easier to learn but with fewer features.
http://bottlepy.org/docs/dev/index.html
https://www.djangoproject.com/

Python CGI - Script outputs source of generated page

I have a CGI script that I wrote in python to use as the home page of the website I am creating. Everything works properly except when you view the page instead of seeing the page that it outputs you see the source code of the page, why is this? I dont mean that it shows me the source code of the .py file, it shows me all the printed information as if I were looking at a .htm file in notepad.
Add the following before you print anything
print "Content-type: text/html"
Probably your script is not getting executed.
Is your python script executable?
Check whether you have the script under cgi-bin directory.
The default Content Type is text, and if you forgot to send the appropriate header in your CGI file, you will end up with what you are seeing.

Categories