How to get data from a web page using GET and POST methods in a Python script on Windows
May 21, 2023
Python programs and scripts can make requests to and receive data from websites and web services using the GET and POST methods (as well as other HTTP methods: PUT, PATCH and DELETE).
But what if you want to pass data from a web page to a Python script?
This can be done in several ways.
How to get data from a web page using the GET and POST methods in a Python script if Python is configured as a CGI module of the Apache web server
See also: How to install Python and PIP on Windows 10. How to set up Python as a web server module
In the web server directory, create a test-python subfolder.
In it, we will create an HTML file with the name test-form.htm and the following content:
<!DOCTYPE html> <head> <title>ZaLinux.ru: An example of running Python on a web server</title> </head> <body> <form action="program.py" method="post"> <label for="name">Name:</label> <input type="text" id="name" name="name"><br><br> <label for="email">Surname:</label> <input type="text" id="surname" name="surname"><br><br> <label for="message">Information:</label><br> <textarea id="info" name="info" rows="4" cols="30"></textarea><br><br> <input type="submit" value="Submit"> </form> </body> </html>
In the same subfolder, create a program.py file with the following content:
#!C:\python\python.exe # Import modules for CGI processing import cgi, cgitb # Create a FieldStorage instance form = cgi.FieldStorage() # Getting data from form fields name = form.getvalue('name') surname = form.getvalue('surname') info = form.getvalue('info') # HTTP header output print ('Content-type: text/html\r\n\r\n') # Output HTML code with received data print ('<html>') print ('<head>') print ('<title>ZaLinux.ru: An example of running Python on a web server</title>') print ('</head>') print ('<body>') print ('<em>Python script reports: </em>', '<br /><br />') print ('<b>Name: </b>', name, '<br />') print ('<b>Surname: </b>', surname, '<br />') print ('<b>Extra information: </b>', info, '<br />') print ('</body>') print ('</html>')
Explanations are given in the comments to the code. To get data from a form, form.getvalue() is used.
Notice the first line with the “#!C:\python\python.exe” shebang – the path to the python.exe file may be different in your case, so edit the line to suit your Python installation.
Open the address http://localhost/test-python/test-form.htm and fill in the form data:
Let's send the data to the server. As you can see, the Python script successfully received the form data sent by the POST method to the web server.
A similar code is used to receive data sent by the GET method – just change the method in the <form action="program.py" method="get"> line:
<!DOCTYPE html> <head> <title>ZaLinux.ru: An example of running Python on a web server</title> </head> <body> <form action="program.py" method="get"> <label for="name">Name:</label> <input type="text" id="name" name="name"><br><br> <label for="email">Surname:</label> <input type="text" id="surname" name="surname"><br><br> <label for="message">Information:</label><br> <textarea id="info" name="info" rows="4" cols="30"></textarea><br><br> <input type="submit" value="Submit"> </form> </body> </html>
Please note that Python scripts for use with the web server CGI module need to be improved: the script must send an HTTP header before displaying the data. If you do not change the Python script, then the program will fail.
Related articles:
- How to install Apache web server with PHP, MySQL and phpMyAdmin on Windows [updated: September 2024] (61.3%)
- Configuring Apache Web Server to Run Perl Programs on Windows (61.3%)
- Apache window appears and immediately disappears (SOLVED) (61.3%)
- PHP not displaying emoji from MySQL/MariaDB database (SOLVED) (60.6%)
- “ERROR 1366 (22007): Incorrect string value” in MySQL/MariaDB (SOLVED) (60.6%)
- Error “net::ERR_CERT_AUTHORITY_INVALID” for self-signed certificate (SOLVED) (RANDOM - 4.9%)