Reputation: 79
I'm doing a python program (and using PyQt5 for GUI) that needs to be run as root (because I'm programming sockets on it). It has a button,when I click on it, it opens another python file (the "child" file: chrometest.py
, it's based on this library, eel: https://www.youtube.com/watch?v=2kbeBzEQfXE, it lets me open a js file). The problem is that eel won't work when it's run as root, so I don't know how I could switch users to run only this function as regular user.
Main python program (sample, the one runing as root)
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QMainWindow):
def __init__(self, *args):
super(Window, self).__init__()
self.img = QtWidgets.QLabel()
self.open_js= QtWidgets.QPushButton('Load')
self.width = 400
self.height = 150
self.init_ui()
def init_ui(self):
self.img.setPixmap(QtGui.QPixmap("someimage.png"))
self.open_js.clicked.connect(self.openjs)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
h_layout = QtWidgets.QHBoxLayout(central_widget)
h_layout.addWidget(self.img)
h_layout.addWidget(self.open_js)
self.setWindowTitle('Main Window')
self.setGeometry(600,150,self.width,self.height)
def openjs(self):
#here is where I think I need to switch to regular user
exec(open("chrometest.py").read())
def main():
app = QtWidgets.QApplication(sys.argv)
main = Window()
main.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
chrometest.py (program that needs to be run as regular user)
import eel
eel.init('webfiles')
eel.start('index.html')
I tried to use this tutorial https://www.tutorialspoint.com/python/os_chown.htm in the line commented but it didn't work
Upvotes: 0
Views: 66
Reputation: 312
Like Mani Kandan said, you can run process by os.system
, but if you already working under root, it will be executed with same privileges.
You can run program under specified user using sudo -u
or sudo --user
argument like
sudo -u regular_user "python3 chrometest.py"
So, in Python it will look like:
import os
os.system('sudo -u regular_user python3 chrometest.py')
and about your TabError: inconsistent use of tabs and spaces in indentation
error: change all tabs in your script to spaces or vice versa.
Upvotes: 0
Reputation: 149075
It is common in Unix-like systems to have a process that needs to be root at a time, for example to listen on ports below 1024, but then executes as a regular non priviledges user for security reasons. Whatever the language the design is as follow:
That is what you should do here:
def openjs(self):
pid = os.fork()
if 0 == pid:
os.setuid(uid_of_non_priviledged_user)
# you can now safely execute code from chrometest.
else:
# optionaly wait for child:
os.waitpid(pid, os.WEXITED)
Upvotes: 1
Reputation: 570
oh yes!!.. you can able to run a child python file as a regular python file.but u need to add this line in your parent python file.
import os
os.system("python chrometest.py")
by this line your parent python file can run your child python file.
Upvotes: 0