By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,277 Members | 2,363 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,277 IT Pros & Developers. It's quick & easy.

Client/Server Question

P: n/a
My server.py looks like this

---------------------------------------------CODE----------------------------------
#!/usr/bin/env python
import socket
import sys
import os

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = 2000

s.bind((host,port))
s.listen(1)
conn, addr = s.accept()
print 'client is at', addr

while True:
data = conn.recv(1000000)
if (data == 'MaxSim'):
print 'MaxiSim'
os.system('notepad')
elif (data == 'Driving Sim'):
print 'Driving Sim'
os.system('explorer')
elif (data == 'SHUTDOWN'):
print 'Shutting down...'
os.system('shutdown -s')
conn.close()
break
-------------------------------------------CODE
END-------------------------------------

I am running this above program on a windows machine. My client is a
Linux box. What I want to achieve is that server.py should follows
instructions till I send a 'SHUTDOWN' command upon which it should shut
down.

When I run this program and suppose send 'MaxSim' to it, it launches
notepad.exe fine, but then after that it doesn't accept subsequent
command. I want is that it should accept subsequent commands like
Driving Sim and launch windows explorer etc untill I send a 'SHUTDOWN'
command.

Any help on this, it will be greatly appreciated.

Jul 28 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
di********@gmail.com wrote:
My server.py looks like this

---------------------------------------------CODE----------------------------------
#!/usr/bin/env python
import socket
import sys
import os

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = 2000

s.bind((host,port))
s.listen(1)
conn, addr = s.accept()
print 'client is at', addr

while True:
data = conn.recv(1000000)
if (data == 'MaxSim'):
print 'MaxiSim'
os.system('notepad')
elif (data == 'Driving Sim'):
print 'Driving Sim'
os.system('explorer')
elif (data == 'SHUTDOWN'):
print 'Shutting down...'
os.system('shutdown -s')
conn.close()
break
-------------------------------------------CODE
END-------------------------------------

I am running this above program on a windows machine. My client is a
Linux box. What I want to achieve is that server.py should follows
instructions till I send a 'SHUTDOWN' command upon which it should shut
down.

When I run this program and suppose send 'MaxSim' to it, it launches
notepad.exe fine, but then after that it doesn't accept subsequent
command. I want is that it should accept subsequent commands like
Driving Sim and launch windows explorer etc untill I send a 'SHUTDOWN'
command.

Any help on this, it will be greatly appreciated.

os.system() blocks until the called program has finished. Use the
subprocess module <http://docs.python.org/lib/module-subprocess.html>:

<untested_code>

import subprocess

subprocess.Popen("notepad")

</untested_code>
Dennis
Jul 28 '06 #2

P: n/a
Is os.system() going to be deprecated in future ?.....I read somewhere.

Dennis Benzinger wrote:
di********@gmail.com wrote:
My server.py looks like this

---------------------------------------------CODE----------------------------------
#!/usr/bin/env python
import socket
import sys
import os

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = 2000

s.bind((host,port))
s.listen(1)
conn, addr = s.accept()
print 'client is at', addr

while True:
data = conn.recv(1000000)
if (data == 'MaxSim'):
print 'MaxiSim'
os.system('notepad')
elif (data == 'Driving Sim'):
print 'Driving Sim'
os.system('explorer')
elif (data == 'SHUTDOWN'):
print 'Shutting down...'
os.system('shutdown -s')
conn.close()
break
-------------------------------------------CODE
END-------------------------------------

I am running this above program on a windows machine. My client is a
Linux box. What I want to achieve is that server.py should follows
instructions till I send a 'SHUTDOWN' command upon which it should shut
down.

When I run this program and suppose send 'MaxSim' to it, it launches
notepad.exe fine, but then after that it doesn't accept subsequent
command. I want is that it should accept subsequent commands like
Driving Sim and launch windows explorer etc untill I send a 'SHUTDOWN'
command.

Any help on this, it will be greatly appreciated.


os.system() blocks until the called program has finished. Use the
subprocess module <http://docs.python.org/lib/module-subprocess.html>:

<untested_code>

import subprocess

subprocess.Popen("notepad")

</untested_code>
Dennis
Jul 28 '06 #3

P: n/a
i highly doubt it.
http://www.google.com/search?domains...&submit=search
di********@gmail.com wrote:
Is os.system() going to be deprecated in future ?.....I read somewhere.

Dennis Benzinger wrote:
di********@gmail.com wrote:
My server.py looks like this
>
---------------------------------------------CODE----------------------------------
#!/usr/bin/env python
import socket
import sys
import os
>
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = 2000
>
s.bind((host,port))
s.listen(1)
conn, addr = s.accept()
print 'client is at', addr
>
while True:
data = conn.recv(1000000)
if (data == 'MaxSim'):
print 'MaxiSim'
os.system('notepad')
elif (data == 'Driving Sim'):
print 'Driving Sim'
os.system('explorer')
elif (data == 'SHUTDOWN'):
print 'Shutting down...'
os.system('shutdown -s')
conn.close()
break
-------------------------------------------CODE
END-------------------------------------
>
I am running this above program on a windows machine. My client is a
Linux box. What I want to achieve is that server.py should follows
instructions till I send a 'SHUTDOWN' command upon which it should shut
down.
>
When I run this program and suppose send 'MaxSim' to it, it launches
notepad.exe fine, but then after that it doesn't accept subsequent
command. I want is that it should accept subsequent commands like
Driving Sim and launch windows explorer etc untill I send a 'SHUTDOWN'
command.
>
Any help on this, it will be greatly appreciated.
>

os.system() blocks until the called program has finished. Use the
subprocess module <http://docs.python.org/lib/module-subprocess.html>:

<untested_code>

import subprocess

subprocess.Popen("notepad")

</untested_code>
Dennis
Jul 28 '06 #4

P: n/a
you might want to look at sshd. if you're on a windows box, you may
need cygwin. if you're on linux, you either already have it, or it's in
your package manager.

di********@gmail.com wrote:
My server.py looks like this

---------------------------------------------CODE----------------------------------
#!/usr/bin/env python
import socket
import sys
import os

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = 2000

s.bind((host,port))
s.listen(1)
conn, addr = s.accept()
print 'client is at', addr

while True:
data = conn.recv(1000000)
if (data == 'MaxSim'):
print 'MaxiSim'
os.system('notepad')
elif (data == 'Driving Sim'):
print 'Driving Sim'
os.system('explorer')
elif (data == 'SHUTDOWN'):
print 'Shutting down...'
os.system('shutdown -s')
conn.close()
break
-------------------------------------------CODE
END-------------------------------------

I am running this above program on a windows machine. My client is a
Linux box. What I want to achieve is that server.py should follows
instructions till I send a 'SHUTDOWN' command upon which it should shut
down.

When I run this program and suppose send 'MaxSim' to it, it launches
notepad.exe fine, but then after that it doesn't accept subsequent
command. I want is that it should accept subsequent commands like
Driving Sim and launch windows explorer etc untill I send a 'SHUTDOWN'
command.

Any help on this, it will be greatly appreciated.
Jul 28 '06 #5

P: n/a
di********@gmail.com wrote:
Is os.system() going to be deprecated in future ?.....I read somewhere.
[...]
Sometime in the future it will. But that won't happen soon. Read the
second paragraph of "Backwards Compatibility" in the subprocess PEP
<http://www.python.org/dev/peps/pep-0324/>.
Dennis
Jul 29 '06 #6

P: n/a

di********@gmail.com wrote:
My server.py looks like this

---------------------------------------------CODE----------------------------------
#!/usr/bin/env python
import socket
import sys
import os

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = 2000

s.bind((host,port))
s.listen(1)
conn, addr = s.accept()
print 'client is at', addr

while True:
data = conn.recv(1000000)
if (data == 'MaxSim'):
print 'MaxiSim'
os.system('notepad')
elif (data == 'Driving Sim'):
print 'Driving Sim'
os.system('explorer')
elif (data == 'SHUTDOWN'):
print 'Shutting down...'
os.system('shutdown -s')
conn.close()
break
-------------------------------------------CODE
END-------------------------------------

I am running this above program on a windows machine. My client is a
Linux box. What I want to achieve is that server.py should follows
instructions till I send a 'SHUTDOWN' command upon which it should shut
down.

When I run this program and suppose send 'MaxSim' to it, it launches
notepad.exe fine, but then after that it doesn't accept subsequent
command.
As others noted, that's because os.system() blocks.
You have more bugs than that.

The recv() might return "MaxiSimDriving Sim". It could return
"MaxiS" on one call, and "im" on the next. If the remote side
closes the connection, recv() will keep returning the empty
string, and your program will be stuck in an infinite loop.

Did you understand Faulkner's suggustion? Anyone who connects to
TCP port 2000 can invoke "shutdown -s" (which I assume shuts down
your host).
--
--Bryan

Jul 29 '06 #7

P: n/a
The subprocess module didn't work here. I tried using os.popen(), that
won't help it either.
What I am trying to achieve is that the server daemon is sitting and
listening to command from the client. As client send "MaxSim" it
launches MaxSim.exe and then again waits for subsequent commands.
Support client sends "DrivingSim" it should launch DrivingSim.exe etc.
It does till client sends a "SHUTODWN" message upon which it shuts down
itself.

Any pointers to achieve this more seamlessly.

Every help is greatly appreciated.

Thanks
Dennis Benzinger wrote:
os.system() blocks until the called program has finished. Use the
subprocess module <http://docs.python.org/lib/module-subprocess.html>:

<untested_code>

import subprocess

subprocess.Popen("notepad")

</untested_code>
Dennis
Aug 10 '06 #8

P: n/a
The recv() might return "MaxiSimDriving Sim". It could return
"MaxiS" on one call, and "im" on the next. If the remote side
closes the connection, recv() will keep returning the empty
string, and your program will be stuck in an infinite loop.
How to overcome this problem ?
Did you understand Faulkner's suggustion? Anyone who connects to
TCP port 2000 can invoke "shutdown -s" (which I assume shuts down
your host).
I did, anyone who connects to port 2000 might be able to send a
shutdown and close the computer. I am aware that its not the best way
to achieve this but my app runs on a small network without actually
connecting to internet.

Can you give any tips to overcome the above mentioned problem.
--Bryan
Aug 10 '06 #9

P: n/a
I apologize.

subprocess did work for me like a charm I made a mistake in my code.
Appreciate your help.

Dennis Benzinger wrote:
os.system() blocks until the called program has finished. Use the
subprocess module <http://docs.python.org/lib/module-subprocess.html>:

<untested_code>

import subprocess

subprocess.Popen("notepad")

</untested_code>
Dennis
Aug 10 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.