【问题标题】:How to customize the page sent by SimpleHTTPServer?如何自定义 SimpleHTTPServer 发送的页面?
【发布时间】:2017-05-28 23:11:58
【问题描述】:

我在我的代码中使用SimpleHTTPServer 类来响应客户端请求(它实际上是用于网络项目的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 正在生成目录列表。如果您要求文件,它将提供文件的内容。你到底想做什么?
  • @JoshLee 非常感谢您的回复。我希望服务器以大于 390 字节的custom page 响应。我不知道是否可以创建任意大小的页面,以便服务器使用它来响应。 (我已经更新了我的帖子以显示服务器发送的默认页面)

标签: python mininet


【解决方案1】:

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

您可以在同一目录中创建 index.html 文件,也可以通过切换到 BaseHTTPRequestHandler 并覆盖 do_GET 自己实现 HTTP 响应。

【讨论】:

    猜你喜欢
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-23
    • 2014-12-02
    • 2021-08-15
    • 1970-01-01
    相关资源
    最近更新 更多