CGI Module - Python - python

I found this CGI Module, its letting me use HTML tags inside a python script.
ive seen some topics in here that shows how to use it, but when im using it it doesnt works.
import cgi
print ("""
<html>
<body>
Hello
</body>
</html>
""")
and this is the output when im running the script:
<html>
<body>
Hello
</body>
</html>
how can i use this properly?
thanks.

If you have your CGI script already hooked up to a web server, you will need to emit the HTTP headers too, e.g.
print("Content-Type: text/html") # HTML is following
print() # blank line, end of headers
print ("""
<html>
<body>
Hello
</body>
</html>
""")
Note that the cgi module is not being used in any way to achieve this; just simple calls to print(). The module is useful when you want to process form data submitted by a client through a HTML form.

Related

(python website) how to let website get output from other python script

follow by discussion Get output of python script from within python script
I create online webpage (.html) run other .py script result, but face model not found error
logically seems the python code is fine, might the setting error or other issue I don't come up myself
printbob.py
#!/usr/bin/env python
import sys
def main(args):
for arg in args:
print(arg)
if __name__ == '__main__':
main(sys.argv)
test_0109_003.html
<html>
<head>
<link rel="stylesheet" href="https://pyscript.net/latest/pyscript.css" />
<script defer src="https://pyscript.net/latest/pyscript.js"></script>
</head>
<body>
<b><p>title test 1.10-test_get_ print </p></b>
<br>
<py-script>
import printbob
printbob.main('arg1 arg2 arg3 arg4'.split(' '))
</py-script>
</body>
</html>
(pic 01) the result website showing
(pic 02) I put .py and .html script in WinSCP, online host system
so how to solve this problem, I locate that there might be the resaon
my winscp ip port is private that public cannot access to private's file, I'm not sure if this the reason, and if so, how to deal with it?

Dynamic source code shows in browser on Windows

I'm running Windows 7, and using Google Chrome as my web browser.
This is my code:
#!/usr/bin/env python
print 'content-type: text/html\n'
print """
<!DOCTYPE html>
<html>
<head>
<title> sample </title>
</head>
<body>
"""
print 2 + 3
print """
</body>
</html>
"""
In Command Prompt, where python results in C:\Python27\python.exe, but according to this post I should use #!/usr/bin/env python instead.
When I try to run this .py code in my browser, the source code appears instead of just 5.
If someone would provide detailed instructions as to how to properly work this, I would be most grateful.
Thanks!
Okay. So of course it is not possible to run python directly in the browser but you can configure a local server to be able to run CGI scripts. Since you are on windows you could use something like a WAMP.
Try following this tutorial
https://wiki.python.org/moin/CgiScripts

Embedding Python Game Into HTML Using Skulpt

I have written a game in Python using the PyGame library that I am trying to embed into an HTML page to allow me to play in a web browser.
I am attempting to do this using the JavaScript library Skulpt. I have attached a test script below that successfully outputs the print statement below.
skulpt.html
<html>
<head>
<script src="assets/skulpt/skulpt.js" type="text/javascript"></script>
</head>
<body>
<textarea id="pythonCode">
print "I am python."
</textarea><br />
<pre id="output"></pre>
<script type="text/javascript">
function outf(text) {
var mypre = document.getElementById("output");
mypre.innerHTML = mypre.innerHTML + text;
}
var code = document.getElementById("pythonCode").value;
Sk.configure({output:outf});
eval(Sk.importMainWithBody("<stdin>",false,code));
</script>
</body>
</html>
Output of skulpt.html:
The issue that I am having is that when I use my game code instead of the simple print statement shown above it produces the error seen below;
I have included all relevant images to my web servers' directory at the correct path. I am unsure of why this error is being produced. Any help would be much appreciated, thanks!
Also, here is the attached Python game code (and a live demo of the error):
http://nicolasward.com/portfolio/skulpt.html
You have a lot of indentation on line 1 -> remember, in python, indentation always matters. Take away all those spaces/tabs on the first line and it should run.

Translating Python print statements to Django

I am in the process of creating a Django web application that reads a URL and outputs selected data from the page. I have written the code in Python that parses the web page and it currently returns the information that I need to display in the Django app as desired.
Before I dive in I just want to confirm what I have researched is correct as I only have a limited time to complete the project.
To summarise my python code, it is in the src folder in a class called "manage.py"
I have created print statements that print the information that I need to display (I did this to ensure it was returning the correct data)
print(variable1)
print("some text" + variable2)
Can I create the Django app code in the same file, "manage.py"? (The project has already been created as a Django app in Eclipse when I started building the project)
Would I build the Django code as I've estimated below if I'm using the variables defined from the Python code above?
<!DOCTYPE>
<html>
<head>
<title>{% block title %}Title of website{% endblock %}</title>
</head>
<body>
<h1>Web page report</h1>
<h2>Summary of web page</h2>
<h3>Title of document</h3>
<p>{{variable1}}</p>
<h3>The file size of the document</h3>
<p>{"Some text" + {variable2}}</p>
</body>
</html>
Django has strict rules about where to place which information. You can not write everything into manage.py. Answering requests from the browser is for example done using view functions.

Javascript with python cgi not giving expected results

In the below Python CGI script, I am using javascript function for changing the picture on click. But apparently it doesn't seems to work. I am following the example from w3 schools js example. So I just want to conform, is it possible to use the javascript inside Python CGI script.
If same example has to be done in Python, what should be the approach.
#!/usr/bin/env python
import cgi
print """print "Content-type: text/html; charset=utf-8
<!DOCTYPE html>
<html>
<body>
<script>
function changeImage()
{
element=document.getElementById('myimage')
if (element.src.match("bulbon"))
{
element.src="../pic_bulboff.gif";
}
else
{
element.src="../pic_bulbon.gif";
}
}
</script>
<img id="myimage" onclick="changeImage()"
src="../pic_bulboff.gif" width="100" height="180">
<p>Click the light bulb to turn on/off the light</p>
</body>
</html>
"""
Yes, you can send javascript inside the HTML via a python CGI scrpit. You have to change the initial part of the string that you are sending to:
print """Content-type: text/html; charset=utf-8\n\n
<!DOCTYPE html>
...
EDIT to add the following comments:
There were two problems in your code:
the 1st, you had to remove the part 'print "', because you were already printing a string;
the 2nd, it is a specification of CGI that it must have "\n\n" separating the header, eg. "content-type: text/html; charset=utf-8\n\n" from the following part eg. "...", (in fact as you had one implicit "\n", it would be enough to add just one more...).

Categories