1、多线程和Queue
需求:
编写py脚本实现,生产者从content中读取内容放到Queue中,然后消费者从Queue读取内容,最后保存到newcontent
源码如下:
content:
《念奴娇 赤壁怀古》
年代: 宋 作者: 苏轼
大江东去,浪淘尽,千古风流人物。
故垒西边,人道是,三国周郎赤壁。
乱石穿空,惊涛拍岸,卷起千堆雪。
江山如画,一时多少豪杰。
遥想公瑾当年,小乔初嫁了,雄姿英发。
羽扇纶巾,谈笑间,樯橹灰飞烟灭。(樯橹 一作:强掳)
故国神游,多情应笑我,早生华发。
人生如梦,一尊还酹江月。(人生 一作:人间;尊 通:樽)
江城子·乙卯正月二十日夜记梦
宋代:苏轼
十年生死两茫茫,不思量,自难忘。千里孤坟,无处话凄凉。纵使相逢应不识,尘满面,鬓如霜。
夜来幽梦忽还乡,小轩窗,正梳妆。相顾无言,惟有泪千行。料得年年肠断处,明月夜,短松冈。(肠断 一作:断肠)
newcontent 内容为空
threadQueue.py
#!/usr/bin/env python #coding:utf8 import sys print(sys.getdefaultencoding()) import codecs from threading import Thread import time from queue import Queue class Produce(Thread): def __init__(self,queue): super(Produce,self).__init__() self.fileName = "content" self.fileList = list() self.queue = queue def run(self): with codecs.open(self.fileName,'r',encoding = 'utf-8') as fd: data = fd.readlines() print(data) self.fileList += data for line in self.fileList: self.queue.put(line) class Consumer(Thread): def __init__(self,queue): self.queue = queue super(Consumer,self).__init__() self.newPassword = "newcontent" self.fileList = list() self.status = 1 def run(self): while 1: if self.queue.empty(): time.sleep(1) self.status += 1 print(self.status) if self.status == 5: break else: self.status = 1 data = self.queue.get() self.fileList.append(data) with codecs.open(self.newPassword,"w",encoding = 'utf-8') as fd: fd.writelines(self.fileList) def main(): q = Queue() produce = Produce(q) consumer = Consumer(q) produce.start() consumer.start() if __name__ == "__main__": main()
运行结果:
utf-8 ['《念奴娇 赤壁怀古》 ', '年代: 宋 作者: 苏轼 ', '大江东去,浪淘尽,千古风流人物。 ', '故垒西边,人道是,三国周郎赤壁。 ', '乱石穿空,惊涛拍岸,卷起千堆雪。 ', '江山如画,一时多少豪杰。 ', '遥想公瑾当年,小乔初嫁了,雄姿英发。 ', '羽扇纶巾,谈笑间,樯橹灰飞烟灭。(樯橹 一作:强掳) ', '故国神游,多情应笑我,早生华发。 ', '人生如梦,一尊还酹江月。(人生 一作:人间;尊 通:樽) ', ' ', '江城子·乙卯正月二十日夜记梦 ', ' ', '宋代:苏轼 ', ' ', '十年生死两茫茫,不思量,自难忘。千里孤坟,无处话凄凉。纵使相逢应不识,尘满面,鬓如霜。 ', '夜来幽梦忽还乡,小轩窗,正梳妆。相顾无言,惟有泪千行。料得年年肠断处,明月夜,短松冈。(肠断 一作:断肠)'] 2 2 3 4 5
打开newcontent 验证:内容跟content 一致
《念奴娇 赤壁怀古》
年代: 宋 作者: 苏轼
大江东去,浪淘尽,千古风流人物。
故垒西边,人道是,三国周郎赤壁。
乱石穿空,惊涛拍岸,卷起千堆雪。
江山如画,一时多少豪杰。
遥想公瑾当年,小乔初嫁了,雄姿英发。
羽扇纶巾,谈笑间,樯橹灰飞烟灭。(樯橹 一作:强掳)
故国神游,多情应笑我,早生华发。
人生如梦,一尊还酹江月。(人生 一作:人间;尊 通:樽)
江城子·乙卯正月二十日夜记梦
宋代:苏轼
十年生死两茫茫,不思量,自难忘。千里孤坟,无处话凄凉。纵使相逢应不识,尘满面,鬓如霜。
夜来幽梦忽还乡,小轩窗,正梳妆。相顾无言,惟有泪千行。料得年年肠断处,明月夜,短松冈。(肠断 一作:断肠)
2、Socket编程
练习内容:
服务端socklet向客户端client发送数据,client接受并打印。
socketutil.py
#!/usr/bin/env python #coding:utf8 import socket import sys from datetime import time import time class InitSocket(object): def __init__(self,host,port,type): self.host = host self.port = port self.address = (host,port) self.type = type self.s = None self.createsocket() def createsocket(self): if self.type.upper() == "TCP": self.s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) return self.s elif self.type.upper == "UDP": self.s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) return self.s else: print("you must input the InitSocket type is 'UDP|TCP'") class SocketServer(InitSocket): def __init__(self,host,port,type,backlog): self.backlog = backlog self.clientAddress =None super(SocketServer,self).__init__(host,port,type) def run(self): self.s.bind(self.address) self.s.listen(self.backlog) print("server start....") conn,self.clientAddress = self.s.accept() print("accept connect from {0}".format(conn)) for i in range(1,10): print("i = {0}".format(str(i)).encode("utf-8")) conn.sendall("i = {0}".format(str(i)).encode("utf-8")) self.s.close() class ClientSocket(InitSocket): def run(self): self.s.connect(self.address) status = 1 while 1: data = self.s.recv(1024) if len(data.decode("utf-8")) > 0 : print(data) else: status +=1 time.sleep(1) if status >5: break
testsocketserver.py
from sockettest.socketutil import SocketServer if __name__ == "__main__": sockServer = SocketServer(host="192.168.88.121",port=8000,type="tcp",backlog=5) sockServer.run()
testsocketclient.py
from sockettest.socketutil import ClientSocket if __name__ == '__main__': socketClient = ClientSocket(host="192.168.88.121", port=8000, type="tcp") socketClient.run()
一次运行testsocketserver.py和testsocketclient.py
testsocketserver.py
server start.... accept connect from <socket.socket fd=188, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('192.168.88.121', 8000), raddr=('192.168.88.121', 63138)> b'i = 1' b'i = 2' b'i = 3' b'i = 4' b'i = 5' b'i = 6' b'i = 7' b'i = 8' b'i = 9'
testsocketclient.py
b'i = 1'
b'i = 2i = 3i = 4i = 5i = 6i = 7i = 8i = 9'