【问题标题】:Unable to view files in a browser with python http server无法使用 python http 服务器在浏览器中查看文件
【发布时间】:2016-10-01 19:04:18
【问题描述】:

我正在使用命令为远程机器上的文件夹创建 python httpserver:

python -m SimpleHTTPServer 9999

但我无法使用它在浏览器中查看文件。只要点击链接,文件就会被下载。有没有办法创建服务器,以便我只能在浏览器中查看文件。

【问题讨论】:

  • 是什么样的文件? PDF、图片、MS Office、...?
  • .sh .config 等扩展,但里面的内容是 ascii。我可以看到 .txt 文件,但我也想看到其他文件
  • 检查两件事:1)您的浏览器可以内联显示这些文件吗? 2) HTTP 响应的Content-Type 标头是什么?
  • 对于.sh文件,内容类型:application/x-sh
  • 你在什么操作系统上运行这个服务器?

标签: python browser httpserver


【解决方案1】:

要让浏览器以内联方式打开文件而不是下载文件,您必须使用适当的 http Content 标头提供文件。

使内容加载到浏览器内而不是作为下载的是标题Content-Disposition: inline

要添加这些标题,您可以将默认的SimpleHTTPRequestHandler 子类化为自定义标题。

这是使用 python 3 完成的方式。如果必须使用 python 2,则必须修改导入以及可能的其他部分。

把它放在一个可执行的脚本文件中,你可以调用 myserver.py 并像这样运行它:./myserver.py 9999

#!/usr/bin/env python3

from http.server import SimpleHTTPRequestHandler, test
import argparse


class InlineHandler(SimpleHTTPRequestHandler):

    def end_headers(self):
        mimetype = self.guess_type(self.path)
        is_file = not self.path.endswith('/')
        # This part adds extra headers for some file types.
        if is_file and mimetype in ['text/plain', 'application/octet-stream']:
            self.send_header('Content-Type', 'text/plain')
            self.send_header('Content-Disposition', 'inline')
        super().end_headers()

# The following is based on the standard library implementation 
# https://github.com/python/cpython/blob/3.6/Lib/http/server.py#L1195
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--bind', '-b', default='', metavar='ADDRESS',
                        help='Specify alternate bind address '
                             '[default: all interfaces]')
    parser.add_argument('port', action='store',
                        default=8000, type=int,
                        nargs='?',
                        help='Specify alternate port [default: 8000]')
    args = parser.parse_args()
    test(InlineHandler, port=args.port, bind=args.bind)

【讨论】:

  • 我在使用 python3 的 http.server 提供没有 .htm/.html 扩展名的 html 内容时遇到了问题。使用上面的方法,我用self.send_header('Content-Type', 'text/html') 替换了self.send_header('Content-Type', 'text/plain'),现在它可以工作了。非常有用的sn-p。
  • 将mhtml文件更改为内容类型multipart/related后无法查看。
【解决方案2】:

foo.sh 这样的服务文件应该可以使用SimpleHTTPServer 正常工作。使用curl 作为客户端,我得到如下 HTTP 响应:

$ curl -v http://localhost:9999/so.sh
* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 9999 (#0)
> GET /so.sh HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:9999
> Accept: */*
> 
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Server: SimpleHTTP/0.6 Python/2.7.6
< Date: Thu, 02 Jun 2016 07:28:57 GMT
< Content-type: text/x-sh
< Content-Length: 11
< Last-Modified: Thu, 02 Jun 2016 07:27:41 GMT
< 
echo "foo"
* Closing connection 0

你看到标题行

Content-type: text/x-sh

这对于文件foo.sh 是正确的。从文件扩展名shtext/x-sh 的映射发生在GNU/Linux 系统上的/etc/mime.types 中。我的浏览器

Chromium 50.0.2661.102

能够内联显示文件。

总结:只要您提供已知文件并且您的浏览器可以内联显示它们,一切都会正常工作。

【讨论】:

  • Content-type: application/x-sh 在我的情况下返回
  • 这可能取决于您的操作系统的配置。您的操作系统是否有文件/etc/mime.types?在我的 Ubuntu 上,这包括从 shtext/x-sh 的映射。 Python 可能会读取此配置以设置 Content-Type 标头。
  • 是的,我找到了这个文件,对应的条目是 application/x-sh sh 对我来说。谢谢
猜你喜欢
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 2017-05-09
  • 2018-01-23
  • 2012-04-10
  • 2011-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多