473,386 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

os.popen vs os.system

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.
Jul 19 '05 #1
5 4510
rbt wrote:
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
Jul 19 '05 #2
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).
Jul 19 '05 #3
rbt
Peter Hansen wrote:
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())
Jul 19 '05 #4
rbt
Peter Hansen wrote:
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<xx*@somewhere.com>"
t = "so*****@somewhere.com"
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)

Jul 19 '05 #5
rbt
Peter Hansen wrote:
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!
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Sami Viitanen | last post by:
Hello, I was using os.popen function to get CVS command output to string from script.. Same commands worked well with Windows, but Linux seems to have problem with those. Whole CVS commands...
3
by: Grumble | last post by:
Hello all, Is there an iostream equivalent to cstdio's popen()? Is there a way to change a FILE * into an iostream?
3
by: Jesse | last post by:
Hi all, I have a problem using wget and Popen. I hope someone can help. -- Problem -- I want to use the command: wget -nv -O "dir/cpan.txt" "http://search.cpan.org" and capture all it's...
8
by: clyfish | last post by:
In cmd, I can use find like this. C:\>netstat -an | find "445" TCP 0.0.0.0:445 0.0.0.0:0 LISTENING UDP 0.0.0.0:445 *:* C:\> And os.system is OK....
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.