PyQT terminal emulator


hide:

I am trying to develop a "console" in pyqt. Similar to where all consoles are in xterm.js, it's the front end, it doesn't spawn any child processes, it's just an I/O that lets me plug in whatever I want later.

Can I use any existing python packages or simple widgets and place them on a terminal like a pyqt application?

It's a client server application, so the terminal is used to send commands to the backend server and retrieve the output like a bash shell (for example)

eyllanesc:

You can use QTermWidget (if you can't install it and you're using ubuntu, you can check this answer ).

For example, here 's a translation of the official RemoteTerm example that allows remote access to the shell over sockets:

terminal.py

import os
import sys

from PyQt5 import QtCore, QtWidgets, QtNetwork

import QTermWidget


class RemoteTerm(QTermWidget.QTermWidget):
    def __init__(self, ipaddr, port, parent=None):
        super().__init__(0, parent)

        self.socket = QtNetwork.QTcpSocket(self)

        self.socket.error.connect(self.atError)
        self.socket.readyRead.connect(self.on_readyRead)
        self.sendData.connect(self.socket.write)

        self.startTerminalTeletype()
        self.socket.connectToHost(ipaddr, port)

    @QtCore.pyqtSlot()
    def on_readyRead(self):
        data = self.socket.readAll().data()
        os.write(self.getPtySlaveFd(), data)

    @QtCore.pyqtSlot()
    def atError(self):
        print(self.socket.errorString())


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)

    QtCore.QCoreApplication.setApplicationName("QTermWidget Test")
    QtCore.QCoreApplication.setApplicationVersion("1.0")

    parser = QtCore.QCommandLineParser()
    parser.addHelpOption()
    parser.addVersionOption()
    parser.setApplicationDescription(
        "Example(client-side) for remote terminal of QTermWidget"
    )
    parser.addPositionalArgument("ipaddr", "adrress of host")
    parser.addPositionalArgument("port", "port of host")

    parser.process(QtCore.QCoreApplication.arguments())

    requiredArguments = parser.positionalArguments()
    if len(requiredArguments) != 2:
        parser.showHelp(1)
        sys.exit(-1)

    address, port = requiredArguments
    w = RemoteTerm(QtNetwork.QHostAddress(address), int(port))
    w.resize(640, 480)
    w.show()
    sys.exit(app.exec_())

shell-srv.py

#!/usr/bin/env python

import sys
import os
import socket
import pty


def usage(program):
    print("Example(server-side) for remote terminal of QTermWidget.")
    print("Usage: %s ipaddr port" % program)


def main():
    if len(sys.argv) != 3:
        usage(sys.argv[0])
        sys.exit(1)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    try:
        s.bind((sys.argv[1], int(sys.argv[2])))
        s.listen(0)
        print("[+]Start Server.")
    except Exception as e:
        print("[-]Error Happened: %s" % e.message)
        sys.exit(2)

    while True:
        c = s.accept()
        os.dup2(c[0].fileno(), 0)
        os.dup2(c[0].fileno(), 1)
        os.dup2(c[0].fileno(), 2)

        # It's important to use pty to spawn the shell.
        pty.spawn("/bin/sh")
        c[0].close()


if __name__ == "__main__":
    main()

Related


PyQT terminal emulator

hide: I am trying to develop a "console" in pyqt. Similar to where all consoles are in xterm.js, it's the front end, it doesn't spawn any child processes, it's just an I/O that lets me plug in whatever I want later. Can I use any existing python packages or si

PyQT terminal emulator

hide: I am trying to develop a "console" in pyqt. Similar to where all consoles are in xterm.js, it's the front end, it doesn't spawn any child processes, it's just an I/O that lets me plug in whatever I want later. Can I use any existing python packages or si

Terminal Emulator vs Terminal

coding I'm having a hard time understanding the difference between running a terminal emulator from the GUI (gnome) and booting directly into the terminal. I've been a sys/network admin for over 10 years and I'm pretty familiar with terminal emulation and how

Terminal Emulator vs Terminal

coding I'm having a hard time understanding the difference between running a terminal emulator from the GUI (gnome) and booting directly into the terminal. I've been a sys/network admin for over 10 years and I'm pretty familiar with terminal emulation and how

Terminal Emulator vs Terminal

coding I'm having a hard time understanding the difference between running a terminal emulator from the GUI (gnome) and booting directly into the terminal. I've been a sys/network admin for over 10 years and I'm pretty familiar with terminal emulation and how

Linux terminal emulator with terminal autocomplete?

Yaniv Aknin I've migrated back to Linux from OSX and am happy most of the time. One of the main things I miss is iTerm2 , especially its terminal-based autocomplete (tmux integration is nice too). I looked around but couldn't find the equivalent table in Linux

Linux terminal emulator with terminal autocomplete?

Yaniv Aknin I've migrated back to Linux from OSX and am happy most of the time. One of the main things I miss is iTerm2 , especially its terminal-based autocomplete (tmux integration is nice too). I looked around but couldn't find the equivalent table in Linux

Writing a Linux Terminal Emulator

Non-chip: I want to write a x11 terminal emulator, but I don't know how to generate and communicate with the shell, is there any basic (pseudo or C) code? Things like what kind of PTY to create, how to bind the shell to the shell, what signals do I have to cat

Writing a Linux Terminal Emulator

Non-chip: I want to write a x11 terminal emulator, but I don't know how to generate and communicate with the shell, is there any basic (pseudo or C) code? Things like what kind of PTY to create, how to bind the shell to the shell, what signals do I have to cat

Java Terminal Emulator

Rima: Does anyone know of a library or class to emulate a vt100 terminal (graphical or not is irrelevant). What I basically want is a class that implements the logic of the vt100 terminal (e.g. the delete function will be called when a "delete" code is receive

Highlight output in terminal emulator

Benjamin Lindqvist Can't imagine I'm the first to wonder, but I'm really getting nowhere with this - maybe I'm just using the wrong search term. I'm looking for a terminal emulator that will allow me to use the keyboard to navigate through the output, just lik

Terminal emulator (eg Cathode)?

Pedro Rolo This is cool : http://osxdaily.com/2011/01/27/the-ultimate-retro-terminal-cathode/ Are there any similar applications for Ubuntu? Pedro Rolo I just stumbled across that terminologythis is the Terminal that comes with the Enlightenment Window Manager

Ubuntu X terminal emulator

stupid professor Whenever I press Ctrl+Alt+T, the X terminal emulator opens instead of Gnome Terminal. I do not know why. What is this x terminal emulator and what does it do? How is it different from Gnome Terminal? vanadium Totally possible workaround A very

CygWin terminal emulator?

Crocodile I have got: Windows 7 Segwyn The development board is connected to the COM1 port. I need: Connect to its serial port from CygWin. If I was using Linux, I would be running something like picocom or minicom . Also in Windows 7 I can install hypertermin

Is the terminal emulator process a server?

Tim Is the temporary emulator working on the server? If yes, what are its clients? How do I find its customers? thanks. netstatThe output contains the lxterminal process. Does this mean that the terminal emulator process is a UNIX domain socket based server? $

Change terminal emulator

Amelio Vazquez-Reina I can access the remote computer via ssh. On this machine, if I run, echo $TERMI get xterm. If I want to change the terminal emulator to another emulator (assuming it's installed on the remote computer), what should I do? Tim Kennedy The s

Is the terminal emulator process a server?

Tim Is the temporary emulator working on the server? If yes, what are its clients? How do I find its customers? thanks. netstatThe output contains the lxterminal process. Does this mean that the terminal emulator process is a UNIX domain socket based server? $

Writing a Linux Terminal Emulator

Non-chip: I want to write a x11 terminal emulator, but I don't know how to generate and communicate with the shell, is there any basic (pseudo or C) code? Things like what kind of PTY to create, how to bind the shell to the shell, what signals do I have to cat

Java Terminal Emulator

Rima: Does anyone know of a library or class to emulate a vt100 terminal (graphical or not is irrelevant). What I basically want is a class that implements the logic of the vt100 terminal (e.g. the delete function will be called when a "delete" code is receive

Highlight output in terminal emulator

Benjamin Lindqvist Can't imagine I'm the first to wonder, but I'm really getting nowhere with this - maybe I'm just using the wrong search term. I'm looking for a terminal emulator that will allow me to use the keyboard to navigate through the output, just lik

Terminal emulator (eg Cathode)?

Pedro Rolo This is cool : http://osxdaily.com/2011/01/27/the-ultimate-retro-terminal-cathode/ Are there any similar applications for Ubuntu? Pedro Rolo I just stumbled across that terminologythis is the Terminal that comes with the Enlightenment Window Manager

Change terminal emulator

Amelio Vazquez-Reina I can access the remote computer via ssh. On this machine, if I run, echo $TERMI get xterm. If I want to change the terminal emulator to another emulator (assuming it's installed on the remote computer), what should I do? Tim Kennedy The s

CygWin terminal emulator?

Crocodile I have got: Windows 7 Segwyn The development board is connected to the COM1 port. I need: Connect to its serial port from CygWin. If I was using Linux, I would be running something like picocom or minicom . Also in Windows 7 I can install hypertermin

Is the terminal emulator process a server?

Tim Is the temporary emulator working on the server? If yes, what are its clients? How do I find its customers? thanks. netstatThe output contains the lxterminal process. Does this mean that the terminal emulator process is a UNIX domain socket based server? $

Terminal emulator (eg Cathode)?

Pedro Rolo This is cool : http://osxdaily.com/2011/01/27/the-ultimate-retro-terminal-cathode/ Are there any similar applications for Ubuntu? Pedro Rolo I just stumbled across that terminologythis is the Terminal that comes with the Enlightenment Window Manager

Highlight output in terminal emulator

Benjamin Lindqvist Can't imagine I'm the first to wonder, but I'm really getting nowhere with this - maybe I'm just using the wrong search term. I'm looking for a terminal emulator that will allow me to use the keyboard to navigate through the output, just lik

How to Create a Terminal Emulator in Python

Shadur Naregav Tired of traditional cmdwindows so wanted to build one myself. Any tutorials, libraries etc would be helpful. Thanks in advance!:) Serge Ballesta A shell in the sense of a CLI command interpreter (bash or cmd.exe) can be independent of the opera

Is an online Ubuntu terminal emulator available?

username I'd like to use some terminal commands from the official Ubuntu packages, but currently don't have access to an Ubuntu PC. Is there a free online terminal emulator like Ubuntu 16.04 running? I've tried this step, but it seems to be very limited (i.e.

Start the default terminal emulator by command

Charlie How to start default terminal emulator via command? x-terminal-emulatorCan't work because I'm not using Debian and xdg-terminalit doesn't seem to exist either. Is there any other way to start the default terminal emulator? It will be on GNOME, although