Skip to content

Instantly share code, notes, and snippets.

@Yoplitein
Last active June 6, 2017 00:58
Show Gist options
  • Save Yoplitein/b9b7bf4c954788818cec to your computer and use it in GitHub Desktop.
Save Yoplitein/b9b7bf4c954788818cec to your computer and use it in GitHub Desktop.
Python script to generate a list of mimetype -> extension mappings for nginx/lighttpd
#!/usr/bin/env python2
mimeList = ""
mapping = {}
with open("/etc/mime.types", "r") as f:
mimeList = f.read().split("\n")
for entry in mimeList:
entry = entry.split("\t")
while '' in entry:
entry.remove('')
if len(entry) == 2: #entry is ["mime", "extension1 extension2"]
mime, extensions = entry
for extension in extensions.split(" "):
mapping[extension] = mime
print("mimetype.assign = (")
for extension, mime in mapping.iteritems():
print(''' "{}" => "{}",'''.format(extension, mime))
print(")")
#!/usr/bin/env python
mimeList = ""
types = {}
with open("/etc/mime.types", "r") as f:
mimeList = f.read().split("\n")
for entry in mimeList:
entry = entry.split("\t")
while '' in entry:
entry.remove('')
if len(entry) == 2: #entry is ["mime", "extension1 extension2"]
mime, extensions = entry
for extension in extensions.split(" "):
types[mime] = types.get(mime, []) + [extension]
print("types {")
for mime, extensions in types.iteritems():
print(" {} {};".format(mime, " ".join(extensions)))
print("}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment