【问题标题】:Python change socket window titlePython更改套接字窗口标题
【发布时间】:2018-09-01 19:11:13
【问题描述】:

当我使用 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);
    }
}

【问题讨论】:

标签: python sockets title putty


【解决方案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")
    ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-08
    • 2014-01-16
    相关资源
    最近更新 更多