This is using python 2.7.6
I'm trying to serve files from my local, current working directory. I'm attempting to set up a server that runs from this directory, but have not been able to get any of its files to display.
I've been attempting to run the code found here to set up a server that listens for http requests in the current directory, but haven't had any luck getting the files to display.
I've been trying to display the html file images.html
that exists in this directory, but when I run the following script and navigate to 127.0.0.1:8000/images.html in my browser, I just get a blank page. This also occurs when trying to navigate to 127.0.0.1:8000/ to display the directory contents.
import SocketServer
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
print self.path
self.send_response(200)
httpd = SocketServer.TCPServer(("", 8000), MyHandler)
httpd.serve_forever()
The server is running as when I do navigate to 127.0.0.1:8000/images.html I get the expected command line output:
127.0.0.1 - - [29/May/2015 20:03:43] "GET /images.html HTTP/1.1" 200 -
/images.html
However, if I run
python -m SimpleHTTPServer 8000
from the current directory and navigate to 127.0.0.1:8000/images.html, the html file does display as expected.
What am I missing?
You are providing your own handler for the server. When the GET request comes in it invokes your handler to try to serve the request. Your handler, however, does not load any files from disk and does not write them to the socket (can be done with self.wfile.write
). So you are overriding the default behavior of the do_GET
which would be to either send back a directory listing or the file that was requested, and are instead directing it to do nothing (well, print to stdout) when handling those requests.
You might want to look at the example at https://wiki.python.org/moin/BaseHttpServer to see an example that has a handler that tries to actually do things.