How to safely get the file extension from a URL?

后端 未结 9 1368
北荒
北荒 2021-02-02 08:40

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:802         


        
9条回答
  •  不要未来只要你来
    2021-02-02 09:09

    To get the content-type you can write a function one like I have written using urllib2. If you need to utilize page content anyway it is likely that you will use urllib2 so no need to import os.

    import urllib2
    
    def getContentType(pageUrl):
        page = urllib2.urlopen(pageUrl)
        pageHeaders = page.headers
        contentType = pageHeaders.getheader('content-type')
        return contentType
    

提交回复
热议问题