Python Forum
Simple URL Example – Get Method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple URL Example – Get Method
#1
Hello. I am learning Python 3 from a .pdf from Tutorialspoint. I am at a stage where I'm learning how to write CGI programs. I copied the following program, which when run should result in
Hello ZARA ALI
Simple URL Example – Get Method
Here is a simple URL, which passes two values to hello_get.py program using GET method.
/cgi-bin/hello_get.py?first_name=Malhar&last_name=Lathkar
Given below is the hello_get.py script to handle the input given by web browser. We are
going to use the cgi module, which makes it very easy to access the passed information -
#!/usr/bin/python3
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
first_name = form.getvalue('first_name')
last_name = form.getvalue('last_name')
print ("Content-type:text/html")
print()
print ("<html>)"
print ("<head>")
print ("<title>Hello - Second CGI Program</title>")
print ("</head>")
print ("<body>")
print ("<h2>Hello %s %s</h2>" % (first_name, last_name))
print ("</body>")
print ("</html>">)
I modified the code, as it said "invalid syntax":

#!/usr/bin/python3
# Import modules for CGI handling
import cgi, cgitb
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
first_name = form.getvalue('first_name')
last_name = form.getvalue('last_name')
print ("Content-type:text/html")
print()
print ("<html>")
print ("<head>")
print ("<title>Hello - Second CGI Program</title>")
print ("</head>")
print ("<body>")
print ("<h2>Hello %s %s</h2>" % (first_name, last_name))
print ("</body>")
print ("</html>")
And the result I'm getting now is:
Content-type:text/html

<html>
<head>
<title>Hello - Second CGI Program</title>
</head>
<body>
<h2>Hello None None</h2>
</body>
</html>
Reply
#2
Why use the cgi module instead of a (small) web framework like Flask, or Bottle?
buran and whois1230 like this post
Reply
#3
I am running the newest version of Python on Linux Ubuntu 18.04 LTS. The IDE I'm using to write Python code is IDLE. I tried to install Flask, and this is what happened:
user@Lenovo-ideapad-110-17IKB:~$ pip install Flask

Command 'pip' not found, but can be installed with:

sudo apt install python-pip

user@Lenovo-ideapad-110-17IKB:~$ sudo apt install python-pip
[sudo] password for user: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-hwe-5.4-headers-5.4.0-42 linux-hwe-5.4-headers-5.4.0-45
  linux-hwe-5.4-headers-5.4.0-47 linux-hwe-5.4-headers-5.4.0-48
  linux-hwe-5.4-headers-5.4.0-51
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
  libpython-all-dev libpython-dev libpython2.7-dev python-all python-all-dev
  python-asn1crypto python-cffi-backend python-crypto python-cryptography
  python-dbus python-dev python-enum34 python-idna python-ipaddress
  python-keyring python-keyrings.alt python-pip-whl python-pkg-resources
  python-secretstorage python-setuptools python-wheel python-xdg python2.7-dev
Suggested packages:
  python-crypto-doc python-cryptography-doc python-cryptography-vectors
  python-dbus-dbg python-dbus-doc python-enum34-doc gir1.2-gnomekeyring-1.0
  python-fs python-gdata python-keyczar python-secretstorage-doc
  python-setuptools-doc
The following NEW packages will be installed:
  libpython-all-dev libpython-dev libpython2.7-dev python-all python-all-dev
  python-asn1crypto python-cffi-backend python-crypto python-cryptography
  python-dbus python-dev python-enum34 python-idna python-ipaddress
  python-keyring python-keyrings.alt python-pip python-pip-whl
  python-pkg-resources python-secretstorage python-setuptools python-wheel
  python-xdg python2.7-dev
0 upgraded, 24 newly installed, 0 to remove and 5 not upgraded.
Need to get 31,8 MB of archives.
After this operation, 51,9 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 http://de.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libpython2.7-dev amd64 2.7.17-1~18.04ubuntu1.2 [28,3 MB]
Get:2 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 libpython-dev amd64 2.7.15~rc1-1 [7.684 B]
Get:3 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 libpython-all-dev amd64 2.7.15~rc1-1 [1.092 B]
Get:4 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 python-all amd64 2.7.15~rc1-1 [1.076 B]
Get:5 http://de.archive.ubuntu.com/ubuntu bionic-updates/main amd64 python2.7-dev amd64 2.7.17-1~18.04ubuntu1.2 [279 kB]
Get:6 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 python-dev amd64 2.7.15~rc1-1 [1.256 B]
Get:7 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 python-all-dev amd64 2.7.15~rc1-1 [1.100 B]
Get:8 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 python-asn1crypto all 0.24.0-1 [72,7 kB]
Get:9 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 python-cffi-backend amd64 1.11.5-1 [63,4 kB]
Get:10 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 python-crypto amd64 2.6.1-8ubuntu2 [244 kB]
Get:11 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 python-enum34 all 1.1.6-2 [34,8 kB]
Get:12 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 python-idna all 2.6-1 [32,4 kB]
Get:13 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 python-ipaddress all 1.0.17-1 [18,2 kB]
Get:14 http://de.archive.ubuntu.com/ubuntu bionic-updates/main amd64 python-cryptography amd64 2.1.4-1ubuntu1.4 [276 kB]
Get:15 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 python-dbus amd64 1.2.6-1 [90,2 kB]
Get:16 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 python-secretstorage all 2.3.1-2 [11,8 kB]
Get:17 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 python-keyring all 10.6.0-1 [30,6 kB]
Get:18 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 python-keyrings.alt all 3.0-1 [16,7 kB]
Get:19 http://de.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 python-pip-whl all 9.0.1-2.3~ubuntu1.18.04.4 [1.653 kB]
Get:20 http://de.archive.ubuntu.com/ubuntu bionic-updates/universe amd64 python-pip all 9.0.1-2.3~ubuntu1.18.04.4 [151 kB]
Get:21 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 python-pkg-resources all 39.0.1-2 [128 kB]
Get:22 http://de.archive.ubuntu.com/ubuntu bionic/main amd64 python-setuptools all 39.0.1-2 [329 kB]
Get:23 http://de.archive.ubuntu.com/ubuntu bionic/universe amd64 python-wheel all 0.30.0-0.2 [36,4 kB]
Get:24 http://de.archive.ubuntu.com/ubuntu bionic/universe amd64 python-xdg all 0.25-4ubuntu1 [31,3 kB]
Fetched 31,8 MB in 5s (5.902 kB/s)     
Selecting previously unselected package libpython2.7-dev:amd64.
(Reading database ... 312843 files and directories currently installed.)
Preparing to unpack .../00-libpython2.7-dev_2.7.17-1~18.04ubuntu1.2_amd64.deb ...
Unpacking libpython2.7-dev:amd64 (2.7.17-1~18.04ubuntu1.2) ...
Selecting previously unselected package libpython-dev:amd64.
Preparing to unpack .../01-libpython-dev_2.7.15~rc1-1_amd64.deb ...
Unpacking libpython-dev:amd64 (2.7.15~rc1-1) ...
Selecting previously unselected package libpython-all-dev:amd64.
Preparing to unpack .../02-libpython-all-dev_2.7.15~rc1-1_amd64.deb ...
Unpacking libpython-all-dev:amd64 (2.7.15~rc1-1) ...
Selecting previously unselected package python-all.
Preparing to unpack .../03-python-all_2.7.15~rc1-1_amd64.deb ...
Unpacking python-all (2.7.15~rc1-1) ...
Selecting previously unselected package python2.7-dev.
Preparing to unpack .../04-python2.7-dev_2.7.17-1~18.04ubuntu1.2_amd64.deb ...
Unpacking python2.7-dev (2.7.17-1~18.04ubuntu1.2) ...
Selecting previously unselected package python-dev.
Preparing to unpack .../05-python-dev_2.7.15~rc1-1_amd64.deb ...
Unpacking python-dev (2.7.15~rc1-1) ...
Selecting previously unselected package python-all-dev.
Preparing to unpack .../06-python-all-dev_2.7.15~rc1-1_amd64.deb ...
Unpacking python-all-dev (2.7.15~rc1-1) ...
Selecting previously unselected package python-asn1crypto.
Preparing to unpack .../07-python-asn1crypto_0.24.0-1_all.deb ...
Unpacking python-asn1crypto (0.24.0-1) ...
Selecting previously unselected package python-cffi-backend.
Preparing to unpack .../08-python-cffi-backend_1.11.5-1_amd64.deb ...
Unpacking python-cffi-backend (1.11.5-1) ...
Selecting previously unselected package python-crypto.
Preparing to unpack .../09-python-crypto_2.6.1-8ubuntu2_amd64.deb ...
Unpacking python-crypto (2.6.1-8ubuntu2) ...
Selecting previously unselected package python-enum34.
Preparing to unpack .../10-python-enum34_1.1.6-2_all.deb ...
Unpacking python-enum34 (1.1.6-2) ...
Selecting previously unselected package python-idna.
Preparing to unpack .../11-python-idna_2.6-1_all.deb ...
Unpacking python-idna (2.6-1) ...
Selecting previously unselected package python-ipaddress.
Preparing to unpack .../12-python-ipaddress_1.0.17-1_all.deb ...
Unpacking python-ipaddress (1.0.17-1) ...
Selecting previously unselected package python-cryptography.
Preparing to unpack .../13-python-cryptography_2.1.4-1ubuntu1.4_amd64.deb ...
Unpacking python-cryptography (2.1.4-1ubuntu1.4) ...
Selecting previously unselected package python-dbus.
Preparing to unpack .../14-python-dbus_1.2.6-1_amd64.deb ...
Unpacking python-dbus (1.2.6-1) ...
Selecting previously unselected package python-secretstorage.
Preparing to unpack .../15-python-secretstorage_2.3.1-2_all.deb ...
Unpacking python-secretstorage (2.3.1-2) ...
Selecting previously unselected package python-keyring.
Preparing to unpack .../16-python-keyring_10.6.0-1_all.deb ...
Unpacking python-keyring (10.6.0-1) ...
Selecting previously unselected package python-keyrings.alt.
Preparing to unpack .../17-python-keyrings.alt_3.0-1_all.deb ...
Unpacking python-keyrings.alt (3.0-1) ...
Selecting previously unselected package python-pip-whl.
Preparing to unpack .../18-python-pip-whl_9.0.1-2.3~ubuntu1.18.04.4_all.deb ...
Unpacking python-pip-whl (9.0.1-2.3~ubuntu1.18.04.4) ...
Selecting previously unselected package python-pip.
Preparing to unpack .../19-python-pip_9.0.1-2.3~ubuntu1.18.04.4_all.deb ...
Unpacking python-pip (9.0.1-2.3~ubuntu1.18.04.4) ...
Selecting previously unselected package python-pkg-resources.
Preparing to unpack .../20-python-pkg-resources_39.0.1-2_all.deb ...
Unpacking python-pkg-resources (39.0.1-2) ...
Selecting previously unselected package python-setuptools.
Preparing to unpack .../21-python-setuptools_39.0.1-2_all.deb ...
Unpacking python-setuptools (39.0.1-2) ...
Selecting previously unselected package python-wheel.
Preparing to unpack .../22-python-wheel_0.30.0-0.2_all.deb ...
Unpacking python-wheel (0.30.0-0.2) ...
Selecting previously unselected package python-xdg.
Preparing to unpack .../23-python-xdg_0.25-4ubuntu1_all.deb ...
Unpacking python-xdg (0.25-4ubuntu1) ...
Setting up python-idna (2.6-1) ...
Setting up python-pip-whl (9.0.1-2.3~ubuntu1.18.04.4) ...
Setting up python-asn1crypto (0.24.0-1) ...
Setting up python-crypto (2.6.1-8ubuntu2) ...
Setting up python-wheel (0.30.0-0.2) ...
Setting up python-pkg-resources (39.0.1-2) ...
Setting up python-keyrings.alt (3.0-1) ...
Setting up python-cffi-backend (1.11.5-1) ...
Setting up python-enum34 (1.1.6-2) ...
Setting up libpython2.7-dev:amd64 (2.7.17-1~18.04ubuntu1.2) ...
Setting up python-dbus (1.2.6-1) ...
Setting up python-ipaddress (1.0.17-1) ...
Setting up python-pip (9.0.1-2.3~ubuntu1.18.04.4) ...
Setting up python2.7-dev (2.7.17-1~18.04ubuntu1.2) ...
Setting up python-all (2.7.15~rc1-1) ...
Setting up python-xdg (0.25-4ubuntu1) ...
Setting up libpython-dev:amd64 (2.7.15~rc1-1) ...
Setting up python-setuptools (39.0.1-2) ...
Setting up python-dev (2.7.15~rc1-1) ...
Setting up libpython-all-dev:amd64 (2.7.15~rc1-1) ...
Setting up python-all-dev (2.7.15~rc1-1) ...
Setting up python-cryptography (2.1.4-1ubuntu1.4) ...
Setting up python-secretstorage (2.3.1-2) ...
Setting up python-keyring (10.6.0-1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
user@Lenovo-ideapad-110-17IKB:~$ pip install Flask
Collecting Flask
  Downloading https://files.pythonhosted.org/packages/f2/28/2a03252dfb9ebf377f40fba6a7841b47083260bf8bd8e737b0c6952df83f/Flask-1.1.2-py2.py3-none-any.whl (94kB)
    100% |████████████████████████████████| 102kB 3.0MB/s 
Collecting click>=5.1 (from Flask)
  Downloading https://files.pythonhosted.org/packages/d2/3d/fa76db83bf75c4f8d338c2fd15c8d33fdd7ad23a9b5e57eb6c5de26b430e/click-7.1.2-py2.py3-none-any.whl (82kB)
    100% |████████████████████████████████| 92kB 5.5MB/s 
Collecting Werkzeug>=0.15 (from Flask)
  Downloading https://files.pythonhosted.org/packages/cc/94/5f7079a0e00bd6863ef8f1da638721e9da21e5bacee597595b318f71d62e/Werkzeug-1.0.1-py2.py3-none-any.whl (298kB)
    100% |████████████████████████████████| 307kB 2.4MB/s 
Collecting itsdangerous>=0.24 (from Flask)
  Downloading https://files.pythonhosted.org/packages/76/ae/44b03b253d6fade317f32c24d100b3b35c2239807046a4c953c7b89fa49e/itsdangerous-1.1.0-py2.py3-none-any.whl
Collecting Jinja2>=2.10.1 (from Flask)
  Downloading https://files.pythonhosted.org/packages/30/9e/f663a2aa66a09d838042ae1a2c5659828bb9b41ea3a6efa20a20fd92b121/Jinja2-2.11.2-py2.py3-none-any.whl (125kB)
    100% |████████████████████████████████| 133kB 4.5MB/s 
Collecting MarkupSafe>=0.23 (from Jinja2>=2.10.1->Flask)
  Downloading https://files.pythonhosted.org/packages/fb/40/f3adb7cf24a8012813c5edb20329eb22d5d8e2a0ecf73d21d6b85865da11/MarkupSafe-1.1.1-cp27-cp27mu-manylinux1_x86_64.whl
Installing collected packages: click, Werkzeug, itsdangerous, MarkupSafe, Jinja2, Flask
Successfully installed Flask-1.1.2 Jinja2-2.11.2 MarkupSafe-1.1.1 Werkzeug-1.0.1 click-7.1.2 itsdangerous-1.1.0
user@Lenovo-ideapad-110-17IKB:~$ pip install -U https://github.com/pallets/flask/archive/master.tar.gz
Collecting https://github.com/pallets/flask/archive/master.tar.gz
  Downloading https://github.com/pallets/flask/archive/master.tar.gz
     \ 798kB 8.7MB/s
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-Q9wHDr-build/setup.py", line 5, in <module>
        with open("src/flask/__init__.py", encoding="utf8") as f:
    TypeError: 'encoding' is an invalid keyword argument for this function
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-Q9wHDr-build/
user@Lenovo-ideapad-110-17IKB:~$ 
Reply
#4
(Nov-18-2020, 06:35 PM)whois1230 Wrote: Linux Ubuntu 18.04
On 18.04 pip and python point to Python 2.7
So use pip3 and python3.
sudo apt install python3-pip
Default Python3 version on 18 is Python 3.5(old now).
If you have installed a newer Python version yourself,you my need to alias to that version.

Look also at pyenv Simple Python Version Management.

Python Wrote:CGI is going to be deprecated in Python 3.8 and removed in 3.10
A little history about WSGI(Why is WSGI necessary?) that has taken over written bye Python community that knew that CGI had no future,so new way was needed.
The modern and much better way to do similar stuff to CGI in Python,is to use eg Flask.
whois1230 likes this post
Reply
#5
Ok, so I installed flask. And then I wrote this in IDLE:
#!/usr/bin/python3
# Import modules for CGI handling
import cgi, cgitb

# Create instance of FieldStorage
form = cgi.FieldStorage()

# Get data from fields
first_name = form.getvalue('first_name')
last_name = form.getvalue('last_name')

print ("Content-type:text/html")
print ()
print ("<html>")
print ("<head>")
print ("<title>Hello - Second CGI Program</title>")
print ("<head>")
print ("<body>")
print ("h2>Hello %s %s</h2>" % (first_name, last_name))
print ("<body>")
print ("</html")
This is the supposed output:
https://ibb.co/J2P7qkT
This is the actual output:
https://ibb.co/gW6rbqp
Reply
#6
You might have installed Flask, but you don't appear to be using it. Did you post the wrong code?
whois1230 likes this post
Reply
#7
How can I use it?
Reply
#8
(Nov-19-2020, 07:17 PM)whois1230 Wrote: How can I use it?
There are lot of tutorials about Flask if you search.
Here a post with some basic setup.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Best Method for simple GUI? PythonAndArduino 4 3,541 Nov-17-2017, 06:22 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020