Python更改套接字窗口标题

时间:2018-09-01 19:11:13

标签: python sockets title putty

当我使用PuTTY连接到服务器时,窗口会显示“ DBA-LT2017-PuTTY”

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

这是我到目前为止的代码

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的情况下更改会话标题

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

1 个答案:

答案 0 :(得分:1)

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")
    ...

enter image description here