如何自定义SimpleHTTPServer发送的页面?

How to customize the page sent by SimpleHTTPServer?

我在我的代码中使用 SimpleHTTPServer class 来响应客户端请求(它实际上是用于网络项目的 mininet python 脚本)。客户端每5秒向服务器发送一个请求10.0.0.1:

server.cmd('python -m SimpleHTTPServer 80 &')

def tcp_thread(client_id):
    for i in range(180):
        client_id.cmd('wget -O - 10.0.0.1')
        time.sleep(5)

使用 Wireshark 进行跟踪时,我注意到服务器发送了一个大小为 390 字节的垃圾页面,如下所示:

Hypertext Transfer Protocol
    HTTP/1.0 200 OK\r\n
        [Expert Info (Chat/Sequence): HTTP/1.0 200 OK\r\n]
        Request Version: HTTP/1.0
        Status Code: 200
        Response Phrase: OK
    Server: SimpleHTTP/0.6 Python/2.7.6\r\n
    Date: Fri, 08 Jul 2016 16:16:47 GMT\r\n
    Content-type: text/html; charset=UTF-8\r\n
    Content-Length: 390\r\n
    \r\n
    [HTTP response 1/1]
    [Time since request: 0.000905000 seconds]
    [Request in frame: 75]
    File Data: 390 bytes

页面内容如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><html>\n
<title>Directory listing for /</title>\n
<body>\n
<h2>Directory listing for /</h2>\n
<hr>\n
<ul>\n
<li><a href="experiment.py">experiment.py</a>\n
<li><a href="experiment1.mn">experiment1.mn</a>\n
<li><a href="experiment1.py">experiment1.py</a>\n
<li><a href="README">README</a>\n
<li><a href="rules.txt">rules.txt</a>\n
</ul>\n
<hr>\n
</body>\n
</html>\n

我的问题是:如何更改页面内容,以便将发送的页面 size 增加到大于 390 字节?我尝试搜索有关自定义页面的信息,但没有一个能清楚地解决这个问题。

谢谢。

SimpleHTTPServer 提供目录列表、文件和 index.html,如文档中所述:https://docs.python.org/2.7/library/simplehttpserver.html

您可以在同一目录中创建 index.html 文件,或者您可以通过切换到 BaseHTTPRequestHandler 并覆盖 do_GET.

自己实现 HTTP 响应