最新消息:

python写Excel数据表

python admin 4874浏览 0评论
 1: #encoding:cp936
  2: from xlwt import *
  3: from time import localtime
  4: from httplib import HTTPConnection,responses
  5: from urlparse import urlparse
  6: from urllib import unquote
  7: from thread import start_new_thread
  8: from time import sleep
  9:
 10: def status(url):
 11:     urllist=urlparse(url)
 12:     #print urllist
 13:     server=urllist[1]
 14:     host=server.split(":")
 15:     if urllist[4]!="":
 16:         path=urllist[2]+"?"+urllist[4]
 17:         if len(host)==1:
 18:             host=unquote(host[0])
 19:             port=None
 20:             conn=HTTPConnection(host)
 21:         else:
 22:             port=host[1]
 23:             host=unquote(host[0])
 24:             conn=HTTPConnection(host,port)
 25:         conn.request("GET",path)
 26:         httpcode=conn.getresponse().status
 27:         return httpcode
 28:     else:
 29:         path=urllist[2]
 30:         if len(host)==1:
 31:             host=unquote(host[0])
 32:             port=None
 33:             conn=HTTPConnection(host)
 34:         else:
 35:             port=host[1]
 36:             host=unquote(host[0])
 37:             conn=HTTPConnection(host,port)
 38:         conn.request("GET",path)
 39:         httpcode=conn.getresponse().status
 40:         return httpcode
 41:
 42: def thread1():
 43:     global wb
 44:     global sign1
 45:     ws=wb.add_sheet(u"高危级别网站")
 46:     ws.write(0,0,u"网站")
 47:     ws.write(0,1,u"Google是否认为恶意")
 48:     ws.write(0,2,u"等级")
 49:     ws.write(0,3,u"最后一次检测时间")
 50:     ws.write(0,4,u"验证时间")
 51:     ws.write(0,5,u"状态")
 52:     ws.write(0,6,u"存在恶意链接")
 53:     ws.write(0,7,u"恶意链接有效")
 54:     f=open("1.csv","r")
 55:     i=0
 56:     timex=localtime()
 57:     time_str="%d-%02d-%02d %02d:%02d:%02d" % (timex[0],
 58:                                               timex[1],
 59:                                               timex[2],
 60:                                               timex[3],
 61:                                               timex[4],
 62:                                               timex[5])
 63:     for line in f:
 64:         j=0
 65:         uri=""
 66:         if i!=0:
 67:             for x in line.split(","):
 68:                 if j==0:
 69:                     uri=x
 70:                 ws.write(i,j,unicode(x,"gb2312").encode("gb2312"))
 71:                 j+=1
 72:             ws.write(i,4,time_str)
 73:             try:
 74:                 httpcode=status("http://"+uri)
 75:             except:
 76:                 httpcode=404
 77:                 pass
 78:             print "thread1:%02d" % i,responses[httpcode]
 79:             ws.write(i,5,responses[httpcode])
 80:         else:
 81:             pass
 82:         i+=1
 83:     f.close()
 84:     sign1=1
 85:     print "thread1:Done"
 86:
 87: def thread2():
 88:     global wb
 89:     global sign2
 90:     ws=wb.add_sheet(u"挂马源")
 91:     ws.write(0,0,u"链接")
 92:     ws.write(0,1,u"检测恶意次数")
 93:     ws.write(0,2,u"文件类型")
 94:     ws.write(0,3,u"第一次检测时间")
 95:     ws.write(0,4,u"最后一次检测时间")
 96:     ws.write(0,5,u"验证时间")
 97:     ws.write(0,6,u"状态")
 98:     ws.write(0,7,u"利用漏洞")
 99:     timex=localtime()
100:     time_str="%d-%02d-%02d %02d:%02d:%02d" % (timex[0],
101:                                               timex[1],
102:                                               timex[2],
103:                                               timex[3],
104:                                               timex[4],
105:                                               timex[5])
106:     i=0
107:     f=open("2.csv","r")
108:     for line in f:
109:         j=0
110:         if i!=0:
111:             uri=""
112:             for x in line.split(","):
113:                 if j==0:
114:                     uri=x
115:                 ws.write(i,j,unicode(x,"gb2312").encode("gb2312"))
116:                 j+=1
117:
118:             try:
119:                 httpcode=status(uri)
120:             except:
121:                 httpcode=404
122:             print "thread2:%02d" % i,responses[httpcode]
123:             ws.write(i,5,time_str)
124:             ws.write(i,6,responses[httpcode])
125:         else:
126:             pass
127:         i+=1
128:     f.close()
129:     sign2=1
130:     print "thread2:done"
131:
132: if __name__=="__main__":
133:     sign1=sign2=0
134:     wb=Workbook()
135:     start_new_thread(thread1,())
136:     start_new_thread(thread2,())
137:     while sign1==0 or sign2==0:
138:         sleep(1)
139:     wb.save("result.xls")
140:     print "All done!"
141:

转载请注明:爱开源 » python写Excel数据表

您必须 登录 才能发表评论!