The Python standard library includes the tkinter
module (Tkinter in Python 2), and you can access the X clipboard through that.
python3 -c 'import sys, tkinter; sys.stdout.write(tkinter.Tk().clipboard_get())'
python2 -c 'import sys, Tkinter; sys.stdout.write(Tkinter.Tk().clipboard_get())'
You can also set the clipboard content, but that's a bit more invasive, because the clipboard content is owned by a running application. Programs like xsel
leave a background process running while they own the clipboard. Here's a simple code fragment that you need to keep running until you're no longer interested in the content; close the window when you're done.
echo -n 'new clipboard content' | python3 -c 'import tkinter; w = tkinter.Tk(); w.clipboard_clear(); w.clipboard_append(input()); w.mainloop()'
echo -n 'new clipboard content' | python2 -c 'import Tkinter, sys; w = Tkinter.Tk(); w.clipboard_clear(); w.clipboard_append(sys.stdin.read()); w.mainloop()'
(There are better ways to access the clipboard in Python, such as pyperclip which is cross-platform and doesn't require displaying a window, but they aren't part of the standard library.)
xsel
is a native Linux program, why don't you want to use that? And what Linux distribution are you using?xsel
. That's whyxsel
exists, after all.