Skip to content
The Chat Archive
gpt-4o-2024-08-06· OpenAI WildChat-4.8M (AI2)

import time import subprocess import ctypes, sys script = """powershell.exe…

  1. User

    import time import subprocess import ctypes, sys

    script = """powershell.exe wgListenPort=52528wgListenPort = 52528 wgIP = "162.159.193.5" wgPORT=2408wgPORT = 2408 EndPoints = New-Object System.Net.IPEndPoint([System.Net.IPAddress]::Parse([System.Net.Dns]::GetHostAddresses(wgIP)),wgIP)), wgPORT) Socket=New−ObjectSystem.Net.Sockets.UDPClientSocket = New-Object System.Net.Sockets.UDPClient wgListenPort SendMessage=SendMessage = Socket.Send([Text.Encoding]::ASCII.GetBytes(":)"), 2, EndPoints)EndPoints) 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', 'wgListenPort=52528\nwgListenPort = 52528\nw...>

  2. 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 when shell=True is used, it needs to be formatted correctly, so that it is executed properly by Popen. 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:

    1. Script Formatting: The script is assigned as a multi-line string to improve readability.
    2. Use of -Command: When passing the script to PowerShell from Python, you should use -Command as a parameter for PowerShell to execute the script correctly.
    3. Error Handling: Print both stdout and stderr outputs for better error tracking.
    4. 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.