0

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()

2 Answers 2

1
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 ...
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

I added an index.html file to the same directory, in it i wrote "hello" .. still shows me directories...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.