dchurch24
April 28th, 2009, 04:48 PM
Hi all,
I have a python script that listens on an IP port and executes a given command.
The commands are looked up in a MySQL database and then sent to the tcp python server. There are multiple 'servers' running on the network.
However, I have a compiled program called ADU which executes fine - in turn the ADU unit switches a relay and turns a specified device on (lamps, amplifiers etc...)
I have another usb relay that isn't an ADU one and the script to execute it is:
echo -e 'xff\x02\x01' > /dev/ttyUSB0
This works fine from terminal, but does not run from inside Python.
Ok, I thought, I'll wrap it up in a script, so I have a script called SwitchR1.sh with the command above inside it and nothing else.
It doesn't execute either?!?
The python code is:
import commands
import os, subprocess
class EchoRequestHandler(SocketServer.BaseRequestHandler ):
def setup(self):
print self.client_address, 'connected!'
self.request.send('hi ' + str(self.client_address) + '\n')
def run_command(self,cmd):
#os.system(cmd)
subprocess.call(cmd,shell=True)
print cmd
def handle(self):
data = 'dummy'
while data:
data = self.request.recv(1024)
self.request.send(data)
if data.strip() == 'bye':
return
self.run_command(data.strip())
def finish(self):
print self.client_address, 'disconnected!'
self.request.send('bye ' + str(self.client_address) + '\n')
#server host is a tuple ('host', port)
server = SocketServer.ThreadingTCPServer(('', 50002), EchoRequestHandler)
server.serve_forever()
I have tried os.system(cmd) as well, as can be seen in the commented line.
The correct command prints out to the screen so I know the IP bit is working and recieving the correct command. It just doesn't execute.
I have made sure that the user running the IP listener is the same owner of the scripts too, just in case it was a permissions error.
I can send commands such as "ls -l" etc... and they execute fine.
Does anyone know why this isn't working?
I have a python script that listens on an IP port and executes a given command.
The commands are looked up in a MySQL database and then sent to the tcp python server. There are multiple 'servers' running on the network.
However, I have a compiled program called ADU which executes fine - in turn the ADU unit switches a relay and turns a specified device on (lamps, amplifiers etc...)
I have another usb relay that isn't an ADU one and the script to execute it is:
echo -e 'xff\x02\x01' > /dev/ttyUSB0
This works fine from terminal, but does not run from inside Python.
Ok, I thought, I'll wrap it up in a script, so I have a script called SwitchR1.sh with the command above inside it and nothing else.
It doesn't execute either?!?
The python code is:
import commands
import os, subprocess
class EchoRequestHandler(SocketServer.BaseRequestHandler ):
def setup(self):
print self.client_address, 'connected!'
self.request.send('hi ' + str(self.client_address) + '\n')
def run_command(self,cmd):
#os.system(cmd)
subprocess.call(cmd,shell=True)
print cmd
def handle(self):
data = 'dummy'
while data:
data = self.request.recv(1024)
self.request.send(data)
if data.strip() == 'bye':
return
self.run_command(data.strip())
def finish(self):
print self.client_address, 'disconnected!'
self.request.send('bye ' + str(self.client_address) + '\n')
#server host is a tuple ('host', port)
server = SocketServer.ThreadingTCPServer(('', 50002), EchoRequestHandler)
server.serve_forever()
I have tried os.system(cmd) as well, as can be seen in the commented line.
The correct command prints out to the screen so I know the IP bit is working and recieving the correct command. It just doesn't execute.
I have made sure that the user running the IP listener is the same owner of the scripts too, just in case it was a permissions error.
I can send commands such as "ls -l" etc... and they execute fine.
Does anyone know why this isn't working?