Loading...
X

How to get data from a web page using GET and POST methods in a Python script

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 web server CGI module

See also: How to install Python as a CGI module in Apache on Linux

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:

#!/usr/bin/python3

# 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.

This file must have permissions to execute code:

sudo chmod +x /srv/http/test-python/program.py

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.

Useful links:

How to get data from a web page using GET and POST methods in a Python script if Python is not configured as a web server CGI module

In this part, I will describe another way to pass data from web pages using the GET and POST methods to a Python program. This solution has its drawbacks: PHP is required. But at the same time, this method also has advantages:

  • you do not need to set up Python as an Apache web server module. It is enough that Python is installed on the operating system
  • no modification of Python scripts is required to send HTTP headers

Brief description of the algorithm:

  • the PHP script receives data from the user through a web page using the GET or POST methods. A website using a web form sends data to the server – a trivial task
  • the same PHP script, using shell_exec, runs a Python script, passing the data received by the GET or POST method as command line arguments
  • and again the same PHP script waits for the completion of the Python program, receives data from it and displays to the user

In the web server directory, create a test-python subfolder.

In it, we will create an HTML file with the name index.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="python-connector.php" 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 python-connector.php file with the following content:

<?php

echo shell_exec ('python /srv/http/test-python/script.py ' . $_POST["name"] . ' ' . $_POST["surname"] . ' '. $_POST["info"]);

This file will accept input from the web form and run the Python script.

We will also create the script.py file itself with the following content:

import sys

name = sys.argv[1]
surname = sys.argv[2]
if len(sys.argv) < 4:
    info = ''
else:
    info = sys.argv[3]

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>')

The script.py file does NOT need to be executable. The script.py file is NOT required to output HTTP headers. Also, it is not necessary to specify a shebang in the script.py file.

Also, this method does not use the cgi and cgitb modules, but uses the sys module, which is needed to receive command line arguments.

Also note that this implementation hides the fact that the server is using a Python script.

Open the address http://localhost/test-python/ and fill in the form data:

Let's send the data to the server. As you can see, the PHP script successfully received the data from the form, sent by the POST method to the web server, then the script.py program was launched, in which the received data was sent as command line arguments. After script.py finished running, the output of this program was first sent into a PHP script and eventually displayed to the user in a web browser.

A similar code is used to receive data sent by the GET method – it is enough to change the method in the line in the HTML code of the web form:

<form action="program.py" method="get">

Leave Your Observation

Your email address will not be published. Required fields are marked *