0

I now have a small java script server working correctly, called by:

<?php
  $handle = fsockopen("udp://78.129.148.16",12345);
  fwrite($handle,"vzctlrestart110");
  fclose($handle);
?>

On a remote server the following python server is running and executing the comand's

#!/usr/bin/python
import os
import socket
print "  Loading Bindings..."
settings = {}
line = 0
for each in open('/root/actions.txt', 'r'):
 line = line + 1
 each = each.rstrip()
 if each != "":
   if each[0] != '#':
     a = each.partition(':')
     if a[2]:
       settings[a[0]] = a[2]
     else:
       print "    Err @ line",line,":",each
print "  Starting Server...",
port = 12345
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("", port))
print "OK."
print "  Listening on port:", port
while True:
    datagram = s.recv(1024)
    if not datagram:
        break
    print "Rx Cmd:", datagram
    if settings.has_key(datagram):
      print "Launch:", settings[datagram]
      os.system(settings[datagram]+" &")
s.close()

Is it possible to easily send the output of the command back, when the server is started and is running a command the output is shown in the ssh window, however I want this output to be sent back to the browser of the original client, maybe setting the browser to wait for 15 seconds and then check for any data received via the socket.

I know I am asking quite a lot, however I am creating a PHP script which I have a large knowledge about, however my python knowledge lacks greatly.

Thanks, Ashley

1
  • Thanks SilentGhost for the reformat. Commented Jun 11, 2009 at 14:17

2 Answers 2

1

Yes, you can read the output of the command. For this I would recommend the Python subprocess module. Then you can just s.write() it back.

Naturally this has some implications, you would probably have to let your PHP script run for a while longer since the process may be running slow.

# The pipe behaves like a file object in Python.
process = Popen(cmd, shell=True, stdout=PIPE)
process_output = ""
while process.poll():
     process_output += process.stdout.read(256)
s.write(process_output)

# Better yet.
process = Popen(cmd, shell=true, stdout=PIPE)
stdout, stderr = process.communicate() # will read and wait for process to end.
s.write(stdout)

Integrated into your code:

# ... snip ...
import subprocess
con, addr = s.accept()
while True:
    datagram = con.recv(1024)
    if not datagram:
        break
    print "Rx Cmd:", datagram
    if settings.has_key(datagram):
        print "Launch:", settings[datagram]
        process = subprocess.Popen(settings[datagram]+" &", shell=True, stdout=subprocess.PIPE)
        stdout, stderr = process.communicate()
        con.send(stdout)
    con.close()
s.close()
Sign up to request clarification or add additional context in comments.

28 Comments

all my commands take about 15 to 20 second's, as I said im very new to python, any change of a basic script that I can get the idea of how it work's and implement it?
Well sadly that depends on the command you are running with your Python script I think. If you could do it asynchronously from your page it would at least not make the browser "spin", but that would obviously make matters much more complex.
I am running the commands vzctl from openvz such as vzctl restart 110 e.t.c, I don't mind the brower spinning, as I want the user to be able to see that the command is running. Im not sure exactly how I could intergrate your above code into the script i'm already using, for example exactly where to place it.
Okay. Well this would probably go after and in the same block as the print "Launch:" statement
Im getting the following error, the server start's fine however once I ask it to run a command it crashes and throws this error. Traceback (most recent call last): File "vzctl.py", line 30, in <module> process = Popen(cmd, shell=true, stdout=PIPE) NameError: name 'Popen' is not defined I says the name Popen isnt defined, guessing I need to Include another class at the top of the script?
|
0

Here's an example of how to get the output of a command:

>>> import commands
>>> s =  commands.getoutput("ls *")
>>> s
'client.py'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.