【问题标题】:json.decoder.JSONDecodeError: Unterminated string starting at: line 1 column 1 (char 0)json.decoder.JSONDecodeError:未终止的字符串开始于:第 1 行第 1 列(字符 0)
【发布时间】:2019-02-22 20:51:43
【问题描述】:

此错误仅在我执行 ipconfig 时发生。 Dir 和 mkdir 似乎都可以工作! 第一部分是发送命令的程序,另一部分是发送命令的程序,第二部分是连接到它的程序。

import socket
import json
from sys import exit

class Listener:
    def __init__(self, ip, port):
        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(1024)
        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 run(self):
        while True:
            command = input(">> ")
            command = command.split(" ")
            print(command)
            result = self.execute_remotely(command)
            print(result)

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

这是连接到它的程序:

import socket
import subprocess
import json
from sys import exit

class Connect:
    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(1024).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 run(self):
        while True:
            command = self.reliable_recieve()
            if command[0] == "exit":
                self.connection.close()
                exit()
            command_result = self.execute_sys_command(command)
            self.reliable_send(command_result)
        self.connection.close()

my_connection = Connect("127.0.0.1", 4444)
my_connection.run()

我不知道为什么会这样,这没有意义。我能想到的只是在返回命令结果时发生错误。我认为这是因为它与 dir 或 mkdir 没有任何不同,例如空格或编码方式。所以可能是在返回。

【问题讨论】:

  • 如果我不得不猜测,您的 ipconfig JSON 数据可能大于 1024 字节,因此当您尝试解析它时 - 它会中断。在解析之前从您的套接字中收集所有 JSON 数据。
  • 我该怎么做?
  • 您可以围绕您的self.connection.recv(1024) 代码执行while True: ... 循环并连接结果,直到没有更多数据 - 此时您可以对其进行解码,然后解析包含的 JSON。或者,对于更稳健的方法,请检查 this answer 并忽略 #4/#5 步骤,因为您没有添加任何有效负载。

标签: python python-3.x sockets


【解决方案1】:

我刚刚将self.connection.recv(1024) 更改为更大的数字,从而接受更多字节。正如@zwer 所提到的,问题是“ipconfig”命令的输出大于 1024 字节。

【讨论】:

    猜你喜欢
    • 2016-03-03
    • 2023-03-25
    • 2020-11-11
    • 2017-05-09
    • 2020-01-02
    • 2019-08-13
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多