Friday, July 15, 2016

python: Simple http server with CGI scripts enabled

If you want to experiment some python code as CGI script to serve by a HTTP server, you can get started by these steps:

  1. Create a cgi-bin directory.
  2. Ready!

No, really, it's that simple! Try these CGI scripts out.

Example 1: cgi-bin/hello.py

#!/usr/bin/env python3

localvars_table = '<table>'
for x in dir():
  localvars_table += '<tr><td>%s</td></tr>' % x
localvars_table += '</table>'

print("Content-type: text/html")
print("")
print("""<html><body>
<p>Hello World! Your custom CGI script is working. Here are your current Python local variables.</p>
%s
<p>NOTE: If you want to write useful CGI script, try the Python 'cgi' module. See cgitest.py script.</p>
</body></html>""" % (localvars_table))

To test and run this, you simply invoke these couple commands:

bash> chmod a+x cgi-bin/hello.py
bash> python3 -m http.server --cgi

You may now test it on your browser with http://localhost:8000/cgi-bin/hello.py. Hit CTRL+C to stop the server.

If you want to do more with fancy CGI scripts, try the Python's cgi module. Here is another example.

Example 2: cgi-bin/cgitest.py

#!/usr/bin/env python3

import cgi
cgi.test()

Again chmod your cgitest.py script and visit http://localhost:8000/cgi-bin/cgitest.py. You will see all the HTTP related data as expected when working with a CGI script. See https://docs.python.org/3/library/cgi.html for more details.

19 comments:

  1. Nice Post. Thank You For Sharing This Information.

    SRI ANNAPOORNESHAWARI ASTROLOGY CENTER.Best Astrologer In dhule

    ReplyDelete
  2. Nicely well-written article. It was an awesome article to read. Complete rich content and fully informative.
    For sofa repair work contact thesofastore gives
    Best Sofa Repair Services in Yemalur,Bangalore

    ReplyDelete
  3. Nice post.Keep sharing. Thanks for sharing.

    DURGAANUGARHA ASTROLOGY.black magic Astrologer In kr-puram

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Nice blog with good content,thanks for sharing.
    For web design & development service contact Multisoft digital technologies, They have
    Web Development in Koramangala

    ReplyDelete
  6. Its a Really good post,thansk for sharing
    The best astrological services contact. Best Astrologer In Lakshmi Devi Nagar

    ReplyDelete
  7. wow..amazing post blog that understand the value of providing a quality resource for free.
    visit here
    Bridal Jewellery for Rent in Basavanagudi

    ReplyDelete
  8. thanks for sharing nice blog keep posting like this if you like more visit it https://mulemasters.in/

    ReplyDelete
  9. Nice article..
    Visit website for best astrological services contact. Black Magic Astrologer in Hougang

    ReplyDelete
  10. Very good article,Thank you
    For Astrological service contact Shri Durga astro center,They gives
    Vashikaran Astrologer in Kolar

    ReplyDelete
  11. Thank you for sharing very useful & informative article.
    For web design & development service contact Multisoft digital technologies, We have Digital Marketing in Channapatna

    ReplyDelete
  12. Such a great post, thanks for posting this kind of useful information
    Visit our website for best astrological services contact. Vashikaran Astrologer in Tumkur

    ReplyDelete
  13. Nice post. Keep sharing. Thanks for sharing.

    Durgaanugarha astrology center. Best Astrologer In malleswaram


    ReplyDelete
  14. Hello!!

    Useful blog. thank you for sharing to us.

    indo farm tractor price

    ReplyDelete

  15. Thanks for sharing nice blog, Keep Posting like this: https://www.fastprepacademy.com/gmat-coaching-in-hyderabad/

    ReplyDelete
  16. Thanks for sharing nice blog.
    Visit my Website: https://www.fastprepacademy.com/gmat-coaching-in-hyderabad/

    ReplyDelete