• 超大型文件传输方案 + socket + subprocess popen 远程执行系统命令


     

    server 端

    import socket
    import subprocess
    import struct
    import json
    
    server = socket.socket()
    server.bind(("127.0.0.1",8080))
    server.listen(5)
    
    while True:
        conn, addr = server.accept()
        print(addr)
    
        while True:
            try:
                cmd = conn.recv(1024)
                obj = subprocess.Popen(cmd.decode(),
                                 shell=True,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE
                                 )
                stdout = obj.stdout.read()
                stderr = obj.stderr.read()
    
                header_dic = {
                    "total_size":len(stdout) + len(stderr),
                    "md5" : "password",
                    "filename" : "a.txt"
                }
                header_json = json.dumps(header_dic)
                header_bytes = header_json.encode("utf-8")
    
                header_size = len(header_bytes)
    
                header = struct.pack("i", header_size)
                conn.send(header)
    
                conn.send(header_bytes)
                conn.send(stdout+stderr)
    
            except ConnectionResetError:
                break
    
        conn.close()
    server.close()

     

     

    client 端

    
    
    import json
    import struct
    from socket import *
    
    client = socket(AF_INET,SOCK_STREAM)
    client.connect(('127.0.0.1',8080))
    
    while True:
    
        cmd = input(">>:").strip()
        if not cmd : continue
    
        client.send(cmd.encode())
    
        header_size = struct.unpack("i",client.recv(4))[0]
        header_bytes = client.recv(header_size)
    
        header_json = header_bytes.decode()
    
        header_dic = json.loads(header_json)
        total_size = header_dic["total_size"]
    
        recv_size = 0
        res = b""
        while recv_size < total_size:
            recv_data = client.recv(1024)
            res += recv_data
            recv_size += len(recv_data)
    
        print(res.decode("gbk"))
    
    client.close()
    
    
    
     

    cmd:   

            dir | ipconfig

  • 相关阅读:
    POJ 2348 Euclid's Game【博弈】
    POJ 2484 A Funny Game【博弈】
    HDU 4193 Non-negative Partial Sums【单调队列】
    占坑补题
    Codeforces 658D Bear and Polynomials【数学】
    Codeforces 658C Bear and Forgotten Tree 3【构造】
    Codeforces 658B Bear and Displayed Friends【set】
    POJ 1704 Georgia and Bob【博弈】
    1001. A+B Format

  • 原文地址:https://www.cnblogs.com/max404/p/11962707.html
Copyright © 2020-2023  润新知