import time import subprocess import ctypes, sys script = """powershell.exe…
- User
import time import subprocess import ctypes, sys
script = """powershell.exe wgIP = "162.159.193.5" EndPoints = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Parse([System.Net.Dns]::GetHostAddresses(wgPORT) wgListenPort Socket.Send([Text.Encoding]::ASCII.GetBytes(":)"), 2, Socket.Close()"""
def main(): while True: try: print(subprocess.Popen(script, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, creationflags=0x08000000)) except Exception as error: print(error) else: time.sleep(5)
if name == "main": main()
Исправь эту ошибку : <Popen: returncode: None args: ['powershell.exe', 'w...>
- Assistant
The error message you are seeing is likely due to how you're passing the PowerShell script to
subprocess.Popen. The script is a string, and whenshell=Trueis used, it needs to be formatted correctly, so that it is executed properly byPopen. Let's correct the script execution and handle the PowerShell script better. Here is how you can do it:import time import subprocess # Corrected PowerShell script that is properly formatted. script = """ $wgListenPort = 52528 $wgIP = "162.159.193.5" $wgPORT = 2408 $EndPoints = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Parse([System.Net.Dns]::GetHostAddresses($wgIP)), $wgPORT) $Socket = New-Object System.Net.Sockets.UdpClient $wgListenPort $SendMessage = $Socket.Send([Text.Encoding]::ASCII.GetBytes(":)"), 2, $EndPoints) $Socket.Close() """ def main(): while True: try: # Use '-Command' to pass the script to PowerShell. process = subprocess.Popen( ["powershell.exe", "-Command", script], stdout=subprocess.PIPE, stderr=subprocess.PIPE ) stdout, stderr = process.communicate() # Get the output and error print(stdout.decode('utf-8')) if stderr: print(stderr.decode('utf-8')) except Exception as error: print(error) finally: time.sleep(5) if __name__ == "__main__": main()Key Changes:
- Script Formatting: The script is assigned as a multi-line string to improve readability.
- Use of
-Command: When passing the script to PowerShell from Python, you should use-Commandas a parameter for PowerShell to execute the script correctly. - Error Handling: Print both
stdoutandstderroutputs for better error tracking. - Proper Process Handling: Use
communicate()to properly wait for the process to end and capture its output.
Ensure PowerShell is installed and accessible from the environment where you are running this Python script.