How to safely clear both clipboards in Gnome, from Python?

The Gnome desktop has 2 clipboards, X.org (saves every selection) and inherited (CTRL + C). I am writing a simple python script to clear both clipboards, reliably, as this can be done after copying the password.

The code I saw here is as follows:

# empty X.org clipboard
os.system("xclip -i /dev/null")  

# empty GNOME clipboard
os.system("touch blank")  
os.system("xclip -selection clipboard blank")  

Unfortunately, this code for some reason creates a file with a name blank, so we need to delete it:

os.remove("blank")

However, the main problem is that by invoking both of these scenarios, it leaves the process xclipopen, even after closing the terminal.

So, we have two problems with this option:

1) It creates an empty file, which seems to me an incorrect method

2) It leaves the process open, which could be a security hole.

:

 os.system("echo "" | xclip -selection clipboard")  # empty clipboard

\n newline , .

, ?

+4
3

:

#CLIPBOARD cleaner
subprocess.run(["xsel","-bc"])

#PRIMARY cleaner
subprocess.run(["xsel","-c"])

. , .

0

  • GNOME " "; X11 . , PRIMARY CLIPBOARD. "".
  • "" ( - , ), . ( ) X, ( ) . ( , ​​ .)
  • xclip , , . , , , - /, , , .
  • os.system ( system ), , (, ! in less). ( , /bin/sh), ( ) , , a href= "/questions/1239161/perl-forward-sigint-to-parent-process-from-system-command" > , , .

  • , Python Xlib, . , , - .
  • Tkinter, , (Tk ), .
  • xclip xsel, , ( Ubuntu, ). Python, subprocess; Python 3.5

    subprocess.run("xclip",stdin=subprocess.DEVNULL)
    subprocess.run(["xclip","-selection","clipboard"],input="")
    subprocess.run(["xsel","-c"])
    

    ( stdin input , .) xsel --clear, .

.

+3

I know three ways to clear the clipboard from Python. First use tkinter:

try:
    from Tkinter import Tk
except ImportError:
    from tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.destroy()

Secondly, with xclip, but I use xclip as follows:

echo -n | xclip -selection clipboard

Does it create a new line?

Finally, this is possible for user xsel:

xsel -bc
+2
source

Source: https://habr.com/ru/post/1692847/