python 简单的图像服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2463723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple image server
提问by Joel
I have a bunch of images that I need others to browse via a web browser in pretty much the same way as Apache-Gallery.
我有一堆图像,我需要其他人以与Apache-Gallery几乎相同的方式通过网络浏览器浏览这些图像。
I'd be able to dump all my images in a directory so that users hitting:
我将能够将我的所有图像转储到一个目录中,以便用户点击:
would see small thumbnails and selecting an image would load it full size on a page with options to browse the previous or next image.
会看到小缩略图,选择一个图像会在页面上加载它的全尺寸,并提供浏览上一张或下一张图像的选项。
I'm looking for a non Apache solution, much like the wonderfull Python simple http server, that can be launched anywhere with minimal configuration & fuss e.g.
我正在寻找一个非 Apache 解决方案,就像美妙的 Python 简单 http 服务器一样,它可以以最少的配置和大惊小怪的方式在任何地方启动,例如
python -m SimpleHTTPServer 8000
In fact, the python solution above is pretty much want I want except it doesn't thumbnail the images but just a simple directory listing.
事实上,上面的python 解决方案几乎是我想要的,除了它不缩略图像而只是一个简单的目录列表。
Happy to use an app written in any common language so long as it is self contained and can run on linux on a custom port (and to re-iterate, not an Apache module).
很高兴使用以任何通用语言编写的应用程序,只要它是自包含的并且可以在 linux 上的自定义端口上运行(并重新迭代,而不是 Apache 模块)。
UPDATE
更新
I just found a python script called curatorwhich is simple to run. It generates the required thumbs and static html from any images in the directory you point it at, after which you can use the SimpleHttpServer to vend the results.
我刚刚找到了一个名为curator的 Python 脚本,它很容易运行。它从您指向的目录中的任何图像生成所需的缩略图和静态 html,之后您可以使用 SimpleHttpServer 来提供结果。
采纳答案by Joel
Thanks for the answers and comments. The solution I ended up using was as per my update:
感谢您的回答和评论。我最终使用的解决方案是根据我的更新:
- Run
curator
in the directory containing all my images. This generates thumbs and an index page, as well as pagination to all the full sized images. - Run "
*python -m SimpleHTTPServer 8000*
" in that directory to browse the resultant html generated by curator
curator
在包含我所有图像的目录中运行。这会生成缩略图和索引页,以及所有全尺寸图像的分页。*python -m SimpleHTTPServer 8000*
在该目录中运行“ ”以浏览由 curator 生成的结果 html
So this is a simple two step process that pretty much satisfies my initial requirements.
所以这是一个简单的两步过程,几乎可以满足我的初始要求。
回答by unwitting
This was all quite a long time ago now, but I've just begun imageMe, which aims to fulfil exactly this need. You can set it up with the instructions on the site, but if you just want to serve a basic gallery of the images in and below your current location, this command will run it on port 8000:
这已经是很久以前的事情了,但我刚刚开始使用imageMe,它旨在满足这一需求。您可以按照网站上的说明进行设置,但如果您只想提供当前位置中和下方图像的基本图库,则此命令将在端口 8000 上运行它:
curl https://raw.githubusercontent.com/unwitting/imageme/master/imageme.py | python
Hopefully that helps anyone else coming along to have this question answered!
希望这可以帮助其他人回答这个问题!
回答by Mizipzor
Although it doesnt use the SimpleHTTPServer class, this cgi-bin script shows how to display images in a very simply way. Extend it to fit your needs. Source is here.
虽然它不使用 SimpleHTTPServer 类,但是这个 cgi-bin 脚本展示了如何以非常简单的方式显示图像。扩展它以满足您的需求。来源在这里。
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()