Skip to content

Python3 - String-Operatoren

Python3
  • Bei meinem Restic Python3 Projekt möchte ich die Funktion

    --exclude-file=
    

    mit einbauen. Meine ersten Versuche

    process = subprocess.run(['restic','-r',backups[liste2[row]]['repository'],'backup',backups[liste2[row]]['source'],'--exclude-file=',(backups[liste2[row]]['exclude'])],...
    

    Das geht aber nicht, da schmeißt es ständig

    open  : no such file or directory
    

    Ok, was machen? Da es ja auf der Konsole ganz normal funktioniert.

    restic -r /home/frank/restic_test/ backup /home/frank/Bilder --exclude-file=excludes.txt
    

    Ok, ich muss das irgendwie als String zusammenfassen. Nach einiger Zeit habe ich dann das hier gefunden.

    process = subprocess.run(['restic','-r',backups[liste2[row]]['repository'],'backup',backups[liste2[row]]['source'],'--exclude-file=%s' % (backups[liste2[row]]['exclude'])],
    

    Der entscheidende Teil

    '--exclude-file=%s' % (backups[liste2[row]]['exclude'])]
    

    Hier wird an die Stelle, wo der Pfad reinkommt, ein Platzhalter gesetzt %s Den füllen wir dann mit der Variable die in den Klammern dahinter steht. Jetzt funktioniert es 🙂

    Würde auch mit mehreren Variablen gehen.

    subprocess('Programm %s %s' % (option1, option2))
    

    Quelle: https://www.python-forum.de/viewtopic.php?t=4434

  • Python - mehrsprachiges GUI

    Python3 python3
    1
    1
    0 Stimmen
    1 Beiträge
    206 Aufrufe
    Niemand hat geantwortet
  • Restic UI - QtWaitingSpinner unexpected type 'float'

    Restic UI restic-ui python3
    2
    0 Stimmen
    2 Beiträge
    306 Aufrufe
    FrankMF
    Der Autor hat meine Anpassungen um ein paar Änderungen erweitert und in sein Repo eingepflegt. Der QtWaitingSpinenr ist jetzt auch 3.10 kompatibel [image: 1641634993220-1b268980-92ca-42a4-89a0-a6e4b7ab9378-grafik.png]
  • Restic UI - QThread

    Restic UI restic-ui python3
    1
    0 Stimmen
    1 Beiträge
    154 Aufrufe
    Niemand hat geantwortet
  • Restic UI - Changelog

    Angeheftet Restic UI restic-ui python3 pyqt5
    3
    0 Stimmen
    3 Beiträge
    364 Aufrufe
    FrankMF
    v1.5.0 - Release for restic v0.14.0 with compression and migration tool
  • Python3 - QT_QPA_PLATFORM

    Python3 pyqt5 python3
    2
    2
    0 Stimmen
    2 Beiträge
    405 Aufrufe
    FrankMF
    Nachdem ich ja mittlerweile schon lange weder auf Cinnamon bin, hatte ich nach einem Update von VSCodium wieder ein anderes Design. Ok, schauen wir rein. (venv) frank@frank-MS-7C37:~/restic-ui-public$ env | grep "QT" QT_ACCESSIBILITY=1 QT_QPA_PLATFORMTHEME=qt5ct Wenn ich das auf QT_QPA_PLATFORMTHEME=gnome3 ändere, sieht es wie auf meinem Linux Mint Cinnamon Desktop aus.
  • Python3 - subprocess.run

    Python3 python3
    2
    0 Stimmen
    2 Beiträge
    277 Aufrufe
    FrankMF
    Ich möchte das Thema noch mal ausgraben. Bin beim Recherchieren über diese Links gestolpert, die mein Interesse geweckt haben. https://security.openstack.org/guidelines/dg_avoid-shell-true.html https://bandit.readthedocs.io/en/latest/plugins/b603_subprocess_without_shell_equals_true.html Da ich aktuell nur eine Desktop Anwendung entwickle, ist das Thema Sicherheit nicht ganz so wichtig, weil wer hackt sich schon selber!? Aber, da man ja nie weiß, wie so ein Tool evt. mal benutzt wird, sollte man von Anfang an auf ein paar Dinge achten. Ok, schauen wir uns das mal genauer an. Link 1 - shell=True Ich kopiere mal das Beispiel aus dem Link. def count_lines(website): return subprocess.check_output('curl %s | wc -l' % website, shell=True) Ok, das Problem ist shell=True Ein Beispiel aus meinem Projekt result = subprocess.run(['restic', '-r', backup_data[row].repository, 'stats'], input=pass_word.pw[0], capture_output=True, text=True) Gesetzt wird der in meinem Beispiel nicht. Besuchen wir mal die Webseite vom subprocess.run https://docs.python.org/3/library/subprocess.html?highlight=subprocess run#subprocess.run Wenn ich das jetzt richtig verstehe, subprocess.run(args, *, stdin=None, input=None, stdout=None, stderr=None, capture_output=False, shell=False, cwd=None, timeout=None, check=False, encoding=None, errors=None, text=None, env=None, universal_newlines=None, **other_popen_kwargs) dann ist standardmäßig shell=False gesetzt. Damit ist das in meinem Projekt kein Problem. Link 2 - B603: subprocess_without_shell_equals_true Ein Tool auf gitlab.com wirft Security Warnings aus, dabei war diese. Schauen wir mal, was uns das sagen möchte. Python possesses many mechanisms to invoke an external executable. However, doing so may present a security issue if appropriate care is not taken to sanitize any user provided or variable input. Ok, es geht also um die Prüfung von Eingaben bzw. Variablen. Der Merksatz "Keine Benutzereingabe wird ungeprüft übernommen!" ist doch mit das Wichtigste, wenn man irgendwas programmiert. Nochmal mein Beispiel von oben. result = subprocess.run(['restic', '-r', backup_data[row].repository, 'stats'], input=pass_word.pw[0], capture_output=True, text=True) Ich übergebe dem Prozess einige Eingaben / Variablen. backup_data[row].repository pass_word.pw[0] Lesen wir wieder ein wenig in der Dokumentation von subprocess.run args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments. Das Wichtigste in Kürze Providing a sequence of arguments is generally preferred Der Aufruf von subprocess.run erwartet als erste Übergabe args subprocess.run(args, *, .... Mein Beispiel result = subprocess.run(['restic', '-r', backup_data[row].repository, 'stats'], .... Das zwischen den eckigen Klammern ist args. Laut der Anleitung ist es empfohlen, das als ein Argument zu übergeben, also so. Somit ist dafür gesorgt, das das Modul die Argumente selbst ein wenig "überwacht". as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). Somit hätten man schon mal was für sie "Sicherheit" getan. args = ['restic', '-r', backup_data[row].repository, 'stats'] result = subprocess.run(args, .... Man sollte trotzdem auf diese beiden Variablen * backup_data[row].repository * pass_word.pw[0] ein Auge behalten. Ich denke, das habe ich in meinem Projekt eingehalten, indem ich Pfadangabe mit den Systemwerkzeugen auswählbar mache, keine Texteingaben! Passwörter und andere Bezeichnung werden mit regex auf korrekte Eingaben geprüft usw. Fazit Wieder viel gelernt und ich denke, es passt so weit alles. Bei der ganzen Spielerei, dann noch entdeckt, das ich im Code dieses Argument drin hatte. check=False Das ist aber Standard, somit kann das weg. Habe das im kompletten Projekt dann entfernt und mir fällt gerade auf, da der Code immer gleich ist, muss das jetzt eigentlich alles in eine Funktion Mal auf die ToDo-Liste drauf schreiben.
  • Python3 - virtuelle Entwicklungsumgebung

    Angeheftet Python3 python3
    3
    0 Stimmen
    3 Beiträge
    425 Aufrufe
    FrankMF
    Nach Systemwechsel erneut Probleme. Hier noch mal aufgelistet. Aufpassen, das kein venv Ordner vorhanden ist! Neu anlegen! python3 -m venv venv Dann meckert mein Linux Mint Cinnamon The virtual environment was not created successfully because ensurepip is not available. On Debian/Ubuntu systems, you need to install the python3-venv package using the following command. apt install python3.8-venv You may need to use sudo with that command. After installing the python3-venv package, recreate your virtual environment. Failing command: ['/home/frank/Restic_UI_Produktiv/restic-ui-public/venv/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip'] Ok, das ist einfach apt install python3.8-venv python3 -m venv venv Danach aktivieren source venv/bin/activate Installation von PyQt5 (venv) frank@frank-MS-7C37:~/Restic_UI_Produktiv/restic-ui-public$ pip3 install PyQt5 Collecting PyQt5 Downloading PyQt5-5.15.4-cp36.cp37.cp38.cp39-abi3-manylinux2014_x86_64.whl (8.3 MB) |████████████████████████████████| 8.3 MB 1.7 MB/s Collecting PyQt5-Qt5>=5.15 Downloading PyQt5_Qt5-5.15.2-py3-none-manylinux2014_x86_64.whl (59.9 MB) |████████████████████████████████| 59.9 MB 18.3 MB/s Collecting PyQt5-sip<13,>=12.8 Downloading PyQt5_sip-12.9.0-cp38-cp38-manylinux1_x86_64.whl (332 kB) |████████████████████████████████| 332 kB 46.1 MB/s Installing collected packages: PyQt5-Qt5, PyQt5-sip, PyQt5 Successfully installed PyQt5-5.15.4 PyQt5-Qt5-5.15.2 PyQt5-sip-12.9.0 Danach meckert er über ein fehlendes Modul requests (venv) frank@frank-MS-7C37:~/Restic_UI_Produktiv/restic-ui-public$ python3 restic_ui.py Traceback (most recent call last): File "restic_ui.py", line 41, in <module> from functions import ( File "/home/frank/Restic_UI_Produktiv/restic-ui-public/functions.py", line 19, in <module> import requests as req ModuleNotFoundError: No module named 'requests' Installation requests (venv) frank@frank-MS-7C37:~/Restic_UI_Produktiv/restic-ui-public$ pip3 install requests Collecting requests Downloading requests-2.25.1-py2.py3-none-any.whl (61 kB) |████████████████████████████████| 61 kB 802 kB/s Collecting chardet<5,>=3.0.2 Downloading chardet-4.0.0-py2.py3-none-any.whl (178 kB) |████████████████████████████████| 178 kB 2.7 MB/s Collecting certifi>=2017.4.17 Downloading certifi-2021.5.30-py2.py3-none-any.whl (145 kB) |████████████████████████████████| 145 kB 14.0 MB/s Collecting idna<3,>=2.5 Downloading idna-2.10-py2.py3-none-any.whl (58 kB) |████████████████████████████████| 58 kB 9.4 MB/s Collecting urllib3<1.27,>=1.21.1 Downloading urllib3-1.26.6-py2.py3-none-any.whl (138 kB) |████████████████████████████████| 138 kB 11.5 MB/s Installing collected packages: chardet, certifi, idna, urllib3, requests Successfully installed certifi-2021.5.30 chardet-4.0.0 idna-2.10 requests-2.25.1 urllib3-1.26.6 Und schwupps, geht mein Tool wieder [image: 1625848253488-4968ce42-7c66-4c8a-a2ad-424b9a529d87-grafik.png]
  • Wireguard UI - mein erstes Python3 Projekt

    Python3 pyqt5 python3
    2
    3
    0 Stimmen
    2 Beiträge
    294 Aufrufe
    FrankMF
    Dinge entwickeln sich. Es sieht alles schon wieder was anders aus Hauptfenster [image: 1600889374694-270b5c68-3d64-44ca-9a01-e3f24ba4a26c-grafik.png] Setup Fenster zum Erstellen der wg0.conf [image: 1600889412743-27c3cb28-50f5-4e00-b03d-de47c07372a0-grafik.png] Und alles funktioniert so weit