Click here to Skip to main content
13,346,480 members (65,594 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi, I can run python files from web browser at the address http://localhost/cgi-bin/ but I want to run them at the address http://localhost:anyport/ , how can I do?

I read a book on Python and it said that I had to activate the web server to understand python code, using only 3 lines of code, put them in a .py file and run the script and then I can type "http://localhost:anyport/" in the address bar of the web browser and see it works.

I did it but it didn't work, here are the 3 lines of code:

from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
HTTPServer(('localhost',8000), SimpleHTTPRequestHandler).serve_forever()

Save these as ""
I'm a little confused here, it said "run this script" and I wonder if "run" here means run the "" with python.exe??? If so, I did that and after typing "http://localhost:8000", the web browser showed a cannot display page (the address does not exist). Could you please make it clear, they say programming web with python is very easy, but I don't feel its ease, it's even not easier than PHP.

Your help would be highly appreciated! Thanks!
PS: Ah, what about the #!usr/bin/python ? Is it the same on all machine installed with Python? What is it?, Thanks

In fact, the script I saved as "" didn't run successfully, the error said "No module named BaseHTTPServer", How could I fix this?
Thank you!
Posted 13-Sep-12 13:27pm
Updated 14-Sep-12 3:57am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The three lines of code you list are python code.

You run the script using python.exe

The first two lines import some python classes from various modules.

The third line creates a "HTTPServer". It then calls the serve_forever() function of the HTTPServer.


Presumably that function continously listens on the designated port ('localhost', 8000) and handles the requests with SimpleHTTPRequestHandler


This class serves files from the current directory and below, directly mapping the directory structure to HTTP requests.

SimpleHTTPServer doesn't handle CGI files, but just serves static html.

There is a python class that does that:[^]

However, there isn't any reason you should be using any of those to accomplish your stated goal.

You already have a webserver running (Apache?) -- you should be configuring that to serve the pages you want from the directory you want on the port you want. (See the documentation for your web server to figure out how to do that.)

If you are just trying to learn python for programming web pages, then stick with running them from http://localhost/cgi-bin/ -- don't bother with trying to run a webserver in python as well.
supernorb 14-Sep-12 5:05am
Thank you, the way you mentioned about configuring the Apache server to achieve my goal is nice, but I think there's another way that I read from my book (using the 3 lines of code above), I want to make it work and it seems to have something wrong here, I really don't want to include the "cgi-bin" in the path to the resources.
TRK3 14-Sep-12 13:41pm
It really depends on what your actual purpose is.

If you want to just serve python files from a different path, then the right way to do it configure your Apache server.

If what you really want to do is learn python and see all things it can do, then maybe you have a valid reason for running the python server -- but even that is kind of doubtful.

Which is it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.180111.1 | Last Updated 14 Sep 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100