简单图像服务器

时间:2010-03-17 15:50:56

标签: python http image

我有一堆图像,我需要其他人通过网络浏览器浏览,其方式与Apache-Gallery几乎相同。

我可以将所有图像转储到目录中,以便用户点击:

http://server:port/directory

会看到小缩略图并选择图像会在页面上加载完整尺寸,并可选择浏览上一张或下一张图像。

我正在寻找一种非Apache解决方案,就像精彩的Python简单http服务器一样,只需最少的配置就可以在任何地方启动。大惊小怪,例如

python -m SimpleHTTPServer 8000

事实上,上面的python解决方案非常想要我想要的,除了它不缩略图像,只是一个简单的目录列表。

很高兴使用以任何通用语言编写的应用程序,只要它是自包含的,并且可以在自定义端口上运行linux(并重新迭代,而不是Apache模块)。

更新

我刚刚发现了一个名为 curator 的python脚本,它很容易运行。它会从您指向的目录中的任何图像生成所需的拇指和静态html,之后您可以使用SimpleHttpServer来提供结果。

3 个答案:

答案 0 :(得分:12)

这已经很久以前了,但我刚开始imageMe,其目的是满足这一需求。您可以使用网站上的说明进行设置,但如果您只想在当前位置内和下方提供图像的基本库,则此命令将在端口8000上运行:

curl https://raw.githubusercontent.com/unwitting/imageme/master/imageme.py | python

希望这可以帮助其他任何人来回答这个问题!

答案 1 :(得分:4)

感谢您的回答和评论。我最终使用的解决方案是根据我的更新:

  1. 在包含我所有图片的目录中运行curator。这会生成大拇指和索引页面,以及对所有完整大小的图像进行分页。
  2. 在该目录中运行“ *python -m SimpleHTTPServer 8000* ”以浏览由策展人生成的html
  3. 所以这是一个简单的两步过程,几乎可以满足我的初始要求。

答案 2 :(得分:3)

虽然它不使用Simple HTTP Server类,但这个cgi-bin脚本显示了如何以非常简单的方式显示图像。扩展它以满足您的需求。 Source is here.

from os import listdir
from random import choice

ext2conttype = {"jpg": "image/jpeg",
                "jpeg": "image/jpeg",
                "png": "image/png",
                "gif": "image/gif"}

def content_type(filename):
    return ext2conttype[filename[filename.rfind(".")+1:].lower()]

def isimage(filename):
    """true if the filename's extension is in the content-type lookup"""
    filename = filename.lower()
    return filename[filename.rfind(".")+1:] in ext2conttype

def random_file(dir):
    """returns the filename of a randomly chosen image in dir"""
    images = [f for f in listdir(dir) if isimage(f)]
    return choice(images)

if __name__ == "__main__":
    dir = "c:\\python\\random_img\\"
    r = random_file(dir)
    print "Content-type: %s\n" % (content_type(r))
    print file(dir+r, "rb").read()