user1047069
user1047069

Reputation: 953

using SimpleHTTPServer and it's result

I'm very new with python, so sorry for the stupid question. i'm trying to set up a http server, so I took the following code from : https://docs.python.org/2/library/simplehttpserver.html put it in a test.py file, and just ran : python test.py When I went to localhost:8000 I saw many directories.. why and why doesn't it show my print "serving at port 8000" ? thanks

import SimpleHTTPServer
import SocketServer

PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()

Upvotes: 0

Views: 373

Answers (2)

mhawke
mhawke

Reputation: 87074

serving at port 8000

will be displayed as output on your terminal, not in an HTML page.

As for SimpleHTTPServer it just serves whatever pages you access using the directory from which it was executed as the root directory. If you do not request a specific page, a listing of files in that directory is shown.

Rather than copying the file from the standard library, you can just run it using the -m Python option:

$ python -m SimpleHTTPServer
Serving HTTP on 0.0.0.0 port 8000 ...

Upvotes: 1

Rob Cherry
Rob Cherry

Reputation: 56

The print in this statement is intended to be used to indicate to the runner of the script in a console that the server was started on "PORT". The directory structure you're seeing when you navigate to http://localhost:8000 is generally what a http server will display when there is no index page. Try creating a index.html in the same directory as your python script with some text in it such as

Hello, world!

And then refresh your web browser.

The do_GET() part of the SimpleHTTPServer documentation speaks about this more.

Upvotes: 0

Related Questions