一、gui 编程
1、简单操作
#!/usr/bin/python #encoding=utf-8 import Tkinter #ptyhon3改小写了 python2是大写 top=Tkinter.Tk() #创建一个窗体 top.mainloop() #进入消息循环 2、界面操作 #!/usr/bin/python #encoding=utf-8 from Tkinter import * root =Tk() #进入窗体 #加入一个数据 lg=['c','cpp','java','js','python'] #list数据 lista= Listbox(root) #创建列表,在root for item in lg: lista.insert(0,item) #循环插入数据 lista.pack() #加入root窗体 #加入多个数据 lg1=['c1','cpp1','java1'] #list数据 lista1= Listbox(root) #创建列表,在root for item in lg1: lista1.insert(0,item) #循环插入数据 lista1.pack() #加入root窗体 root.mainloop() #进入循环 二、多线程1、信号量
#!/usr/bin/python #encoding=utf-8 import subprocess import signal def go(): print "ping baidu OK" #函数 signal.signal(signal.SIGINT,go) #信号,触发这个函数 Linux pingp= subprocess.Popen(args='ping www.baidu.com',shell=True) #Popen打开一个管道 pingp.wait() print(pingp.pid) print (pingp.returncode) 2、多线程 #!/usr/bin/python #encoding=utf-8 import thread import time def printit(threadname): count =0 while True: count +=1 time.sleep(1) print threadname +"time"+ str(count) thread.start_new_thread(printit,("name1",)) thread.start_new_thread(printit,("name2",)) while True: #主线程不能退出 pass3、多线程进阶1
#!/usr/bin/python #encoding=utf-8 import thread import threading import time class mythread(threading.Thread): #类创建线程 def __init__(self,threadid,name,counter): #构造函数 threading.Thread.__init__(self) self.threadid=threadid self.name=name self.counter=counter def run(self): #运行的主体 print "thread start "+self.name printit(self.name) print "thread stop "+self.name def printit(threadname): #运行的函数 count =0 while True: count +=1 time.sleep(1) print threadname +"time"+ str(count)+"\n" thread1= mythread(1,"name1",1) #创建线程对象 thread2= mythread(2,"name2",1) thread1.start() #开始线程 thread2.start() while True: pass 4、多线程进阶2 #!/usr/bin/python #encoding=utf-8 import thread import threading import time class mythread(threading.Thread): #类创建线程 def __init__(self,threadid,name,counter): #构造函数 threading.Thread.__init__(self) self.threadid=threadid self.name=name self.counter=counter def run(self): #运行的主体 print "thread start"+self.name threadLock.acquire()#锁定 加锁 printit(self.name) threadLock.release()#释放 释放锁 print "thread stop "+self.name def printit(threadname): #运行的函数 count =0 while True: count +=1 time.sleep(1) print threadname +"time"+ str(count)+"\n" threadLock=threading.Lock() #声明锁 thread1= mythread(1,"name1",1) #创建线程对象 thread2= mythread(2,"name2",1) thread1.start() #开始线程 thread2.start() while True: pass 5、多线程进阶3 #!/usr/bin/python # -*- coding: UTF-8 -*- import Queue import threading import time # 三个线程 抢5个数据 exitFlag = 0 class myThread (threading.Thread): def __init__(self, threadID, name, q): threading.Thread.__init__(self) self.threadID = threadID self.name = name self.q = q def run(self): print "Starting " + self.name process_data(self.name, self.q) print "Exiting " + self.name def process_data(threadName, q): while not exitFlag: queueLock.acquire() if not workQueue.empty(): data = q.get() queueLock.release() print "%s processing %s" % (threadName, data) else: queueLock.release() time.sleep(1) threadList = ["Thread-1", "Thread-2", "Thread-3"] nameList = ["One", "Two", "Three", "Four", "Five"] queueLock = threading.Lock() workQueue = Queue.Queue(10) threads = [] threadID = 1 # 创建新线程 for tName in threadList: thread = myThread(threadID, tName, workQueue) thread.start() threads.append(thread) threadID += 1 # 填充队列 queueLock.acquire() for word in nameList: workQueue.put(word) queueLock.release() # 等待队列清空 while not workQueue.empty(): pass # 通知线程是时候退出 exitFlag = 1 # 等待所有线程完成 for t in threads: t.join() print "Exiting Main Thread"三、http爬虫基础1、抓取了百度的信息,打印到控制台
def downfile(url,savepath): try: url =url.strip()#清理空格 savepath=savepath.strip()#清理空格 except: #处理异常 pass fbaidu=urllib.urlopen('http://www.baidu.com') fline=fbaidu.readlines() for line in fline : #插入正则表达式,抓取http,抓取email print line2、#http 读取网页,下载到本地#!/usr/bin/python #encoding=utf-8 import urllib2 import time def downfile(url,savepath): try: url =url.strip()#清理空格 savepath=savepath.strip()#清理空格 r=urllib2.Request(url)#请求 req=urllib2.urlopen(r) #打开并保存 savefile=open(savepath,"wb") #写入 savefile.write(req.read()) #duqu shuju savefile.close() req.close() except: #处理异常 pass downfile("http://blog.csdn.net/qq_31780525",r"C:\baidu.txt") time.sleep(15)四、写入word与excel1、写入word#!/usr/bin/env python #encoding=utf-8 from time import sleep #休眠 from win32com.client import Dispatch, constants #客户端 import win32com #com操作office import win32com.client #客户端 # 安装 pywin32-220.win-amd64-py2.7 # 创建word文档,并写入文字 def word (name): word=win32com.client.Dispatch('word.Application') #创建word对象 doc =word.Documents.Add() #增加一个文档 word.Visible =True #设置不可见 sleep(1) rng= doc.Range(0,0) #插入的范围从0开始 rng.InsertAfter("hell world"+name) #写入文档 rng.InsertAfter("hell world hello china") sleep(1) filename="C:\Users\\"+name+".doc" doc.SaveAs(filename)#b保存文件 doc.Close() word.Application.Quit() print "word ok" word("zgf") word("123") word("456") 2、写入excel#!/usr/bin/env python #encoding=utf-8 from time import sleep #休眠 from win32com.client import Dispatch, constants #客户端 import win32com #com操作office import win32com.client #客户端 def excel (name): ex=win32com.client.Dispatch('Excel.Application') #创建excel对象 wk =ex.Workbooks.Add() #增加表格 ex.Visible =True #设置不可见 nwk=wk.ActiveSheet;#当前活动表格 sleep(1) nwk.Cells(1,1).value ="4" nwk.Cells(2, 2).value = "14" filename="C:\Users\\"+name+".xls" wk.SaveAs(filename)#b保存文件 wk.Close() ex.Application.Quit() print "excel ok" excel("zgf") 五、socket编程1、客户端#!/usr/bin/python #encoding=utf-8 import sys reload(sys) sys.setdefaultencoding("utf-8") import socket # 导入 socket 模块 s = socket.socket() # 创建 socket 对象 host = socket.gethostname() # 获取本地主机名 port = 12346 # 设置端口好 s.connect((host, port)) data= str(s.recv(1024)).decode("utf-8") #assii编码 print data s.close()2、服务端#!/usr/bin/python # -*- coding: UTF-8 -*- import sys reload(sys) sys.setdefaultencoding("utf-8") #字符串用utf-8 import socket # 导入 socket 模块 s = socket.socket() # 创建 socket 对象 host = socket.gethostname() # 获取本地主机名 port = 12346 # 设置端口 s.bind((host, port)) # 绑定端口 s.listen(5) # 等待客户端连接 并监听 while True: c, addr = s.accept() # 建立客户端连接。 print "ip : ",addr c.send ("abadsjjasdjsjhadjh小伙子你好") c.close()