Python 更改套接字 window 标题

Python change socket window title

当我使用 PuTTY 连接到服务器时,window 显示 "DBA-LT2017 - PuTTY"

如何更改 PuTTY window 的标题? (不是控制台应用程序)

这是我目前的代码

import socket
import threading
from thread import start_new_thread

connect = ""
conport = 8080

def clientThread(conn):
    while True:
        message = conn.recv(512)

        if message.lower().startswith("quit"):
            conn.close()

        if not message:
            break

def startClient():
    host = connect
    port = conport
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind((host, port))
    sock.listen(1)
    print("[+] Server Started")
    while True:
        conn, addr = sock.accept()

        start_new_thread(clientThread, (conn,))
    sock.close()

client = threading.Thread(target=startClient)
client.start()

我看到一个用 C 编写的脚本,它使用 TCP 并且可以在没有 telnet 的情况下更改 session 标题

void *titleWriter(void *sock)
{
    int thefd = (int)sock;
    char string[2048];
    while(1)
    {
        memset(string, 0, 2048);
        sprintf(string, "%c]0;Test", '3', '[=13=]7');
        if(send(thefd, string, strlen(string), MSG_NOSIGNAL) == -1) return;
        sleep(2);
    }
}

PuTTY 理解 ANSI escape sequences

设置控制台标题的转义序列是:

ESC ] 0;this is the window title BEL

在Python即:

def clientThread(conn):
    conn.send("\x1b]0;this is the window title\x07")
    ...