Join Club ShowMeDo to Learn Python!

This series is a part of Club ShowMeDo. Click to learn why you should join our club with a simple monthly subscription.

  • We teach Python and all the cool modules
  • Topics include beginner Python, GUIs, Web and Desktop Apps
  • Long, specially crafted video tutorials just for you
  • Club video tutorials extend our normal Free content
  • Save your time - we've done the research for you
  • Keep your skills up-to-date
  • Learn at your own pace, everything you need is shown to you
  • Over 12 hours of archived material are waiting for you in the club

Name: [011] John Montgomery
Member: 99 months
Authored: 21 videos
Description: I'm a C/C++, Java and Python programmer living in Hove, UK. ...

Using Python's CGIHTTPServer module to run CGI scripts [ID:646] (2/6)

in series: Introduction to Python web-programming: CGI

(Showmedo is undergoing major changes. To report any problems viewing the videos please email us and include browser and OS specifics. Cheers - Kyran.)

In this ShowMeDo I demonstrate how you can use Python's CGIHTTPServer module to run a simple web server that will handle CGI (wikipedia) scripts.

I then proceed to create a simple "Hello World" Python CGI script and show it being run via a web browser.

from CGIHTTPServer import CGIHTTPRequestHandler
from BaseHTTPServer import HTTPServer

server_address=('',8000)
httpd = HTTPServer(server_address, CGIHTTPRequestHandler)
httpd.serve_forever()

Got any questions?

Get answers in the ShowMeDo Learners Google Group.

Video statistics:

  • Video's rank shown in the most popular listing
  • Video plays: 229 (since July 30th)
  • Plays in last week: 0
  • Published: 73 months ago

Thank-yous, questions and comments

If this video tutorial was helpful please take some time to say thank-you to the authors for their hard work. Feel free to ask questions. Let the author know why their video tutorial was useful - what are you learning about? Did the video tutorial save you time? Would you like to see more?

You may also want to see our ShowMeDo Google Group to speak to our active users and authors.

Your email address will not be published.

Show some quick comments >>








All comments excluding tick-boxed quick-comments

Thanks. That helped. I had a bit of trouble understanding some of the spoken words. Perhaps you could intentionally speak more slowly when technical phrases or terms-of-art are being used, instead of speeding up through them.


Woops. I meant to post a comment here and instead posted it on video 1. Any thoughts on why the page won't load in Chrome? I realize this may not be very relevant to the tutorial, no obligation to answer unless you have an easy already known response.


Very simple and to the point video. Just what I was looking for!


Hi John, thanks for the great vids.

I understand the simple python server isn't for "real world" usage, but what if we do want to use python in a production setting? Is there an apache module? How do you set it up?

thanks,

takayuki


@RyanHunter

Well if you wanted to use Python CGI stuff in the "real world" you probably wouldn't run it via the little Python server used before. It'd be ok if you were just using it at home or similar, but it's probably not suitable for wider use. Also in terms of hosting you'd be more likely to be able to run CGI scripts via apache (which helps avoid having to open up different ports etc).

As for security - well this style of web programming provides very little baked-in security. As a very direct way of doing stuff you have to consider the fact that anything that you write could go wrong. So when you read information from the user you have to make sure you've validated it.

In particular you have to be careful when reading/writing files, starting new processes, talking to databases and outputting html to the user. There are plenty of guides out there to web programming security, but key types to investigate are:

* SQL Injection

* Cross-site scripting exploit

* Cross-site request forgery

There are other types to worry about, but these will give you an idea of things to watch out for. They are also not specific to Python and/or CGI programming - all web-programming has to deal with these kinds of exploits.


Wow! I had no idea that Python was so powerful, that I could run a simple web server in basically 5 lines of code. I was able to take a large .html page I had made before, and stick sections of the text in string variables (like strVar = ''' my text '''), and have an entire web page recreated on the fly from the variables. I learnt a lot in basically just 15 minutes, and only 2 videos! Excellent. I can't wait to finish the series, but I have to leave for home now.

Please discuss more on the security risks of this type of easy server. I want to do a lot of work with this type of quick server.


Hi Steve,

you'll need to enter the full URL directly. e.g. http://127.0.0.1:8000/cgi-bin/hello.py

Basically by default the server won't let you list the contents of the /cgi-bin/ directory, so you'll have to go direct to the relevant file.

cheers,

John


Problem in lesson 2 using Win XP. I start the webserver running CGIServer.py. I then input into the browser http://127.0.0.1:8000 and can see the folders in the browser. However, if I click on the cgi-bin folder, I get the following error response:

Error code 403.

Message: CGI script is not a plain file ('/cgi-bin/').

Error code explanation: 403 = Request forbidden -- authorization will not help.

This is the content of hello.py in the folder cgi-bin:

#!C:/Python25/python.exe

print "HTTP/1.0 200 OK"

print "Content-Type: text/html"

print

print "<b>"

print "Hello World!"

print "</b>"

What am I doing wrong???

Steve Rose


cool! works easily on xp. I didn't realize I could run a little cgi server on xp. I don't know its limitations and how fully it simulates serverside situation. But for this type of example, it is easier and less annoying than uploading little py test progs to my website & running them.


Solid video. I would have liked seeing you show us the source code that the browser actually renders.

Lucas


Video published, thanks for contributing to ShowMeDo


Showmedo is a peer-produced video-tutorials and screencasts site for free and open-source software (FOSS)- with the exception of some club videos, the large majority are free to watch and download.

how to help » about » faq »

Educating the Open-source Community With Showmedo

Although as important as the software it supports, education and documentation are relatively neglected in the Open-source world. Coders love to code, and explaining how best to use or improve the software tends to be deferred or even sidelined.

At Showmedo we believe the community can play a vital role here and also say thanks for the tools and software that make our lives easier. If you have a piece of software you love or a programming langugage you are enthusiastic about, why not make a screencast showing others how to use it? All the stuff you wish you'd been told, the tips, tricks, insights that would have saved you time and frustration.

Screencasting is easier than you think, and we're happy to help you. You can emailus for advice or just use some of the how-to screencasts on the site. This screencasting learning-pathis a good place to start.

Kudos and Thanks for John

Content

Feedback

Showmedo's development is fairly rapid and bugs will inevitably creep in. If you have any problems please drop us a line using the contact address below. Likewise, any suggestions for improvements to the site are gratefully received.

feedback@showmedo.com