python判断网络文件类型测试脚本

测试

       当时想起土司一个帖子,是通过文件头判断下载文件类型的思路来做一个挖掘姬,所以就想用python写一个,所以做了些测试。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import urllib2
import struct
def 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"
"""

其它测试数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
JPEG (jpg) FFD8FF
PNG (png) 89504E47
GIF (gif) 47494638
TIFF (tif) 49492A00
Windows Bitmap (bmp) 424D
CAD (dwg) 41433130
Adobe Photoshop (psd) 38425053
Rich Text Format (rtf) 7B5C727466
XML (xml) 3C3F786D6C
HTML (html) 68746D6C3E
Email [thorough only] (eml) 44656C69766572792D646174653A
Outlook Express (dbx) CFAD12FEC5FD746F
Outlook (pst) 2142444E
MS Word/Excel (xls.or.doc) D0CF11E0
MS Access (mdb) 5374616E64617264204A
WordPerfect (wpd) FF575043
Postscript (eps.or.ps) 252150532D41646F6265
Adobe Acrobat (pdf) 255044462D312E
Quicken (qdf) AC9EBD8F
Windows Password (pwl) E3828596
ZIP Archive (zip) 504B0304
RAR Archive (rar) 52617221
Wave (wav) 57415645
AVI (avi) 41564920
Real Audio (ram) 2E7261FD
Real Media (rm) 2E524D46
MPEG (mpg) 000001BA
MPEG (mpg) 000001B3
Quicktime (mov) 6D6F6F76
Windows Media (asf) 3026B2758E66CF11
MIDI (mid) 4D546864

测试区