Python CGI: Difference between revisions

From XPUB & Lens-Based wiki
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
See: https://docs.python.org/3/library/cgi.html
== Sample code ==
== Sample code ==
=== Hello World ===
=== Hello World ===
Line 18: Line 20:
=== Receive some text from a form ===
=== Receive some text from a form ===
<source lang="python">
<source lang="python">
#!/usr/bin/env python
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
#-*- coding:utf-8 -*-


Line 32: Line 34:
if text:
if text:
     print ("""<div style="border: 5px solid purple">""")
     print ("""<div style="border: 5px solid purple">""")
     print text
     print (text)
     print ("""</div>""")
     print ("""</div>""")


Line 42: Line 44:
</source>
</source>


== RTFM ==
== Documentation ==
 
Python has a CGI module that takes care of many things like working with values posted form a form or handling an uploaded file.
Python has a CGI module that takes care of many things like working with values posted form a form or handling an uploaded file.


http://docs.python.org/2/library/cgi.html#module-cgi
https://docs.python.org/3/library/cgi.html


== Python CGI Checklist ==
== Python CGI Checklist ==
Line 54: Line 57:
=== Mini server ===
=== Mini server ===
<source lang="bash">
<source lang="bash">
python -m http.server --cgi 8000
python3 -m http.server --cgi 8000
</source>
</source>


=== Unicode issue with Apache ===
=== Unicode issue with Apache ===
An unfortunate side-effect of python3 getting "smarter" about text encoding is that it depends on the enclosing environment to "do the right thing", that is, pick the right text encoding to use when you print stuff. In Apache unfortunately the default cgi environment has no encoding set, so python defaults back to ASCII (and often makes your cgi silently fail in the browser). The solution is to make sure Apache passes on the "LANG" environment variable from the system to the script. This is achieved by two small steps:


* Add PassEnv LANG line to the end of your /etc/apache2/apache2.conf or .htaccess.
* Add PassEnv LANG to your site settings (sites-available/000-default.conf inside the <VirtualHost> or to the end of your /etc/apache2/apache2.conf or .htaccess).
* Uncomment . /etc/default/locale line in /etc/apache2/envvars.
* Uncomment . /etc/default/locale line in /etc/apache2/envvars.
* Make sure line similar to LANG="en_US.UTF-8" is present in /etc/default/locale.
 
Finally: make sure line similar to LANG="en_US.UTF-8" is present in /etc/default/locale (if it's not, you may need to reconfigure your shell environment to use utf-8).


https://stackoverflow.com/questions/9322410/set-encoding-in-python-3-cgi-scripts#19574801
https://stackoverflow.com/questions/9322410/set-encoding-in-python-3-cgi-scripts#19574801

Latest revision as of 10:49, 9 June 2020

See: https://docs.python.org/3/library/cgi.html

Sample code

Hello World

#!/usr/bin/python
print ("Content-type: text/html;charset=utf-8\n")
print ("Hello world!")

Dump the env!

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import cgi
cgi.print_environ()

Receive some text from a form

#!/usr/bin/env python3
#-*- coding:utf-8 -*-

import cgitb; cgitb.enable()
import cgi

q = cgi.FieldStorage()
text = q.getvalue("text", "")
print ("Content-type: text/html;charset=utf-8\n")

# Print any received text in a purple box
print ("""<h1>Hello</h1>""")
if text:
    print ("""<div style="border: 5px solid purple">""")
    print (text)
    print ("""</div>""")

# Print the form
print ("""<form method="get" action="">
<textarea name="text"></textarea>
<input type="submit" />
</form>""")

Documentation

Python has a CGI module that takes care of many things like working with values posted form a form or handling an uploaded file.

https://docs.python.org/3/library/cgi.html

Python CGI Checklist

Python 3

Mini server

python3 -m http.server --cgi 8000

Unicode issue with Apache

An unfortunate side-effect of python3 getting "smarter" about text encoding is that it depends on the enclosing environment to "do the right thing", that is, pick the right text encoding to use when you print stuff. In Apache unfortunately the default cgi environment has no encoding set, so python defaults back to ASCII (and often makes your cgi silently fail in the browser). The solution is to make sure Apache passes on the "LANG" environment variable from the system to the script. This is achieved by two small steps:

  • Add PassEnv LANG to your site settings (sites-available/000-default.conf inside the <VirtualHost> or to the end of your /etc/apache2/apache2.conf or .htaccess).
  • Uncomment . /etc/default/locale line in /etc/apache2/envvars.

Finally: make sure line similar to LANG="en_US.UTF-8" is present in /etc/default/locale (if it's not, you may need to reconfigure your shell environment to use utf-8).

https://stackoverflow.com/questions/9322410/set-encoding-in-python-3-cgi-scripts#19574801

A simple test CGI...

#!/usr/bin/env python3
import sys
import cgitb; cgitb.enable()

print('Content-Type: text/html; charset=utf-8')
print()
print('<html><body><pre>' + sys.stdout.encoding + '</pre>h€lló wörld<body></html>')