• 每日作业:4/20


     1、

    方法一:

    服务端:os.popen

    import socket
    import os
    iphone=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    iphone.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
    iphone.bind(("127.0.0.1",9000))
    iphone.listen(5)
    while True:
        conn,addr=iphone.accept()
        print('接到来自%s的电话' % addr[0])
        while True:
            try:
                cmd=conn.recv(1024)
                if not cmd:
                    print("客户端:{} 已经关闭连接".format(addr[0]))
                    break
                print("客户端命令:{}".format(cmd.decode("utf-8")))
                cmd_stat=os.system(cmd.decode("utf-8"))
                # print(cmd_stat)
                if not cmd_stat:
                    cmd_data=os.popen(cmd.decode("utf-8")).read()
                    conn.send(cmd_data.encode("utf-8"))
                else:
                    conn.send("命令不存在".encode("utf-8"))
    
            except Exception:
                print("客户端:{} 非正常关闭连接".format(addr[0]))
                break
        conn.close()
    iphone.close()

    客户端:

    import socket
    iphone=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    
    iphone.connect(("127.0.0.1",9000))
    while True:
        msg=input("请输入命令:>>>").strip()
        if len(msg) == 0: continue
        if msg == "exit()":
            iphone.close()
            break
    
        else:
            iphone.send(msg.encode("utf-8"))
            data=iphone.recv(1024)
            print(data.decode("utf-8"))
    
    iphone.close()

     方法二:(不存在的命令)

    import socket
    import subprocess
    
    phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    
    phone.bind(("127.0.0.1",9000))
    phone.listen(5)
    
    #循环连接
    while True:
        conn,addr=phone.accept()
        # print(conn,addr)
    
        #循环通信
        while True:
            try:
                cmd=conn.recv(1024)
                if not cmd:
                    print("客户端:{} 已经断开连接".format(addr[0]))
                    break
                print("命令名称:%s" %cmd.decode("utf-8"),)
                obj = subprocess.Popen(cmd.decode("utf-8"), shell=True,
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE,
                                       )
                obj.wait()
                cmd_stat=subprocess.Popen.poll(obj)
                if not cmd_stat:
    
                    cmd_data = obj.stdout.read()
                    if not cmd_data:conn.send("命令:{} 执行成功".format(cmd.decode("utf-8")).encode("gbk"))
                    print(cmd_data.decode("gbk"))
                    conn.send(cmd_data)
                else:
                    err_data = obj.stderr.read()
                    conn.send(err_data)
            except Exception:
                print("客户端:{} 非正常退出".format(addr[0]))
                break
        conn.close()
    phone.close()
    server
    import socket
    
    phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    
    phone.connect(("127.0.0.1",9000))
    
    #循环通信
    while True:
        cmd=input("请输入命令:>>>").strip()
        if not cmd:continue
        if cmd == "exit()":
            phone.close()
            break
        phone.send(cmd.encode("utf-8"))
    
        cmd_data=phone.recv(1024)
        print(cmd_data.decode("gbk"))
    
    phone.close()
    client
  • 相关阅读:
    JavaScript中的闭包
    SQL 备忘
    SqlServer 2005 升级至SP2过程中出现"身份验证"无法通过的问题
    unable to start debugging on the web server iis does not list an application that matches the launched url
    Freebsd 编译内核
    Freebsd 6.2中关于无线网络的设定
    【Oracle】ORA01219
    【Linux】Windows到Linux的文件复制
    【Web】jar命令行生成jar包
    【Linux】CIFS挂载Windows共享
  • 原文地址:https://www.cnblogs.com/baicai37/p/12737872.html
Copyright © 2020-2023  润新知