os.popen vs os.system

R

rbt

Is it more appropriate to use os.popen or os.system on a windows XP
client? I'm calling the operting system's shutdown function like this:

restart = os.popen(shutdown -r -f)
print restart.read()
restart.close()

If it matters, I'm doing this remotely over sockets.
 
P

Peter Hansen

rbt said:
Is it more appropriate to use os.popen or os.system on a windows XP
client? I'm calling the operting system's shutdown function like this:

restart = os.popen(shutdown -r -f)
print restart.read()
restart.close()

I don't think there's any significant difference between the above
(assuming you add in the missing quotation marks) and
os.system('shutdown -r -f'), except that the os.system() approach is
shorter and more readable if you are simply discarding or printing the
output like that.
> If it matters, I'm doing this remotely over sockets.

You can't be doing exactly that, since there are no sockets involved...
do you mean the above call is executed -- on the receiving end -- after
some signal is received via a socket in another part of the code?
That's not going to have any effect on things...

-Peter
 
F

F. Petitjean

Le Tue, 17 May 2005 13:50:08 -0400, rbt a écrit :
Is it more appropriate to use os.popen or os.system on a windows XP
client?

Nope. use the subprocess module :)

Microsoft had the great idea to embed white space inside a lot of
directories (compare C:\Program Files\ to /usr/bin ) which means that
you must find the right way to quote the command line string of
os.system() and this quoting is very tricky and under-documented. A
nightmare to thousands of developers which has a cost of millions of
dollars :-(

Exercice for the reader : write a windows application which can only be
invoked from the command line (or from another program) not by clicking
on a cute icon. Extra bonus points if the path to this executable
contain (after Program Files) a company name with some characters hard
to type on some keyboards (or with some locales), if the executable name
has also difficult to type characters and if the case is checked
(windows file system is case preserving).
 
R

rbt

Peter said:
> You can't be doing exactly that, since there are no sockets involved...
do you mean the above call is executed -- on the receiving end -- after
some signal is received via a socket in another part of the code? That's
not going to have any effect on things...

Here's the client portion:

import socket
import time

# Prompt user for host IP and port number.
host = raw_input("What IP: ")
port = raw_input("What port: ")

# Strip any whitespace and make sure port is an int.
host = str.strip(host)
port = str.strip(port)
port = int(port)

def msg():
msg = raw_input("Enter command to send to the server: ")
msg = str.strip(msg)
print "\nYou sent:", msg
return msg

def send(msg):
## Send the data to the server.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.send(msg)
s.close()

send(msg())
 
R

rbt

Peter said:
You can't be doing exactly that, since there are no sockets involved...
do you mean the above call is executed -- on the receiving end -- after
some signal is received via a socket in another part of the code? That's
not going to have any effect on things...

Here's the server portion:

from email.MIMEText import MIMEText
import email.Utils
import os
import smtplib
import socket
import sys
import time

def get_server_ip():
server = socket.gethostbyname(socket.gethostname())
return server

def set_server_port():
## Using "port = 0" will cause a random port to be chosen
port = 0
return port

def listen(ip_param, port_param):

def send_params(ip_param, port_param):
## This function will send the network parameters.
## Change the to and from email addys.
f = "XXX<[email protected]>"
t = "(e-mail address removed)"
msg = MIMEText("""Server IP: %s\nServer Port: %s"""
%(ip_param,port_param))

msg["Subject"] = "%s Listening" %socket.gethostname()
msg["Message-id"] = email.Utils.make_msgid()
msg["From"] = f
msg["To"] = t

h = "smtp.vt.edu"
s = smtplib.SMTP(h)
s.sendmail(f, t, msg.as_string())
s.quit()

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((ip_param, port_param))
except socket.error:
sys.exit("IP & Port Already in use.")
ip, port = s.getsockname()
print "Server", ip, "is listening for connections on port", port
send_params(ip, port)
while 1:
print "\nWaiting for new connection...\n"
s.listen(1)
conn, addr = s.accept()
print "Client", addr[0], "connected and was directed to port",
addr[1]
data = conn.recv(1024)
print "Client sent this message:", data
if data == 'shutdown -r -f':
print data
## restart = os.popen('shutdown -r -f')
## print restart.read()
## restart.close()
elif data == 'shutdown -a':
print data
## abort = os.popen('shutdown -a')
## abort.read()
## abort.close()
else:
print "No valid command received."
conn.close()

# Start the Program

ip = get_server_ip()
port = set_server_port()

listen(ip, port)
 
R

rbt

Peter said:
I don't think there's any significant difference between the above
(assuming you add in the missing quotation marks) and
os.system('shutdown -r -f'), except that the os.system() approach is
shorter and more readable if you are simply discarding or printing the
output like that.

Thanks Peter!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,239
Messages
2,571,200
Members
47,838
Latest member
elibuskamoSeAve

Latest Threads

Top