0

I am creating an Ubuntu App using quickly on ubuntu 12.10. Its a simple GUI for starting, stopping and restarting Apache2 web server.

Let me first give the part of the code in which I am facing problem -

    # handler for restart apache button    
    def on_button_restart_clicked(self, button):
        self.label = self.builder.get_object("labelStatus")
        self.label.set_text("Restarting web server apache2")
        os.system("gksudo /etc/init.d/apache2 restart")
        self.label.set_text("Web server apache2 Restarted")

As soon as the button is clicked the method is invoked but the label is not showing - Restarting web server apache2 In the terminal the output at this time is - * Restarting web server apache2 [ OK ] ... waiting and as soon as the apache is restared the next line is displayed which is - Web server apache2 Restarted

How can I solve the issues -

  1. I don't want to hard code the message in the label text. So how can I keep a track on the terminal output and capture it to the python variable so that I can set the label text.
  2. Since I am using gksudo the popup is coming to enter the password, but the problem is that it is showing the command. How can I gracefully use sudo in python?

I am totally new to python. Thanks in advance

Abhishek Prakash
  • 201
  • 1
  • 3
  • 11

1 Answers1

0

Quick check of Stack brought me to this link; hope it helps

Pipe subprocess standard output to a variable

I also dug this up:

from StringIO import StringIO 
import sys

# store a reference to the old
old_stdout = sys.stdout

# var to store everything that is sent to the std out
result = StringIO()
sys.stdout = result

# your function here; all results should be stored
user_def_function()

# reset the std out
sys.stdout = old_stdout

# result to string
result_string = result.getvalue()
Community
  • 1
  • 1
crownedzero
  • 496
  • 1
  • 7
  • 18