mime types - Python, trying to get file extension via URL -
i'm making image grabber puush service; however, whenever generate random url , attempt verify .png image, error generated. took python language earlier today, i'm new this!
the error generated:
traceback (most recent call last): file "run.py", line 19, in <module> extension = guess_extension(guess_type(url)) file "c:\python33\lib\mimetypes.py", line 320, in guess_extension return _db.guess_extension(type, strict) file "c:\python33\lib\mimetypes.py", line 189, in guess_extension extensions = self.guess_all_extensions(type, strict) file "c:\python33\lib\mimetypes.py", line 168, in guess_all_extensions type = type.lower() attributeerror: 'tuple' object has no attribute 'lower'
the code ran:
#!/usr/bin/env python import sys import urllib mimetypes import guess_type, guess_extension random import choice randoms = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',' j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; downloads = 1; #the number of files want download target = int(sys.argv[1]); while downloads <= target: string = choice(randoms)+choice(randoms)+choice(randoms)+choice(randoms)+choice(randoms) url = 'http://puu.sh/'+string print(str(downloads)+': '+string) #download extension = guess_extension(guess_type(url)) print(extension) #urllib.request('http://puu.sh/'+string, string+'.png') downloads += 1
any ideas on error attempting tell me? thanks.
guess_type() returns tuple (type,encoding)
, whereas guess_extension()
accepts single argument type
.
the line
extension = guess_extension(guess_type(url))
calls guess_type
, passes return value (tuple) guess_extension
. should pass first element of tuple (type
)
extension = guess_extension(guess_type(url)[0])
Comments
Post a Comment