zoukankan      html  css  js  c++  java
  • day8-套接字sock 实现SSH远程执行命令功能

    复习

    #面向对象编程
    #类:
    #对象
    #实例化 :从一个类到产生一个对象的过程
        #对象 = 类名()   #__init__初始化方法,是为了给一个具体的对象放一些初识的属性
    #在类中:
        # 静态属性 直接定义在类中的属性,使用静态属性:类名、对象名都可以调用
        # 动态属性 就是方法 就是定义在类中的函数 默认传一个self
    # class Person:
    #     money = 100
    # sister = Person()
    # father = Person()
    # Person.money += 200
    # Person.money += 500
    # print(Person.money)
    #组合:是什么有什么的关系。
        #一个类的对象作为两外一个类的属性来使用
    #继承
        #子类
        #父类、基类、超类
        #单继承和多继承
        #子类中出现 : 派生 父类中没有的子类中有了
            #派生方法
            #派生属性
        #经典类和新式类在多继承上有什么特点
            #新式类广度优先和经典类深度优先

    # python2 : 经典类 指名道姓去使用父类的方法

    class A:
        def func(self):print('A')

    class B(A):
        def func(self):
            super().func()
            # super(B,self).func()
            print('B')

    b = B()
    b.func()
    super(B,b).func() #不常用

     

    多态

    #初识面向对象
    #继承和组合
    #面向对象的进阶
    #多态
    #封装
    #面向对象:反射
    #类中的内部的 双下方法

    #__init__
    #__dict__

    #多态:python自带多态

    # def func(int a):
    #     print(a)

    # func(2)
    # func('ashkgask')

    class Animal:
        pass

    class Dog(Animal):pass
    class Cat(Animal):pass

    wangcai = Dog()
    kitty = Cat()
    def eat(obj):
        print(obj.name,'在吃')

    eat(1)
    eat('hello')

    #多态:在其他语言里:类的继承
          #在Python中

    #有相同特点的类:鸭子类型
        #list 和 tuple  鸭子类型
        #1+2   'a'+'b'

     

    封装

    #封装就是把方法和属性 扔到一个容器里装起来
    #私有的:只能在类的内部使用,不能直接在类的外部使用
    # class Supermarket:
    #     __DISCONT = 0.8  #加了双下划线就被定义成了一个私有的
    #     def __init__(self,org_price):
    #         self.price = org_price * Supermarket.__DISCONT
    # A.__COUNTRY
    # a = A()
    # a.func()
    # print(A.__dict__)
    # print(A._A__COUNTRY)   #不合法

    # class Goods:
    #     __DISCONT = 0.8  #加了双下划线就被定义成了一个私有的
    #     def __init__(self,org_price):
    #         self.price = org_price * Goods.__DISCONT
    #
    # apple = Goods(10)
    # print(apple.price)

    # class A:
    #     def __func(self):
    #         self.__name = 'alex'
    #
    # a = A()
    # a._A__func()

    #私有的  __名字
    #定义私有静态属性
    #定义私有的方法
    #定义私有的对象属性
    #只要是私有的:类内正常使用,类外不能直接使用。如果一定要用_类名__名字
    #私有的东西可以子类中使用么?不能

    #用到私有变量
        #1.当我们想要隐藏一个变量不被外调用的时候
        #2.当我们写在父类中且不希望子类继承的时候
    class A:
        __B = 'hahaha'

    class B(A):pass

    print(A.__dict__)
    print(B.__dict__)

     

    几个类中的装饰器

    #property
    #classmethod
    #staticmethod
    # class Circle:
    #     def __init__(self,r):
    #         self.r = r
    #
    #     @property
    #     def area(self):
    #         return self.r *self.r *3.14
    #
    # c1 = Circle(5)
    # print(c1.area)

    class A:
        def __init__(self,name):
            self.__name = name
        @property
        def name(self):
            return self.__name
        @name.setter
        def name(self,new_name):
            if type(new_name) is str:
                self.__name = new_name

    # a = A('alex')
    # print(a.name)
    # #希望不从外部被修改 所以设置一个私有的属性
    # #但是又希望能从外部查看 所以使用一个property
    # a.name = 'alex_sb'
    # print(a.name)

    #classmethod
    class A:
        country = 'China'
        def func(self):
            self.name= 'alex'

        @classmethod    #类方法:就是不需要传具体的对象
                        #但是可以使用类的属性、静态属性
        def c_method(cls):
            print('in class method')
            print(cls.country)

        @staticmethod   #静态方法
        def s_method():print('in static method')

    A.c_method()
    A.s_method()

    #完全的面向对象编程
    #所有的方法代码都必须写类里
    #只能把原来的函数写进类里
    #而原本又只是函数 所以就加上一个staticmethod装饰器

     

    反射

    #issubclass(子类,父类)
    # class A:pass
    # class B(A):pass
    # print(issubclass(B,A))
    # isinstance(对象,类名)
    #判断一个对象是不是这个类的实例
    # b = B()
    # print(isinstance(b,B))
    # print(isinstance(b,A))

    #什么叫反射
    #使用字符串的形式去获取变量
    # a = 1
    # b = 2
    # name = input('变量名 :')
    # if name == 'a':
    #     print(a)

    class Person:
        role = 'Person'
        country = 'China'
        def __init__(self,name,age):
            self.name = name
            self.age = age

        def func(self):
            print('%s in func'%self.name)
    alex = Person('alex',80)
    # name = input('属性名 :')
    # Person.role
    # alex.name  alex.age
    # if hasattr(alex,'func'):
    #     func = getattr(alex,'func')
    #     func()


    def func2(self):
        print('%s in func2' % self.name)

    #setattr
    # alex.sex = None
    # setattr(alex,'sex','不详')
    # print(alex.sex)
    # setattr(alex,'func2',func2)  #setattr绑定方法是一个假的
    # # print(alex.func)          # 在使用的时候必须要手动传self对象
    # alex.func2(alex)
    #delattr
    # delattr(alex,'name')  #del alex.name
    # alex.name
    #使用反射来调用类中的方法和属性,对象的属性


    # import demo1
    # print(demo1.a)
    # print(getattr(demo1,'a'))
    # demo1.qqxing()
    # getattr(demo1,'qqxing')()

    a = 'aaaaa'
    import sys
    this_module = sys.modules[__name__]
    print(getattr(this_module,'a'))


    #反射:a.b --> getattr(a,'b')
    #hasattr getattr setattr delattr
    #类,静态属性、类方法
    #对象,方法、属性
    #模块,函数,变量
    #本模块,函数,变量

     

    类中的内置方法

    # class A:
    #     def __call__(self, *args, **kwargs):
    #         print('aaaaaaaa')

    # a = A()()

    #__new__
    class A:
        def __init__(self):     #初始化方法
            self.x = 1
            print('in init function')
        def __new__(cls, *args, **kwargs):  #构造方法
            print('in new function')
            return object.__new__(A, *args, **kwargs)
    a = A()   #先构造一个对象 再初始化属性
    #单例模式
    #从头到尾只有一个实例
    class Singleton:
        # def __new__(cls, *args, **kw):
        #     if not hasattr(cls, '_instance'):
        #         cls._instance = object.__new__(A, *args, **kw)
        #     return cls._instance
        # def __str__(self):
        #     return 'singleton 的 实例'

        def __repr__(self):
            return 'singleton repr'
    one = Singleton()
    two = Singleton()
    # print('%s'%one)
    # two.a = 3
    # print(one.a)
    # print(id(one))
    # print(id(two))
    # print(one == two)
    # print(one is two)

    #面试的时候
    #单例模式是一种设计模式
    #设计模式早期只在java里使用

    # l = list()
    # l.append(123)
    # print(l)

    # print('%r is somebody'%'alex')
    # print('%s is somebody'%'alex')

    #这个类有100个对象,只要name和sex相同,我就认为是相同的对象
    #要求对100个对象进行去重
    class Person:
        def __init__(self,name,age,sex):
            self.name = name
            self.age = age
            self.sex = sex

        def __hash__(self):
            return hash(self.name+self.sex)

        def __eq__(self, other):
            if self.name == other.name and self.sex == other.sex:return True


    p_lst = []
    for i in range(84):
        p_lst.append(Person('egon',i,'male'))

    print(p_lst)
    print(set(p_lst))

     

    基于TCP协议的简单套接字通信

    客户端:

    import socket

    #买手机
    phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #SOCK_STREAM代表TCP协议

    #发起电话链接
    phone.connect(('127.0.0.1',8080))


    #发消息
    phone.send('hello'.encode('utf-8'))

    #收消息
    data=phone.recv(1024)
    print(data)

    #关机
    phone.close()

    服务端

    import socket

    #买手机
    phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #SOCK_STREAM代表TCP协议

    #绑定手机卡
    phone.bind(('127.0.0.1',8080))

    #开机
    phone.listen(5)

    #等电话链接
    print('starting...')
    conn,client_addr=phone.accept() #(套接字链接,客户端的ip和port)
    print(conn)
    # print(client_addr)

    #收消息
    data=conn.recv(1024) # 1024最大的限制
    print('客户端数据: ',data)

    #发消息
    conn.send(data.upper())

    #挂电话
    conn.close()

    #关机
    phone.close()

     

    加上通信循环和链接循环

    服务端:

    import socket

    #买手机
    phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #SOCK_STREAM代表TCP协议
    # phone.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) #就是它,在bind前加

    #绑定手机卡
    phone.bind(('127.0.0.1',8080))

    #开机
    phone.listen(5)

    #等电话链接
    print('starting...')
    while True:
        conn,client_addr=phone.accept() #(套接字链接,客户端的ip和port)
        print(client_addr)

        while True: #通信循环
            #收消息
            try:
                data=conn.recv(1024) # 1024最大的限制
                print('客户端数据: ',data)
                if not data:break #针对linux系统
                #发消息
                conn.send(data.upper())
            except ConnectionResetError:
                break

        #挂电话
        conn.close()

    #关机
    phone.close()

    客户端0

    import socket

    #买手机
    phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #SOCK_STREAM代表TCP协议

    #发起电话链接
    phone.connect(('127.0.0.1',8080))


    while True:
        #发消息
        msg=input('>>: ').strip()
        if not msg:continue
        phone.send(msg.encode('utf-8'))
        print('has send====>')
        #收消息
        data=phone.recv(1024)
        print('has recv=====>')
        print(data.decode('utf-8'))

    #关机
    phone.close()









    客户端1

    import socket

    #买手机
    phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #SOCK_STREAM代表TCP协议

    #发起电话链接
    phone.connect(('127.0.0.1',8080))


    while True:
        #发消息
        msg=input('>>: ').strip()
        if not msg:continue
        phone.send(msg.encode('utf-8'))
        print('has send====>')
        #收消息
        data=phone.recv(1024)
        print('has recv=====>')
        print(data.decode('utf-8'))

    #关机
    phone.close()









     

    客户端2

    import socket

    #买手机
    phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #SOCK_STREAM代表TCP协议

    #发起电话链接
    phone.connect(('127.0.0.1',8080))


    while True:
        #发消息
        msg=input('>>: ').strip()
        if not msg:continue
        phone.send(msg.encode('utf-8'))
        print('has send====>')
        #收消息
        data=phone.recv(1024)
        print('has recv=====>')
        print(data.decode('utf-8'))

    #关机
    phone.close()









     

     

    实现ssh远程执行命令的功能(low版)

    服务端:

    import socket
    import subprocess
    import struct

    phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #SOCK_STREAM代表TCP协议
    phone.bind(('127.0.0.1',8080))
    phone.listen(5)

    while True:
        conn,client_addr=phone.accept() #(套接字链接,客户端的ip和port)
        print(client_addr)

        while True: #通信循环
            #收消息
            try:
                cmd=conn.recv(1024) # 1024最大的限制
                if not cmd:break #针对linux系统

                #执行,拿到执行结果
                obj = subprocess.Popen(cmd.decode('gbk'), shell=True,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE)

                stdout_res=obj.stdout.read()
                stderr_res=obj.stderr.read()
                #先发报头
                total_size=len(stderr_res)+len(stdout_res)
                conn.send(struct.pack('i',total_size))

                #再发真是的数据
                # conn.send(stdout_res+stderr_res)
                conn.send(stdout_res)
                conn.send(stderr_res)
            except ConnectionResetError:
                break

        #挂电话
        conn.close()

    #关机
    phone.close()

    客户端:

    import socket
    import struct

    #买手机
    phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #SOCK_STREAM代表TCP协议

    #发起电话链接
    phone.connect(('127.0.0.1',8080))


    while True:
        #发消息
        cmd=input('>>: ').strip()
        if not cmd:continue
        phone.send(cmd.encode('gbk'))

        #先收报头
        header_struct=phone.recv(4)
        total_size=struct.unpack('i',header_struct)[0]

        #再收消息
        cmd_res=b''
        recv_size=0
        while recv_size < total_size:
            recv_data=phone.recv(1024)
            cmd_res+=recv_data
            recv_size+=len(recv_data)

        print(cmd_res.decode('gbk'))

    #关机
    phone.close()









     

    粘包

    服务端1

    from socket import *


    server=socket(AF_INET,SOCK_STREAM)
    server.bind(('127.0.0.1',8080))
    server.listen()

    conn,addr=server.accept()

    data1=conn.recv(5)
    print('data1:',data1)
    data2=conn.recv(5)
    print('data2:',data2)

     

    客户端1

    from socket import *


    client=socket(AF_INET,SOCK_STREAM)
    client.connect(('127.0.0.1',8080))


    client.send('hello'.encode('utf-8'))
    # import time
    # time.sleep(10)
    client.send('world'.encode('utf-8'))

    服务端2

    from socket import *


    server=socket(AF_INET,SOCK_STREAM)
    server.bind(('127.0.0.1',8080))
    server.listen()

    conn,addr=server.accept()

    data1=conn.recv(1)
    print('data1:',data1)
    import time
    time.sleep(5)
    data2=conn.recv(1024)
    print('data2:',data2)

     

    客户端2

    from socket import *


    client=socket(AF_INET,SOCK_STREAM)
    client.connect(('127.0.0.1',8080))


    client.send('hello'.encode('utf-8'))
    import time
    time.sleep(3)
    client.send('world'.encode('utf-8'))

     

    实现ssh远程执行命令的功能(最终版)

    服务端:

    import socket
    import subprocess
    import struct
    import json

    phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #SOCK_STREAM代表TCP协议
    phone.bind(('127.0.0.1',8080))
    phone.listen(5)

    while True:
        conn,client_addr=phone.accept() #(套接字链接,客户端的ip和port)
        print(client_addr)

        while True: #通信循环
            #收消息
            try:
                cmd=conn.recv(1024) # 1024最大的限制
                if not cmd:break #针对linux系统

                #执行,拿到执行结果
                obj = subprocess.Popen(cmd.decode('gbk'), shell=True,
                                        stdout=subprocess.PIPE,
                                        stderr=subprocess.PIPE)

                stdout_res=obj.stdout.read()
                stderr_res=obj.stderr.read()

                # 制作报头
                header_dic = {
                    'filename': 'a.txt',
                    'total_size': len(stdout_res)+len(stderr_res),
                    'md5': 'xxxxxxxxx'
                }
                head_json = json.dumps(header_dic)
                head_bytes = head_json.encode('utf-8')




                #先发报头长度
                conn.send(struct.pack('i',len(head_bytes)))

                #先发报头
                conn.send(head_bytes)

                #再发真是的数据
                # conn.send(stdout_res+stderr_res)
                conn.send(stdout_res)
                conn.send(stderr_res)
            except ConnectionResetError:
                break

        #挂电话
        conn.close()

    #关机
    phone.close()

     

    客户端:

                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 import socket
    import struct
    import json

    #买手机
    phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #SOCK_STREAM代表TCP协议

    #发起电话链接
    phone.connect(('127.0.0.1',8080))


    while True:
        #发消息
        cmd=input('>>: ').strip()
        if not cmd:continue
        phone.send(cmd.encode('gbk'))

        #先收报头长度
        struct_res=phone.recv(4)
        header_size=struct.unpack('i',struct_res)[0]

        #再收报头
        head_bytes=phone.recv(header_size)
        head_json=head_bytes.decode('utf-8')
        head_dic=json.loads(head_json)
        print(head_dic)

        #最收消息
        cmd_res=b''
        recv_size=0
        total_size=head_dic['total_size']
        while recv_size < total_size:
            recv_data=phone.recv(1024)
            cmd_res+=recv_data
            recv_size+=len(recv_data)

        print(cmd_res.decode('gbk'))

    #关机
    phone.close()









     

    异常处理try


    print('====>1')
    print('====>2')
    print('====>3')
    # l=[]
    # l[1000000000000000000000000000000000000000000]
    # d={}
    # d['k']

    age=input('>>: ').strip()
    if age.isdigit():
        int(age)

    print('====>4')
    print('====>5')




    print('====>6')

     

    执行系统命令的模块


    # import os
    #
    # res=os.system('dir')
    # print('命令的结果',res)


    # import subprocess
    #
    # obj=subprocess.Popen('dir',shell=True,
    #                      stdout=subprocess.PIPE,
    #                      stderr=subprocess.PIPE)
    #
    #
    #
    # stdout_res1=obj.stdout.read()
    # print(stdout_res1.decode('gbk'))

    # stdout_res2=obj.stdout.read() #在第一次读时,管道就空了
    # print('========>',stdout_res2.decode('gbk'))




    # import subprocess
    #
    # obj=subprocess.Popen('diasdfasdfasr',shell=True,
    #                      stdout=subprocess.PIPE,
    #                      stderr=subprocess.PIPE)



    # stdout_res1=obj.stdout.read() #命令执行错误会把结果送到stderr管道
    # print(stdout_res1.decode('gbk'))

    # stdout_res2=obj.stderr.read() #命令执行错误会把结果送到stderr管道
    # print(stdout_res2.decode('gbk'))





    #ls ; pwasdfasdfd; echo 123
    #ls && pwd && echo 123





    #执行命令
    # import subprocess
    #
    # obj=subprocess.Popen('tasklist | findstr pycharm',shell=True,
    #                      stdout=subprocess.PIPE,
    #                      stderr=subprocess.PIPE)
    #
    # print(obj.stdout.read().decode('gbk'))



    import subprocess
    obj1=subprocess.Popen('tasklist',shell=True,
                     stdout=subprocess.PIPE,)

    obj2=subprocess.Popen('findstr pycharm',shell=True,
                         stdin=obj1.stdout,
                         stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE)

    print(obj2.stdout.read().decode('gbk'))

     

  • 相关阅读:
    spring 配置版本问题
    sublime与Emment
    工欲善其事必先利其器之浏览器篇
    工欲善其事必先利其器之windows篇
    工欲善其事必先利其器之搜索引擎
    营销自我
    java必备技能
    离线安装ADT和sdk
    eclipse的小技巧
    匿名内部类
  • 原文地址:https://www.cnblogs.com/huangtiandi001/p/7816013.html
Copyright © 2011-2022 走看看