Raw CGI in Python - python

I created a litlle script but I already have an error, I don't know what causes this or how to fix it, it's on an ubuntu vps, the error is located on this site:
http://alfaxtronic.koding.io/python.py
This is the Script:
#!/usr/bin/python
import os
import platform
import cgitb
cgitb.enable()
print "Content-Type: text/html"
print
print "hi"
form = cgi.FieldStorage()

You missed an import. Add this to the import lines at the top:
import cgi

Related

Python CGI redirect returns "End of script output before headers"

I am trying to build a redirect service that will allow me to track clicks within emails I send to foreign websites.
Example of the URL of this script:
https://example.com/cgi-bin/redir.py?rurl=https%3A%2F%2Fwww.example.com%2Fde&utm_content=test
I am using Apache2 on Ubunut20.04 with the cgi module, calling the following redir.py script:
#!/usr/bin/python3
import webbrowser
redirect_url = "https://www.example.com"
webbrowser.open(redirect_url)
Now this results in the following error:
End of script output before headers: redir.py
Adding a header:
print('Content-Type: text/plain')
print('')
print('hello world')
With this "hello world" output I get exactly this, a "hello world" message.
How to redirect if a header is needed?
Everything you print gets returned to the requesting browser. Your python file should look like this:
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
print("Content-Type: text/html\n\n")
print()
print("<meta http-equiv=\"refresh\" content=\"0; url=TARGETURL\" />")

Python cgi script not working on shared hosting

#!/usr/bin/python
print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
from wsgiref.handlers import CGIHandler
from app import app
CGIHandler().run(app)
a=9
b=8
c=a+b
import test
d=test.addd(77,88)
print c
print d
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'
"""The above code is in my cgi-bin folder as sumitup.py which works fine and gives the add part of the code as result in the browser when I remove the below import part."""
from wsgiref.handlers import CGIHandler
from app import app
CGIHandler().run(app)
"""I had created a test.py and tried calling the addd method which is also working fine.
Do I need to add the code of wsgiref.handlers library explicity in the same folder.
Note:- I am trying to deploy flask app on a shared hosting www.techpython.com
Can you help me for this.Thanks in advance."""
CGIHandler handles the CGI request for you.
There is no need to print content-length or something like this.
On top of the CGIHandler you could use something like Werkzeug ( http://werkzeug.pocoo.net/ ).

Error in python script running in xampp windows

I am new to PYTHON and usually code on PHP. This is the first script I am trying to run on Windows XAMPP. I enabled addhandler for .py and trying to run the following script:
#!C:\Python33\python.exe
# enable debugging
import cgitb
cgitb.enable()
print ("Content-Type: text/html;charset=utf-8")
print ("Hello World!")
and I am getting the following error while running the code:
The server encountered an internal error and was unable to complete your request.
Error message:
malformed header from script 'test.py': Bad header: Hello World!
If you think this is a server error, please contact the webmaster.
You should separate headers from body printing additional newline:
#!C:\Python33\python.exe
import cgitb
cgitb.enable()
print("Content-Type: text/html;charset=utf-8")
print() # <----------- addtional newlnie for header/body separation.
print("Hello World!")
For anyone who's still getting an error, try replacing print() by print("\r\n") in the following way:
print("Content-Type: text/html;charset=utf-8")
print("\r\n")
print("Hello World!")
This should work!

avoid user abort python subprocesses

I want the process i initiate through the script to run on webserver even if user closes the page.
It doesn't seem to be working with this:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import cgi,cgitb,subprocess
print "Content-Type: text/plain;charset=utf-8"
print
form = cgi.FieldStorage()
ticker = form['ticker'].value
print subprocess.Popen(['/usr/bin/env/python','options.py',ticker])
Please help! Thanks!
I guess this is wrong:
'/usr/bin/env/python'
it should be usually:
'/usr/bin/env python'
but better use this:
>>> import sys
>>> sys.executable # contains the executable running this python process
'C:\\Python27\\pythonw.exe'
I use to do it like this:
p = subprocess.Popen([sys.executable,'options.py',ticker])

Python/feedparser script won't display on CGI/ character coding

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os
import cgi
import string
import feedparser
count = 0
print "Content-Type: text/html\n\n"
print """<PRE><B>WORK MAINTENANCE/B></PRE>"""
d = feedparser.parse("http://www.hep.hr/ods/rss/radovi.aspx?dp=zagreb")
for opis in d:
try:
print """<B>Place/Time:</B> %s<br>""" % d.entries[count].title
print """<B>Streets:</B> %s<br>""" % d.entries[count].description
print """<B>Published:</B> %s<br>""" % d.entries[count].date
print "<br>"
count+= 1
except:
pass
I have a problem with CGI and paython script. Under the terminal script runs just fine except "IndexError: list index out of range", and I put pass for that. But when I run script through CGI I only get WORK MAINTENANCE line and first line from d.entries[count].title repeated 9 times? So confusing...
Also how can I setup support in feedparser for Croation(balkan) letters; č,ć,š,ž,đ ?
# -- coding: utf-8 -- is not working and I m running Ubuntu server.
Thank you in advance for help.
Regards.
for opis in d:
try:
print """<B>Place/Time:</B> %s<br>""" % d.entries[count].title
You're not using 'opis' in your output.
Try something like this:
for entry in d.entries:
try:
print """<B>Place/Time:</B> %s<br>""" % entry.title
....
Oke had another problem, text that I manualy entered would show on CGI but RSS web pages wouldnt. So you need to encode before you write:
# -*- coding: utf-8 -*-
import sys, os, string
import cgi
import feedparser
import codecs
d = blablablabla
print "Content-Type: text/html; charset=utf-8\n\n"
print
for entry in d.entries:
print """%s""" % entry.title.encode('utf-8')

Categories