Mysql 4种事务隔离级别

获取隔离级别:

select @@global.tx_isolation, @@tx_isolation;

隔离级别说明:

=============================================================================
隔离级别               脏读(Dirty Read)          不可重复读(NonRepeatable Read)     幻读(Phantom Read)
=============================================================================

未提交读(Read uncommitted)        可能                            可能                       可能

已提交读(Read committed)          不可能                          可能                        可能

可重复读(Repeatable read)          不可能                          不可能                     可能

可串行化(Serializable )                不可能                          不可能                     不可能

=============================================================================

·未提交读(Read Uncommitted):允许脏读,也就是可能读取到其他会话中未提交事务修改的数据

·提交读(Read Committed):只能读取到已经提交的数据。Oracle等多数数据库默认都是该级别 (不重复读)

·可重复读(Repeated Read):可重复读。在同一个事务内的查询都是事务开始时刻一致的,InnoDB默认级别。在SQL标准中,该隔离级别消除了不可重复读,但是还存在幻象读

·串行读(Serializable):完全串行化的读,每次读都需要获得表级共享锁,读写相互都会阻塞

Python实现快速排序算法

#-*-encoding:utf-8-*-
import sys,random,time
reload(sys)
sys.setdefaultencoding('utf8')
def singleSort(list,start,end):
    if start>=end:
        return -1
    pivotPos = start
    pivot = list[pivotPos]
    low = start
    high = end
    while low<high:
        while list[high]>=pivot and low<high:
            high = high-1
        if low>=high: 
            return pivotPos
        exchange(list,pivotPos,high)
        pivotPos=high
        while list[low]<=pivot and low<high:
            low = low+1
        if low>=high: 
            return pivotPos
        exchange(list,pivotPos,low)
        pivotPos=low
def exchange(list,pos1,pos2):
    temp = list[pos1]
    list[pos1] = list[pos2]
    list[pos2] = temp
def qsort(list,low,high):
    pivotpos = singleSort(list,low,high)
    if pivotpos<0:
        return
    qsort(list,low,pivotpos-1)
    qsort(list,pivotpos+1,high)
def generateRandList(num):
    list=[]
    random.seed()
    for x in range(num):
        list.append(random.randint(0,65535))
    return list
if __name__ == '__main__':
     list = generateRandList(10000)
     #list=[4,8,2,3,7,9]
    qsort(list,0,len(list)-1)
    print list

Python多线程之条件变量Condition的使用

1.wait()方法会将使用该方法的线程挂起,并释放占用的Lock(由于这个Lock是Condition隐含的Lock.因此可以对实例化的Condition对象进行acquire()操作).
2.notify()会唤醒某个挂起的线程进入竞争Lock资源状态.
简单范例:

<pre>
#-*-encoding:utf-8-*-
import sys,time
import threading
reload(sys)
sys.setdefaultencoding('utf-8')

cond = threading.Condition()
def p1():
    global cond
    while True:
        cond.acquire()
        print "1 Got Lock"
        cond.notify()
        print "1 notify 2"
        cond.wait()
        print '1 is waked up'
        time.sleep(3)
   
def p2():
    global cond
    while True:
        cond.acquire()
        print "2 Got Lock"
        cond.notify()
        print "2 notify 1"
        cond.wait()
        print '2 is waked up'
        time.sleep(3)
   
   
t1 = threading.Thread(target=p1)
t2 = threading.Thread(target=p2)
t1.start()
t2.start()
</pre>

Python多线程关于信号量的使用

SEM = threading.Semaphore(MaxThreadNumber)
SEM就是一个信号量对象,可以通过acquire()及release()函数来获取信号量及释放占有的信号量.
threading.BoundedSemaphore与Semaphore类似,区别在于当使用BoundedSemaphore信号量时,当release()时会检查是否超出信号量的范围.

Python:获取HTTP代理并验证有效性

#-*-encoding:utf-8-*-
"""
Author: Corpse
Last modified: 2014-10-15
Filename: proxy.py
Description: Process proxy
"""
import threading,socket,multiprocessing,thread,requests,urllib2,cookielib,urllib
import sys,time,re
proxyList = {'nn':[],'nt':[],'wn':[],'wt':[]}
LOCK = {'nn':thread.allocate_lock(),'nt':thread.allocate_lock(),'wn':thread.allocate_lock(),'wt':thread.allocate_lock()}
#因为代理数目过大,为防止开启过多线程,使用信号量进行规模控制
SEM = {'nn':threading.BoundedSemaphore(40),'nt':threading.BoundedSemaphore(40),'wn':threading.BoundedSemaphore(40),'wt':threading.BoundedSemaphore(40)}
def operator(proxyType,pageCounter):#获取每种代理类型的指定页数的代理,验证后发送给服务器
    proxySource = 'http://www.xici.net.co/'+proxyType
    #开启多线程探测IP,每一个页面开启一个线程进行探测
    for pageNumber in xrange(pageCounter):
        url = proxySource+"/"+str(pageNumber+1)
        t = threading.Thread(target=proxyGetor,args=(url,proxyType,))
        t.start()
        t.join()
    global proxyList,SEM
    print "[+]"+proxySource+"前"+str(pageCounter)+'页存在',len(proxyList[proxyType]),"个HTTP代理."
    #至此已获取所有同一类型的代理IP及端口号
    #开放多进程验证代理的有效性
    for pry in proxyList[proxyType]:
        SEM[proxyType].acquire()
        thd = threading.Thread(target=validator,args=(pry,proxyType,10,))
        thd.start()
        thd.join()
    print "[+]经多线程代理有效性检测,"+proxySource+"前"+str(pageCounter)+'页存在',len(proxyList[proxyType]),"个有效HTTP代理."
    sendContent = ''
    for proxy in proxyList[proxyType]:
        sendContent+=proxy+'|'
    if len(sendContent)>0:
        print "[+]开始向服务器发送"+proxyType+"类型的数据"
        sendTarget = 'http://116.90.81.108:8081/sniffer.php' 
        serverResponse=proxySender(sendTarget,proxyType+'#'+sendContent)
        print "[+]向服务器发送"+proxyType+"类型数据的操作已经结束."
        print "[+]服务器返回消息:"+serverResponse
def proxyGetor(target,proxyType):#获取代理操作器
    response = requests.get(target)
    responseText = response.text
    responseText=responseText.replace('\r','').replace('\n','').replace(' ','')
    if responseText is not None:
        #正则匹配出代理
        rst=re.findall('\d+\.\d+\.\d+\.\d+\d+.*?.*?HTTP',responseText)
        global proxyList
        global LOCK
        for row in rst:
            ippt=row.split('><')
            ip=ippt[0][4:-4]
            port=ippt[1][3:-4]
            proxy = ip+':'+port
            LOCK[proxyType].acquire()
            proxyList[proxyType].append(proxy)
            LOCK[proxyType].release()
    else:
        print "[-]"+target+" Has No response." 
    #print '|',cnt,'|'
    #print proxyType+"==>",proxyList[proxyType]
    #sem.release()
def validator(proxyServer,proxyType,internalTime):#代理有效性验证
    global SEM,proxyList,LOCK
    testTarget="http://www.baidu.com"
    opener = urllib2.build_opener(urllib2.ProxyHandler({'http':proxyServer}))
    try:
        rsp = opener.open(testTarget,timeout=internalTime)
        #print '[',proxyType,']',proxyServer,"is OK."
    except Exception,e:#出现异常,判定为无效代理
        #print '[',proxyType,']',proxyServer,"Found Exception."
        LOCK[proxyType].acquire()#获取互斥锁,从列表中删除此代理
        index = proxyList[proxyType].index(proxyServer)#未加入异常处理
        proxyList[proxyType].pop(index)
        LOCK[proxyType].release()    
    finally:
        SEM[proxyType].release()
        #pass
def proxySender(target,data):
    cj = cookielib.LWPCookieJar() 
    cookie_support = urllib2.HTTPCookieProcessor(cj) 
    opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) 
    urllib2.install_opener(opener) 
    headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36', 
               'Referer' : 'http://www.d.com/'} 
    postData = {'F':'R',
                'B':'NBXXOO59X4DZUQWBPZ1LL5PONT6XDSL',
                'O':'1',
                'BI':data,}
    postData = urllib.urlencode(postData) 
    request = urllib2.Request(target, postData, headers) 
    response = urllib2.urlopen(request) 
    text = response.read()
    return text
def mainTrigger():#探测触发器
    proxyTypeTuple = ('nn','nt','wn','wt')
    pageCounter = 2
    #每种代理类型均使用一个单独的进程进行处理
    pool = multiprocessing.Pool(processes=multiprocessing.cpu_count())#设置进程池大小
    for x in xrange(len(proxyTypeTuple)):
        pool.apply_async(operator,(proxyTypeTuple[x],pageCounter,))
    pool.close()#关闭进程申请
    pool.join()#当所有子进程结束后主进程继续执行
def getServerOrder():
    posturl = 'http://116.90.81.108:8081/sniffer.php' 
    cj = cookielib.LWPCookieJar() 
    cookie_support = urllib2.HTTPCookieProcessor(cj) 
    opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler) 
    urllib2.install_opener(opener) 
    headers = {'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36', 
               'Referer' : 'http://www.d.com/',
               'Connection':'Keep-Alive'
               } 
    postData = {'F':'T',
                'B':'NBXXOO59X4DZUQWBPZ1LL5PONT6XDSL',
                'O':'1',} 
    postData = urllib.urlencode(postData) 
    request = urllib2.Request(posturl, postData, headers) 
    response = urllib2.urlopen(request) 
    text = response.read()
    return text
if __name__=="__main__":    
    socket.setdefaulttimeout(10)    
    while(True):
        order = getServerOrder()
        if order!="":
            print "[+]"+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))," Received New Order."
            mainTrigger()
        else:
            print "[-]"+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))," No New Order."
        time.sleep(10)
    
    
    

百度搜索之词条链接分析

非百度站内搜索,词条的链接是经过百度转码过后的.转码方法不得而知,所以现在还没有方法直接解码.
这个程序只是简单的通过借用百度搜索引擎,获取每个词条的标题及经过百度编码过后的链接地址.
经过测试发现每次搜索条目不超过760条,现在尚不知如何突破.后续会持续改进.附Python代码.

#encoding:utf-8
import urllib,re,sys,os
class_t = re.compile(u'<h3\s[^>]*?class=\"t.*?\">[\s\S]*?<a\s[\s\S]*?href[^=]*?=[^\"]*?\"([\S]*?)\"[\s\S]*?>(.*?)<\/a>[\s\S]*?<\/h3>')
http = re.compile('http\:\/\/[\S]*?\.[\S]*')
rsv_page = re.compile('<a[\s]href=\".*rsv\_page=1\".*>')
bd_style= re.compile('http\:\/\/www\.baidu\.com\/link\?\url\=(.*)')
def getUrl(ie,kw,rn,pn):
	try:
		url= 'http://www.baidu.com/s?ie='+ie+'&&wd='+kw+'&&rn='+str(rn)+'&&pn='+str(pn)+'&&cl=3'
		info=urllib.urlopen(url).read()
	except Exception,e:
		print 'Exception Found!'
	nextPage = len(rsv_page.findall(info))
	h3 = []
	h3 = class_t.findall(info)#h3为由元组构成的列表
	if len(h3)"+url
	if(len(h3) > 0):
		l = len(h3)
		fh = open('rec.txt','a+b')
		for i in range(0,l):
			if re.match(http,h3[i][0]):
				code = bd_style.findall(h3[i][0])
				if len(code)>0:
					fh.write(code[0]+'\r\n')
			else:
				pass
		fh.close()
	else:
		print "Not Found"
	if nextPage "+url
	return nextPage
if __name__ == "__main__":
	c=0
	ie = "utf-8"
	kw = sys.argv[1]#搜索关键词
	rn = 10#每页显示条目
	pn = 0#显示条目数
	if os.path.exists('rec.txt'):
		os.remove('rec.txt')
	while getUrl(ie,kw,rn,pn):
		pn+=rn

最终结果范例如下:
为什么幽鬼叫qwerty_dota吧_百度贴吧
L4rQejYYtarAKEHCnK2J5pbhkXOxKYan8I3CXk5cnbWx92efntqzsXeQgV_Ep2M4
DOTA 的QWERT 是谁?_百度知道
hka_1NOumisPcSY9CJIyuUaqzPiTNYEWkG3yzLhbd3bpIGUL07_Ik8vvygLH3PGvBtOFYOqv3lO3Go_tMeAUkK
幽鬼冠名者Qwerty究竟为何成为传奇?终于找到失传已久的真相视频_…
uHYgmFpMQxwGpKNuGQ9tqw9IWhxZRSRFLA8zZ251QbiNyRrK10WZ8_Ilwme1SazY
QWERTY全键盘手机大全】QWERTY全键盘手机报价及图片大全-ZOL…
RvpOd4_m0MKcqWXgyT2UHpvxq4_sfrevZ6j0UPAs0QbVCF_n1hmKKj8Fq2KZThebigATQJeqO40S7vC2TBY4g-9hEzBjfbFTNDb-JSasRI7
QWERTY键盘 – 维基百科,自由的百科全书
YvtRtT9Wdkto4izxBUV9TWTVertr7dY99QH7QOGQw0oE__HjWjfPehPKYKB7tbYG
QWERTY – 百度百科
26PsoOLr21y4xSZFIwyoXyBFpnXyJWRvMIHHAokD9T50veBe_p-Z6DjcgLwc_obr5WU2syp2ModNceGGGPEdra
手机虚拟键盘和手机虚拟qwerty键盘到底有什么区_百度文库
qU9CJGEuIapMkrsaqoq9JzQcXQM7X0t2lr5MRxV848DyS72aGAU4XdF8hq8ic-CRPjzQF1z2eVYYShwdIoRFp9_fhEyFgKw3ArT0rzNSY2_
曲面屏幕+QWERTY键盘:Moment智能手表即将发起众筹—在线播放—…
I-gblQACmgDa8EITeV0QaphobPbZAlWIQOrXlh9RxldwOYmE09aALh68oSLdrPOfO9bD6cSQoYvlOqIBH-5WV_
P990 的四种键盘: QWERTY, QWERTZ, AZERTY 和 Russian …_百度知道
qtF7P3Dewx8KdRbJIVSQPCSc66UrvSUmwMuK6SH-Ngxe1bjPEv_1gzL_e_Rbz2KCl40bMJ3E57DKS32DvbMKpq
qwertyuiop_百度百科
-n8njs2r2wU32ivMc-f8_xgY5hhWu_Qt8NI826OJxEDiBLbMka7zDc35njAXjGoF
幽鬼的名字为什么可能是qwerty-,找到了这个视频。_dota吧_百度贴吧
tOCbxGQTcECEKJHsZKBn3l1DL7sHoTzcJM2_114QK88SJRXcSeobFKPvyZ3ZSIIh
qwerty东西8的贴吧
KBiQZArCQRT89RhiPLP5Bg7ddyWXYMf78HjQKKyR_k0YrcnR0HzuI5HucIKMTY-z1-fHu9DWagW8aZ0VlEBMqIafLdJu4evJJKn7Hy-Nwy7