diff --git a/bd4.py b/bd4.py new file mode 100644 index 0000000..c6dff7b --- /dev/null +++ b/bd4.py @@ -0,0 +1,138 @@ +#coding: utf-8 +#不去重复网页 +#增加搜索完自动调用sqlmap功能 +#改为多线程 +import urllib2 +import string +import urllib +import re +import random +from urlparse import urlparse +import sys,os +from Queue import Queue +import threading + +user_agents = ['Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20130406 Firefox/23.0', \ + 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0', \ + 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533+ \ + (KHTML, like Gecko) Element Browser 5.0', \ + 'IBM WebExplorer /v0.94', 'Galaxy/1.0 [en] (Mac OS X 10.5.6; U; en)', \ + 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)', \ + 'Opera/9.80 (Windows NT 6.0) Presto/2.12.388 Version/12.14', \ + 'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) \ + Version/6.0 Mobile/10A5355d Safari/8536.25', \ + 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) \ + Chrome/28.0.1468.0 Safari/537.36', \ + 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.0; Trident/5.0; TheWorld)'] + +def baidu_search(keyword,pn): + p= {'wd': keyword} + searchurl=("http://www.baidu.com/s?"+urllib.urlencode(p)+"&pn={0}&cl=3&rn=100").format(pn) + print searchurl + res=urllib2.urlopen(searchurl, timeout = 15) + html=res.read() + if len(html) < 200: + print html + print "----------------------------------------------" + if "window.location.href" in html: + searchurl2 = html.split("\'") + res=urllib2.urlopen(searchurl2[1], timeout = 15) + print searchurl2[1] + html = res.read() + if len(html) < 200: + print html + print "==========================" + return html + + +def getbaiduurl(html,Q): + l_htm = html.split('\n') + for tx in l_htm: + if 'href =' in tx: + url=tx.split('\"') + #arr.append(url[1]) + Q.put(url[1]) + +def getRealUrl(urlq, urllist): + while urlq.empty() is not True: + item = urlq.get() + try: + print "Origin link is:"+item + domain=urllib2.Request(item) + r=random.randint(0,11) + domain.add_header('User-agent', user_agents[r]) + domain.add_header('connection','keep-alive') + response=urllib2.urlopen(domain, timeout = 15) + uri=response.geturl() + print "The true link is:"+uri + if 'baidu' not in uri: + urllist.append(uri) + except: + print 'Exception occur!!!' + + +def url_exist(real_url, netloc): + for url_temp in real_url: + if netloc in url_temp: + return 1 + + +def write_url(real_url): + result = open("url.txt","w") + for i in real_url: + result.write(i + '\n') + result.close() + + +def geturl(keyword,Q): + for page in range(10): + pn=page*100+1 + html = baidu_search(keyword,pn)# html type is string + #print type(html) + getbaiduurl(html,Q) + #print arr +def writeQ(url_queue,item): + url_queue.put(item) +def readQ(url_queue,item): + return url_queue.get() + +class MyThread(threading.Thread): + def __init__(self, func, args): + threading.Thread.__init__(self) + self.func = func + self.args = args + #self.name = name + def run(self): + apply(self.func, self.args) + +def main(): + url_queue = Queue(200) + key=raw_input("Please input keyword:") + geturl(key, url_queue) + print 'queue size:',url_queue.qsize() + result = open("url3.txt","w") + urllist = [] + #getRealUrl(url_queue,urllist) + threads = [] + for i in range(10): + t = MyThread(getRealUrl,(url_queue,urllist)) + threads.append(t) + for i in range(10): + threads[i].start() + for i in range(10): + threads[i].join() + for u in urllist: + result.write(u + '\n') + result.close() + #增加自动调用sqlmap扫描功能 + #php脚本 + ''' + print 'key is : ',key + if 'php' in key: + os.system("python D:\share\web\sqlmap\sqlmap.py --proxy=http://10.41.70.8:80 --random-agent --time-sec=10 --tamper tamper\space2morehash.py --batch -m url.txt -b") + #asp脚本 + else: + os.system("python D:\share\web\sqlmap\sqlmap.py --proxy=http://10.41.70.8:80 --random-agent --time-sec=10 --tamper tamper\charunicodeencode.py --batch -m url.txt -b") + ''' +if __name__=='__main__': + main() diff --git a/tcp.py b/tcp.py new file mode 100644 index 0000000..455ac0c --- /dev/null +++ b/tcp.py @@ -0,0 +1,83 @@ +tcp_client.py +#-*- coding: utf-8 -*- +from socket import * + +class TcpClient: + #测试,连接本机 + HOST='127.0.0.1' + #设置侦听端口 + PORT=1122 + BUFSIZ=1024 + ADDR=(HOST, PORT) + def __init__(self): + self.client=socket(AF_INET, SOCK_STREAM) + self.client.connect(self.ADDR) + + while True: + data=raw_input('>') + if not data: + break + + #python3传递的是bytes,所以要编码 + self.client.send(data.encode('utf8')) + print('发送信息到%s:%s' %(self.HOST,data)) + if data.upper()=="QUIT": + break + data=self.client.recv(self.BUFSIZ) + if not data: + break + #print('从%s收到信息:%s' %(self.HOST,data.decode('utf8'))) + print 'recv from %s infomation:%s' %(self.HOST,data.decode('utf8')) + + +if __name__ == '__main__': + client=TcpClient() + +-------------------------------------------------------------------------------------------- +tcp_server.py +#-*- coding: utf-8 -*- +from socket import * +from time import ctime +from time import localtime +import time + +HOST='' +PORT=1122 #设置侦听端口 +BUFSIZ=1024 +ADDR=(HOST, PORT) +sock=socket(AF_INET, SOCK_STREAM) + +sock.bind(ADDR) + +sock.listen(5) +#设置退出条件 +STOP_CHAT=False +while not STOP_CHAT: + print('等待接入,侦听端口:%d' % (PORT)) + tcpClientSock, addr=sock.accept() + #print('接受连接,客户端地址:',addr) + print "accept connection: ",addr + while True: + try: + data=tcpClientSock.recv(BUFSIZ) + except: + #print(e) + tcpClientSock.close() + break + if not data: + break + #python3使用bytes,所以要进行编码 + #s='%s发送给我的信息是:[%s] %s' %(addr[0],ctime(), data.decode('utf8')) + #对日期进行一下格式化 + ISOTIMEFORMAT='%Y-%m-%d %X' + stime=time.strftime(ISOTIMEFORMAT, localtime()) + #s='%s发送给我的信息是:%s' %(addr[0],data.decode('utf8')) + s='%s send me info is:%s' %(addr[0],data.decode('utf8')) + tcpClientSock.send(s.encode('utf8')) + print([stime], ':', data.decode('utf8')) + #如果输入quit(忽略大小写),则程序退出 + STOP_CHAT=(data.decode('utf8').upper()=="QUIT") + if STOP_CHAT: + break +tcpClientSock.close() +sock.close() diff --git "a/\345\237\272\347\241\200/lambda.md" "b/\345\237\272\347\241\200/lambda.md" new file mode 100644 index 0000000..aa69f85 --- /dev/null +++ "b/\345\237\272\347\241\200/lambda.md" @@ -0,0 +1,38 @@ +## lambda表达式 +lambda表达式,通常是在**需要一个函数,但是又不想费神去命名一个函数** 的场合下使用,也就是指**匿名函数** 。 +lambda所表示的匿名函数的内容应该是很简单的,如果复杂的话,干脆就重新定义一个函数了。 +**Python的lambda表达式基本语法是在冒号(:)左边放原函数的参数,可以有多个参数,用逗号(,)隔开即可;冒号右边是返回值。** +如: lambda x,y : (x + y) + +1. 应用在函数式编程中 +Python提供了很多函数式编程的特性,如:map、reduce、filter、sorted等这些函数都支持函数作为参数,lambda函数就可以应用在函数式编程中。如下: +将列表中的元素按照绝对值大小进行升序排列 +``` +list1 = [3,5,-4,-1,0,-2,-6] +sorted(list1, key=lambda x: abs(x)) +``` + +2. 应用在闭包中 +``` +def get_y(a,b): + return lambda x:a*x+b +y1 = get_y(1,1) +y1(1) # 结果为2 +>>> def get_y(a,b): +... return lambda x:a*x+b +... +>>> y1=get_y(1,1) +>>>y1(1) +2 +>>> y1(2) +3 +>>> y1(3) +4 +>>> y2=get_y(2,3) +>>> y2(1) +5 +>>> y2(2) +7 +>>> y2(3) +9 +``` diff --git "a/\345\237\272\347\241\200/yield.md" "b/\345\237\272\347\241\200/yield.md" new file mode 100644 index 0000000..95f3568 --- /dev/null +++ "b/\345\237\272\347\241\200/yield.md" @@ -0,0 +1,55 @@ +函数中使用yield,则该函数即是一个generator(生成器),具有iterable 效果。 + +举例:生成斐波那契數列 +``` +def fab(max): + n, a, b = 0, 0, 1 + while n < max: + yield b + # print b + a, b = b, a + b + n = n + 1 +``` +函数执行效果: +``` +>>> for n in fab(5): +... print n +... +1 +1 +2 +3 +5 +``` +使用yield后,fab函数每次返回一个数,而不是将所有的数都生成存在一个列表里。 +简单地讲,yield 的作用就是把一个函数变成一个 generator,带有 yield 的函数不再是一个普通函数,Python 解释器会将其视为一个 generator,调用 fab(5) 不会执行 fab 函数,而是返回一个 iterable 对象!在 for 循环执行时,每次循环都会执行 fab 函数内部的代码,执行到 yield b 时,fab 函数就返回一个迭代值,下次迭代时,代码从 yield b 的下一条语句继续执行,而函数的本地变量看起来和上次中断执行前是完全一样的,于是函数继续执行,直到再次遇到 yield。 + +也可以手动调用 fab(5) 的 next() 方法(因为 fab(5) 是一个 generator 对象,该对象具有 next() 方法),这样我们就可以更清楚地看到 fab 的执行流程: + +执行流程: +``` +>>> f = fab(5) +>>> f.next() +1 +>>> f.next() +1 +>>> f.next() +2 +>>> f.next() +3 +>>> f.next() +5 +>>> f.next() +Traceback (most recent call last): + File "", line 1, in +StopIteration +``` +当函数执行结束时,generator 自动抛出 StopIteration 异常,表示迭代完成。在 for 循环里,无需处理 StopIteration 异常,循环会正常结束。 + +### 我们可以得出以下结论: + +一个带有 yield 的函数就是一个 generator,它和普通函数不同,生成一个 generator 看起来像函数调用,但不会执行任何函数代码,直到对其调用 next()(**在 for 循环中会自动调用 next())才开始执行。** 虽然执行流程仍按函数的流程执行,但每执行到一个 yield 语句就会中断,并返回一个迭代值,下次执行时从 yield 的下一个语句继续执行。看起来就好像一个函数在正常执行的过程中被 yield 中断了数次,每次中断都会通过 yield 返回当前的迭代值。 + +yield 的好处是显而易见的,把一个函数改写为一个 generator 就获得了迭代能力,比起用类的实例保存状态来计算下一个 next() 的值,不仅代码简洁,而且执行流程异常清晰。 + +参考链接:https://www.ibm.com/developerworks/cn/opensource/os-cn-python-yield/ diff --git "a/\345\237\272\347\241\200/\343\200\212Python Cookbook\343\200\213\347\254\254\344\270\211\347\211\210\344\270\255\346\226\207v1.0.2.pdf" "b/\345\237\272\347\241\200/\343\200\212Python Cookbook\343\200\213\347\254\254\344\270\211\347\211\210\344\270\255\346\226\207v1.0.2.pdf" new file mode 100644 index 0000000..023badf Binary files /dev/null and "b/\345\237\272\347\241\200/\343\200\212Python Cookbook\343\200\213\347\254\254\344\270\211\347\211\210\344\270\255\346\226\207v1.0.2.pdf" differ