Pass HTML input to python script - python

I have a Python script and I want to create a very simple HTML form with 3 fields (username, password and an ID) and a submit button.
When I click Submit I just want to pass these three parameters into my Python script and run the script.
I tried to do it using CGI. I created a cgi-bin folder and added my test.py file in there. The Python code is the following:
#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
username = form.getvalue('username')
password = form.getvalue('password')
room_id = form.getvalue('room_id')
#More code here...
Then I created an index.html file:
<!DOCTYPE html>
<html>
<body>
<form name="search" action="/[path to file]/test.py" method="get">
Username: <input type="text" name="username"> <br>
Password: <input type="password" name="password"> <br>
Room ID: <input type="text" name="room_id"> <br>
<input type="submit" value="Submit">
</form>
</body>
</html>
Finally, I added a server.py file:
import CGIHTTPServer
CGIHTTPServer.test()
I run the server.py file and go to 0.0.0.0:8000. I gave the input but when I click Submit the python code is printed in the form and it is not executed.
I am looking for the simplest and fastest way to make this HTML form execute the script. It is only for test purposes and I do not want to spend time creating a complex web application.
Is CGI a good idea? If yes, what am I doing wrong?

Related

How to run python function once user inputs into form field

I am an html page with a form to enter your email:
<!DOCTYPE html>
<html>
<body>
<h1>The form novalidate attribute</h1>
<p>The novalidate attribute specifies that the form data should not be validated when submitted.</p>
<form action="/action_page.php" novalidate>
<label for="email">Enter your email:</label>
<input type="email" id="email" name="email" required><br><br>
<input type="submit" value="Submit">
</form>
<p><strong>Note:</strong> The novalidate attribute of the form tag is not supported in Safari 10 (or earlier).</p>
</body>
</html>
Is there a way to run a python script when the user enters his email address and then takes the input and runs it through this def function:
def send_email():
email_address=form_input
print(email_address)
So basically, when a user enters an email in the form, it takes the value and runs it through the send email function. I am new to using python with html so the syntax is confusing me. Any ideas or suggestions as to how to but it in the html file?
No, it is not possible to do it just in html.
If you use .php as backend script, read this php form handling
If you want to use python as backend script, you need to use ajax, e.g. ajax intro. In this case, need to run web server that take ajax request and respond the request

in CGI, how it is possible to use both python code and html code

How can I use both HTML and Python code in same file in CGI as we can do the same thing wiht PHP and HTML.
I have following HTML code in Python file.
#!C:/python36/python.exe
import cgi
print("Content-type:text/html")
print("""
<html>
<head><title>html form</title></head>
<body style='background-color:red;'>
<form method="POST" action="#">
<input type="text" placeholder="username" name="username">
<input type="text" placeholder="password" name="password">
<input type="submit" value="submit" name="send">
</html>""")
now I want use python code in this file to get form data and my python code is as follow:
form = cgi.FieldStorage()
username = form.getvalue("username")
password = form.getvalue("password")
print(username,password)
Now, if submit is clicked, I want Python code to be executed and also I do not want to reference my Python code in form action.

How to Execute Python Code on Server from an HTML Form Button

I want to execute python code when user clicks an HTML Form Button. How is it going to be possible? Is it possible that users are not able to view the python code on the server? The forms input are going to be variables in the python code. I am using Flask framework and python 2.7. Security is not a concern yet.
my route.py file:
from pytube import YouTube
from moviepy.editor import *
import os
app = Flask(__name__)
#app.route("/",methods=['GET','POST'])
def landing():
return render_template("index.html",ytdir=ytdir,ytlink=ytlink)
if __name__ == "__main__":
app.run(debug=True)
The index.html in the Template folder:
<form method="POST" action="/">
<input type="text" class="form-control" name="ytdir" placeholder="Example: C:\Downloads">
<h4>The YouTube Video that you want to download</h4>
<textarea class="form-control" rows="3" name="ytlink" placeholder="Example: https://www.youtube.com/watch?v=HipaBLsGB_Y"></textarea>
</form>
<input class="btn btn-primary" type="submit" value="Download">
basically, I have a python code that download the YouTube video into the local drive by using ytdir and ytlink.
Where should I place the python code so that it is executed when the button is clicked.
I recommend for you to do an "api call"
#app.route("/ytdl", methods=["GET"])
def download_video(link):
# call your download code here
# and return the video as an input stream
# read the flask tutorial on how to do that
In the javascript use axios or something to download the file using jquery
like $button.click(() => axios.get(http://<localhost url>:<port>/ytdl) go and read the doc again how to do ajax call

FieldStorage is empty despite a GET request with fields

I am beginning with python CGI programming, using mod_python in apache2, and am trying to retrieve the GET fields in HTTP requests to a simple .py page.
The code:
#!/usr/bin/python
import cgi
import cgitb; cgitb.enable()
print 'Content-type: text/html\n'
print '''
<html>
<body>
'''
form = cgi.FieldStorage()
l = len(form.keys())
print "<p>%s field(s) set.</p>" % l
print '''
</body>
</html>
'''
The page prints "0 field(s) set." What could be wrong here? So far in my search for an answer I haven't find a parameter in mod_python of apache2 that would block the transmission of GET fields to the CGI script.
cgi.FieldStorage relies on a multipart form being POSTed, not sent via GET. Change the method on your form and make sure you have multipart in the form tags:
<form action="" method="post" multipart>
<input type="file" name="file">
<input type="submit" name="submit" value="Submit">
</form>
When you use the GET method, cgi.FieldStorage just grabs the query string, which will at best give you the name of the file you're trying to submit.

Send Post Request from other domain to GAE

I have write an simple python application on GAE.
class Upload(webapp2.RequestHandler):
def post(self):
self.response.out.write('HelloWorld')
app = webapp2.WSGIApplication(['/upload', Upload)],
debug=True)
And it can receive post request.
But there is something with it.
I write one test page.
<html>
<head></head>
<body>
<form action="http://localhost:8080/upload" method="POST">
<input type="text" name="content"/>
<input type="submit" value="submit local"/>
</form>
<form action="http://wp7-gps-tracker.appspot.com/upload" method="POST">
<input type="text" name="content"/>
<input type="submit" value="submit server"/>
</form>
</body>
</html>
The Result
Run in localhost:
Test with IE:Success!
Test with Chrome:Success!
Upload to GAE:
Test with IE:faild!
Test with Chrome:Success!
what's wrong with my application?
I find the problem!
The GAE was forbidden in china!
And my Chrome is using proxy so it can works!
In your face, Chinese government!
Try change action relative your environment ... GAE have different url for version of application.
action="/upload"
Try only using 1 form in your test page, maybe IE is having problem distinguishing which form to submit

Categories