Last change
on this file since 796 was
639,
checked in by aslmd, 13 years ago
|
UTIL PYTHON : Interfacing MCD with python. (Also a toy web server in proto; for the moment, do not use nor modify).
|
-
Property svn:executable set to
*
|
File size:
857 bytes
|
Rev | Line | |
---|
[639] | 1 | #! /usr/bin/env python |
---|
| 2 | |
---|
| 3 | from os import listdir |
---|
| 4 | from random import choice |
---|
| 5 | |
---|
| 6 | ext2conttype = {"jpg": "image/jpeg", |
---|
| 7 | "jpeg": "image/jpeg", |
---|
| 8 | "png": "image/png", |
---|
| 9 | "gif": "image/gif"} |
---|
| 10 | |
---|
| 11 | def content_type(filename): |
---|
| 12 | return ext2conttype[filename[filename.rfind(".")+1:].lower()] |
---|
| 13 | |
---|
| 14 | def isimage(filename): |
---|
| 15 | """true if the filename's extension is in the content-type lookup""" |
---|
| 16 | filename = filename.lower() |
---|
| 17 | return filename[filename.rfind(".")+1:] in ext2conttype |
---|
| 18 | |
---|
| 19 | def random_file(dir): |
---|
| 20 | """returns the filename of a randomly chosen image in dir""" |
---|
| 21 | images = [f for f in listdir(dir) if isimage(f)] |
---|
| 22 | return choice(images) |
---|
| 23 | |
---|
| 24 | if __name__ == "__main__": |
---|
| 25 | dir = "/home/aymeric/Images/wallpapers/" |
---|
| 26 | r = random_file(dir) |
---|
| 27 | print "Content-type: %s\n" % (content_type(r)) |
---|
| 28 | print file(dir+r, "rb").read() |
---|
| 29 | |
---|
Note: See
TracBrowser
for help on using the repository browser.