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;}
You can extend
SimpleHTTPServer.SimpleHTTPRequestHandlerand override thedo_GETmethod to replaceself.pathwithsimplehttpwebpage_content.htmlif/is requested.Since
SimpleHTTPServer.SimpleHTTPRequestHandlerextendsBaseHTTPServer.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
pathvariable mentioned in the documentation ofBaseHTTPServer.BaseHTTPRequestHandler. You can find thedo_GET()method mentioned in the documentation ofSimpleHTTPServer.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/