11

How do I only display simplehttpwebsite_content.html when I visit localhost:8080? So that I can't see my filetree, only the webpage. All these files are in the same directory btw.

simplehttpwebsite.py

#!/usr/bin/env python
import SimpleHTTPServer
import SocketServer

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
server = SocketServer.TCPServer(('0.0.0.0', 8080), Handler)

server.serve_forever()

simplehttpwebsite_content.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html>
  <head>
    <link rel="stylesheet" type="text/css" href="simplehttpwebsite_style.css">
  </head>

  <body>
    This is my first web page
  </body>
</html>

simplehttpwebsite_style.css

body{background-color:blue;}

3 Answers 3

23

You can extend SimpleHTTPServer.SimpleHTTPRequestHandler and override the do_GET method to replace self.path with simplehttpwebpage_content.html if / is requested.

#!/usr/bin/env python
import SimpleHTTPServer
import SocketServer

class MyRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):
        if self.path == '/':
            self.path = '/simplehttpwebpage_content.html'
        return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

Handler = MyRequestHandler
server = SocketServer.TCPServer(('0.0.0.0', 8080), Handler)

server.serve_forever()

Since SimpleHTTPServer.SimpleHTTPRequestHandler extends BaseHTTPServer.BaseHTTPRequestHandler, you can read their documentations to figure what methods and instance variables are available and how you can manipulate them.

You can find the path variable mentioned in the documentation of BaseHTTPServer.BaseHTTPRequestHandler. You can find the do_GET() method mentioned in the documentation of SimpleHTTPServer.SimpleHTTPRequestHandler.

Here is some output from my shell to show what happens when I run this program and then I try to access http://localhost:8080/

susam@swift:~/so$ ls
simplehttpwebpage_content.html  simplehttpwebpage.py  simplehttpwebsite_style.css
susam@swift:~/so$ python simplehttpwebpage.py
swift - - [19/Apr/2012 09:10:23] "GET / HTTP/1.1" 200 -
swift - - [19/Apr/2012 09:10:26] "GET /simplehttpwebsite_style.css HTTP/1.1" 200 -
Sign up to request clarification or add additional context in comments.

3 Comments

Your return is not inside the function. Even when I place the return statement inside the function(method) I get a 404 Error.
Sorry Bentley4, that was a typo. I've fixed it now. Do you see any errors in the console where you are running python simplehttpwebpage.py? Note that simplehttpwebpage_content.html needs to be in the same directory in which you are running this script.
I wrote simplewebsite_content.html instead of simplewebpage_content.html. : ) . Works now.
12

you should call your file index.html, that's the page that gets served automatically instead of listing the directory.

the other possibility would be to override the handlers list_directory(self, path) method.

1 Comment

I don't understand why someone downvoted your answer. Your answer is useful.
0

Building on Susam Pal's answer, here is my implementation which allows the port to be set (just like when you run python -m SimpleHTTPServer 8080) and also serves up html pages when the file exists on the file server, without the .html extension.

#!/usr/bin/env python
import SimpleHTTPServer
import SocketServer
import os.path
import sys

class MyRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def do_GET(self):            
        possible_name = self.path.strip("/")+'.html'
        if self.path == '/':
            # default routing, instead of "index.html"
            self.path = '/simplehttpwebpage_content.html'
        elif os.path.isfile(possible_name):
            # extensionless page serving
            self.path = possible_name

        return SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)

Handler = MyRequestHandler

port = 8000
if len(sys.argv) > 1:
    try:
        p = int(sys.argv[1])
        port = p
    except ValueError:
        print "port value provided must be an integer"

print "serving on port {0}".format(port)
server = SocketServer.TCPServer(('0.0.0.0', port), Handler)
server.serve_forever()

Comments

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.