Handling submitted forms with python 3 - html

I'm trying to write a quick HTML form:
<!DOCTYPE html>
<html>
<head>
<title>some title</title>
</head>
<body>
<form method="post" action="filename.cgi">
Deploy Process Name:<br>
<input type="text" name="deploy_process_name"><br>
<input type="submit" name="submitXML" value="Submit XML"/>
</form>
</body>
</html>
and I wrote the following script in python using cgi package to extract the data from the form and handle it:
For example - Getting the deploy_process_name
import cgi
def htmlTop():
print("""Content-type:text/html\n\n
<!DOCTYPE html>
<html lange="en">
<head>
<title>Title</title>
</head>
<body>""")
def htmlTail():
print("""</body>
</html>""")
def getData():
formData = cgi.FieldStorage()
deploy_pro_name= formData.getvalue("deploy_process_name")
return deploy_pro_name
if __name__ == '__main__':
try:
htmlTop()
depl= getData()
print("DeployProcessName= {}".format(depl))
htmlTail()
except:
cgi.print_exception()
When I click submit, it just downloads the filename.cgi.
How do I make it run the following script once hitting submit?

It seems you have incorrectly set up server end.
Please follow whatever you're server side CGI is served with to the letter (be it apache httpd, nginx, lighttpd, or whatever)
Things to focus on:
ownership (user:group) of the scripts folder
permissions of the scripts folder
ownership (user:group) of the script
permissions of the script
you seem to have used default extension, but did you ensure the CGI module is loaded and is using that extension to handle CGI?

Related

Python: How to embed my python script into a webpage

I have a full website with HTML and need to add an interactive piece that I could easily write with python (or even c/c++). What can I can use/do to accomplish this? The script will take user input do some calculations and display the output. I am completely lost on where to start, any help is appreciated!
If you want to embed Python in your HTML page the you will have to use a Python based web server which will have HTML form for data input and execute provided Python code as server side script and return you the desired result.
You could start by using Django.
If you want to build a web application with python back-end, then you need to do with a web framework, like Django or Flask. Flask is more easy to use and understandable for beginning level.
Jinja2 is a popular templating engine for Python. A web templating system combines a template with a certain data source to render dynamic web pages.
Flask code:
app.py
from flask import Flask, render_template
app = Flask(__name__)
#app.route("/")
def home():
return render_template("home.html")
if __name__ == "__main__":
app.run(debug=True)
home.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Flask Tutorial</title>
</head>
<body>
<h1> My First Try Using Flask </h1>
<p> Flask is Fun </p>
</body>
</html>
For Run Your First App:
python3 app.py
One way would be to embed Python into a html file using <py-script> tag.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Hello World!</title>
<!-- linking to PyScript assets -->
<link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
<script defer src="https://pyscript.net/alpha/pyscript.js"></script>
</head>
<body>
<!-- Put Python code inside the the <py-script> tag -->
<py-script>
print("Hello World!")
input("Enter your name:")
</py-script>
</body>
</html>
Another way could be to embed Python script as a child process (eg: in Node.js):
const { spawn } = require('child_process');
var child = spawn("python", ["-c",`
print("Hi")
print(int(input("Enter a number:")))
`]);
child.stdout.pipe(process.stdout);
child.stderr.pipe(process.stderr);
process.stdin.pipe(child.stdin);
child.on('exit', () => process.exit());

I am writing a quick web page using html and am running into syntax error

I can't seem to find the error in the code, I am not very familiar with HTML. I keep getting a syntax error on line one:
File "C:\Users\____\Desktop\Flask_test\index.html", line 1
<html>
^
SyntaxError: invalid syntax
Here is my code:
<html>
<head>
<title>Home page</title>
</head>
<body>
<h1>Home Page!</h1>
<p>Hello!</p>
</body>
</html>
I'm not familiar with Flask but I believe it's requesting the !DOCTYPE at the beginning. Try adding <!DOCTYPE html> before the starting HTML tag.
Try adding <!DOCTYPE html> and improve the code's formatting.
<!DOCTYPE html>
<html>
<head>
<title>Home page</title>
</head>
<body>
<h1>Home Page!</h1>
<p>Hello!</p>
</body>
</html>
So you are getting Internal Server Error (500)
that means the code of python you have written does work but when a request comes to the server an error occurs, or Flask just cant find your index.html file
by default Flask will search for your index.html file in a folder named "templates" or
you can set the directory of your templates(HTML files) explicitly
Anyway I wrote this script for you, try to run it on your local machine
from flask import Flask, render_template
app = Flask(__name__,
static_url_path='',
static_folder='static',
template_folder='myTemplates')
#app.route('/')
def mainRouter():
return render_template("index.html")
You need to create a directory named "myTemplates" and put your index.html file inside it, you can change the name of course inside the script (template_folder='myTemplates') to use another directory.
and if you have any static files, like images or audio files, videos, etc...
you need to create a directory called "static" and put all your static files there or you can change its name in the script (static_folder='static') to whatever you like.
Add !DOCTYPE + HTML LANG.
<!DOCTYPE html>
<html lang="en-US">
<html lang="en-US">
<head>
<meta charset="utf-8" />
<title>Home page</title>
</head>
<body>
<h1>Home Page!</h1>
<p>Hello!</p>
</body>
</html>

404 not found on method POST on form

I got this simple form on a page. I run this on localhost but when I run the form it shall redirect to http://localhost:63342/welcome.html
but instead I get the 404 not found message.
When I mark the url in the browser and hit return the correct page shows. But the redirect doesnt work when using method="POST" to localhost
Any idea how to solve this?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form method="POST" action="welcome.html">
<input type="text" id="test" name="test"/>
<input type="submit" value="test"/>
</form>
</body>
</html>
I think you want to use the GET method instead of the POST method.
Have a look here for more information: http://www.w3schools.com/TAGS/ref_httpmethods.asp

Do not see changes made in HTML file in Eclipse

I have created simple login.html file in Dynamic Web Project + Maven + GlassFish in Eclipse.
!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login Page</title>
<style type="text/css">
form {text-align: center}
</style>
</head>
<body>
<form action="LoginServlet" method="post">
USERNAME:<input type="text" name="name" value=""/><br>
PASSWORD:<input type="password" name="password" value=""/><br>
<input type="submit" value="ENTER"/>
<input type="reset" value="CLEAR"/>
</form>
</body>
</html>
I launch login.html file like Run as -> Run on server and it looks ok.
But when I changed something in html and try to run it on server I did not see my changes.
Try mvn clean, mvn install.
What should I do to see changes in html file when I Run it on Server? (In Preview I see changes)
When you launch your *.html file on server it opens in Eclipse browser. To see your changes you need to click on Green triangle "Go to the selected URL" and page will be updated.

Sending mail using classic asp

I am new for classic asp.
I have written simple code to send mail using classic asp as follows :
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form method="post" action="ASPformEmailResults.asp">
<p><input type="submit" name="submit" value="Submit"/></p>
</form>
</body>
</html>
ASP page:
<%
Set myMail=CreateObject("CDO.Message")
myMail.Subject="Sending email with CDO"
myMail.From="a#gmail.com"
myMail.To="b#gmail.com"
myMail.TextBody="This is a message."
myMail.Send
set myMail=nothing
%>
<html>
<head>
<title>My First ASP Page</title>
</head>
<body bgcolor="white" text="black">
</body>
</html>
but whenever open html page & click on submit button then mail should sent to given id but it displays asp page content.
Please help me to solve this problem.
Thank You.
Based on the information given, it appears that although IIS is installed, the ASP parser itself is either disabled or unmapped.
The following instructions will enable it for Windows 8; other versions of Windows will be similar:
From the Start screen, search for "Turn Windows features on or off"; it will be under 'Settings'.
Expand 'Internet Information Services'
Expand 'World Wide Web Services'
Expand 'Application Development Features'.
Tick 'ASP'.
Click 'OK', and ASP will be enabled within IIS.
If it is installed, it may well be disabled. Load up the IIS Manager (either through the start screen, or by running inetmgr directly from Start, Run, then:
Expand your computer name
Expand 'Sites'
Click on 'Default Web Site'.
Click on 'Handler Mappings'.
Ensure that the mapping ASPClassic is enabled for *.asp; if it isn't, you will need to create it and ensure that its executable is set to %windir%\system32\inetsrv\asp.dll.