Client/Server Question

bryanjugglercryptographer at yahoo.com bryanjugglercryptographer at yahoo.com
Sat Jul 29 12:39:01 EDT 2006


diffuser78 at 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




More information about the Python-list mailing list