1

I've seen similar questions such as this one: keep multiple console windows open from batch.

However, I have a different situation. I do not want to run a different script in a different console window. My idea is to have socket running as a server and accepting all connections. When a connection is accepted, a new console window is created, and all in-coming and out-going data is shown there. Is that even possible?

7
  • Can provide some more information to understand the question better? Commented Oct 12, 2016 at 12:11
  • 2
    A process can only be attached to one console (i.e. instance of conhost.exe) at a time, and a console with no attached processes automatically closes. You would need to spawn a child process with creationflags=CREATE_NEW_CONSOLE. Commented Oct 12, 2016 at 14:43
  • Please read minimal reproducible example. Commented Oct 12, 2016 at 18:02
  • @eryksun from researches i made about your code that is exactly what I was looking for. If you would post this as an answer I will accept it Commented Oct 13, 2016 at 13:43
  • Will you be relaying data from the socket to sdtin of the child process? You can do that in the handler for a socketserver.ThreadingTCPServer. I could modify the code from the docs to demo that. Commented Oct 13, 2016 at 19:18

1 Answer 1

3

A process can only be attached to one console (i.e. instance of conhost.exe) at a time, and a console with no attached processes automatically closes. You would need to spawn a child process with creationflags=CREATE_NEW_CONSOLE.

The following demo script requires Windows Python 3.3+. It spawns two worker processes and duplicates each socket connection into the worker via socket.share and socket.fromshare. The marshaled socket information is sent to the child's stdin over a pipe. After loading the socket connection, the pipe is closed and CONIN$ is opened as sys.stdin to read standard input from the console.

import sys
import time
import socket
import atexit
import threading
import subprocess

HOST = 'localhost'
PORT = 12345

def worker():
    conn = socket.fromshare(sys.stdin.buffer.read())
    sys.stdin = open('CONIN$', buffering=1)
    while True:
        msg = conn.recv(1024).decode('utf-8')
        if not msg:
            break
        print(msg)
        conn.sendall(b'ok')
    input('press enter to quit')
    return 0

def client(messages):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((HOST, PORT))
        for msg in messages:
            s.sendall(msg.encode('utf-8'))
            response = s.recv(1024)
            if response != b'ok':
                break
            time.sleep(1)

procs = []

def server():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind((HOST, PORT))
        s.listen(1)
        while True:
            conn, addr = s.accept()
            with conn:
                p = subprocess.Popen(
                    ['python', sys.argv[0], '-worker'],
                    stdin=subprocess.PIPE, bufsize=0,
                    creationflags=subprocess.CREATE_NEW_CONSOLE)
                p.stdin.write(conn.share(p.pid))
                p.stdin.close()
                procs.append(p)

def cleanup():
    for p in procs:
        if p.poll() is None:
            p.terminate()

if __name__ == '__main__':
    if '-worker' in sys.argv[1:]:
        sys.exit(worker())

    atexit.register(cleanup)

    threading.Thread(target=server, daemon=True).start()

    tcli = []
    for msgs in (['spam', 'eggs'], ['foo', 'bar']):
        t = threading.Thread(target=client, args=(msgs,))
        t.start()
        tcli.append(t)

    for t in tcli:
        t.join()

    input('press enter to quit')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.