H
H
Hecntv2021-09-12 09:41:55
Python
Hecntv, 2021-09-12 09:41:55

Python code for working with html using CGI does not work, how to do it right?

Good day, please tell me what I'm doing wrong, I tried to write code both html and py in different ways, I substituted an action file with the cgi extension, I read the documentation and it doesn't work, in theory, it should just
transfer data from the input by clicking on the input type: "submit" run the .py code and display the text from the input on the user_name variable, but after clicking the submit button, it just prints the contents of the python file.
Thanks in advance!

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Interactive page</title>
</head>
<body style="margin: 100px; border: 1px solid green; padding: 10px">
    <form method="post" action="cgi-bin/cgi101.py">
        <input style="width: 90%" type="text" name="user_name"
               placeholder="Enter you name!">
        <input type="submit" name="submit">
    </form>
</body>
</html>


import cgi
# import cgitb

# cgitb.enable()

print("Content-type: text/html")

form = cgi.FieldStorage()
user_name = form.getfirst('user_name', 'none')

print(user_name)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alekssamos, 2021-09-12
@alekssamos

I dabbled in this on the free Beget.
In the public_html folder, create a .htaccess file and write to it:

PassengerEnabled On
PassengerPython /usr/bin/python
Ready!
Go to the cgi-bin folder, create, for example, a hello.py file with the following code:
#!/usr/bin/env python2
#-*- coding: utf-8 -*-

print("Content-type: text/html")
print("\n")
print("<h1>Hello world!</h1>")

Let's go and enjoy!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question