Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to safely get the file extension from a URL?

Tags:

python

file

Consider the following URLs

 http://m3u.com/tunein.m3u http://asxsomeurl.com/listen.asx:8024 http://www.plssomeotherurl.com/station.pls?id=111 http://22.198.133.16:8024 

Whats the proper way to determine the file extensions (.m3u/.asx/.pls)? Obviously the last one doesn't have a file extension.

EDIT: I forgot to mention that m3u/asx/pls are playlists (textfiles) for audio streams and must be parsed differently. The goal determine the extension and then send the url to the proper parsing-function. E.g.

 url = argv[1] ext = GetExtension(url) if ext == "pls":   realurl = ParsePLS(url) elif ext == "asx":   realurl = ParseASX(url) (etc.) else:   realurl = url Play(realurl) 
GetExtension() should return the file extension (if any), preferrably without connecting to the URL.
like image 710
Fredrik Avatar asked Jan 23 '11 22:01

Fredrik


People also ask

Is there any way to get the file extension from a URL?

String extension = uri. substring(url. lastIndexOf(".") + 1);

Is URL a file extension?

Derived from the standard Uniform Resource Locator abbreviation (URL), the . url filename extension stands for the Internet Shortcut (. url) file type. The Internet Shortcut feature is part of all Microsoft Windows operating systems, allowing to create quick Internet links on the desktop or in any folder.

What is URL path extension?

Purpose: Identifies requests by whether the requested URL points to an asset with a file extension that matches a literal. The only exception is % which is used for URL encoding.


1 Answers

Use urlparse to parse the path out of the URL, then os.path.splitext to get the extension.

import urlparse, os  url = 'http://www.plssomeotherurl.com/station.pls?id=111' path = urlparse.urlparse(url).path ext = os.path.splitext(path)[1] 

Note that the extension may not be a reliable indicator of the type of the file. The HTTP Content-Type header may be better.

like image 85
payne Avatar answered Sep 29 '22 19:09

payne