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.

24 comments:

  1. Thank you for this valuable information. We are in an ‘ always on ‘ world. Connecting, sharing, collaborating. We, at ISD Global, help brands digitell the digital while distinguishing the distinguished and distilling the brand essence. best digital marketing agencies in dubai.

    ReplyDelete
  2. Nice Post. Thank You For Sharing This Information.

    SRI ANNAPOORNESHAWARI ASTROLOGY CENTER.Best Astrologer In dhule

    ReplyDelete
  3. 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
  4. Nice post.Keep sharing. Thanks for sharing.

    DURGAANUGARHA ASTROLOGY.black magic Astrologer In kr-puram

    ReplyDelete
  5. Very nice & good looking posts,Thank you for sharing.
    For sofa renovation visit thesofastore,the provieds best
    Sofa Refurbishing in HAL,Bangalore

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

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

    ReplyDelete
  8. Hii your blog is looking good.Thanks for sharing this information and keep posting.
    For better sofa renovation contact thesofastore gives best
    Best Sofa Repair Services in Austin Town,Bangalore

    ReplyDelete
  9. Good Article you know your article it's useful me. it's really good and helping your article and yes thanks for sharing information
    For sofa repair work contact thesofastore gives
    Best Sofa Repair Services in Near Me

    ReplyDelete
  10. Awesome post, thanks for sharing
    Potato Chips Manufacturers in Kerala, Contact us potato chips manufacturers in Kerala

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

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

    ReplyDelete
  13. Very nice article came to know more about the topic.

    Best Orthodontist in Bangalore, Contact us best orthodontist in bangalore

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

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

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

    ReplyDelete
  17. thanks for sharing nice blog keep posting like this https://mulemasters.in/

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

    ReplyDelete
  19. 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
  20. Nice post. Keep sharing. Thanks for sharing.

    Durgaanugarha astrology center. Best Astrologer In malleswaram


    ReplyDelete