Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Python
Hi, I can run python files from web browser at the address http://localhost/cgi-bin/myfile.py 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:
 
#!usr/bin/python
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
HTTPServer(('localhost',8000), SimpleHTTPRequestHandler).serve_forever()

 
Save these as "activate.py"
I'm a little confused here, it said "run this script" and I wonder if "run" here means run the "activate.py" 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
 

UPDATE
In fact, the script I saved as "activate.py" didn't run successfully, the error said "No module named BaseHTTPServer", How could I fix this?
Thank you!
Posted 13-Sep-12 12:27pm
supernorb2.5K
Edited 14-Sep-12 2:57am
v5

1 solution

Rate this: bad
good
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.
 
See: http://docs.python.org/py3k/library/http.server.html[^]
 
Presumably that function continously listens on the designated port ('localhost', 8000) and handles the requests with SimpleHTTPRequestHandler
 
See: http://docs.python.org/library/simplehttpserver.html[^]
 
Quote:
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:
 
http://docs.python.org/library/cgihttpserver.html[^]
 
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/myfile.py -- don't bother with trying to run a webserver in python as well.
  Permalink  
Comments
supernorb at 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 at 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
0 _Amy 310
1 OriginalGriff 294
2 Nirav Prabtani 268
3 Sergey Alexandrovich Kryukov 211
4 CHill60 150
0 OriginalGriff 7,889
1 Sergey Alexandrovich Kryukov 7,006
2 Maciej Los 4,014
3 Peter Leow 3,708
4 CHill60 2,762


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 14 Sep 2012
Copyright © CodeProject, 1999-2014
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