python判断网络文件类型测试脚本 发表于 2017-03-14 测试 当时想起土司一个帖子,是通过文件头判断下载文件类型的思路来做一个挖掘姬,所以就想用python写一个,所以做了些测试。 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879import urllib2import structdef typeList(): return { "52617221": "rar", "504B0304": "zip" } def toHex(s): lst = [] for ch in s: hv = hex(ord(ch)).replace('0x', '') if len(hv) == 1: hv = '0'+hv lst.append(hv) return reduce(lambda x,y:x+y, lst)try: response = urllib2.urlopen('http://127.0.0.1:8081/test.lnk.lnk',timeout=5) code = response.getcode() print code #print response.info() print type(response) html = response.read(3) print type(html) print html ty1 =toHex(html).upper() print ty1 if ty1 == "526172": print 'rar' elif ty1 == "504B03": print "zip" else: print "wu" # print bytes2hex(response) except urllib2.URLError,e: pass''' print "Failed to reach the server" #print "The reason:",e.reason elif hasattr(e,"code"): print "The server couldn't fulfill the request" print "Error code:",e.code print "Return content:",e.read()else: pass #其他异常的处理 '''""" ty=typeList() if ty1 in ty: print "ok"""" 其它测试数据12345678910111213141516171819202122232425262728293031JPEG (jpg) FFD8FFPNG (png) 89504E47GIF (gif) 47494638TIFF (tif) 49492A00Windows Bitmap (bmp) 424DCAD (dwg) 41433130Adobe Photoshop (psd) 38425053Rich Text Format (rtf) 7B5C727466XML (xml) 3C3F786D6CHTML (html) 68746D6C3EEmail [thorough only] (eml) 44656C69766572792D646174653AOutlook Express (dbx) CFAD12FEC5FD746FOutlook (pst) 2142444EMS Word/Excel (xls.or.doc) D0CF11E0MS Access (mdb) 5374616E64617264204AWordPerfect (wpd) FF575043Postscript (eps.or.ps) 252150532D41646F6265Adobe Acrobat (pdf) 255044462D312EQuicken (qdf) AC9EBD8FWindows Password (pwl) E3828596ZIP Archive (zip) 504B0304RAR Archive (rar) 52617221Wave (wav) 57415645AVI (avi) 41564920Real Audio (ram) 2E7261FDReal Media (rm) 2E524D46MPEG (mpg) 000001BAMPEG (mpg) 000001B3Quicktime (mov) 6D6F6F76Windows Media (asf) 3026B2758E66CF11MIDI (mid) 4D546864 测试区