本文介绍了一个使用SimpleHTTPServer和SocketServer使用python的简单网站,如何仅显示html文件而不显示整个目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我访问localhost:8080时如何只显示simplehttpwebsite_content.html?这样我就看不到我的文件树,只能看到网页.所有这些文件都位于同一目录中.

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

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

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

simplehttpwebsite_style.css

body{background-color:blue;}

推荐答案

如果需要/,则可以扩展SimpleHTTPServer.SimpleHTTPRequestHandler并覆盖do_GET方法,以用simplehttpwebpage_content.html替换self.path.

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

由于 SimpleHTTPServer.SimpleHTTPRequestHandler 扩展了,您可以阅读其文档,以了解可用的方法和实例变量以及如何操作它们.

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.

您可以找到 BaseHTTPServer.BaseHTTPRequestHandler 文档中提到的path变量.您可以找到 SimpleHTTPServer.SimpleHTTPRequestHandler 的文档中提到的do_GET()方法.

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.

这是我的shell的一些输出,显示了运行此程序然后尝试访问http://localhost:8080/

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 -

这篇关于一个使用SimpleHTTPServer和SocketServer使用python的简单网站,如何仅显示html文件而不显示整个目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 05:06