Aber: Wenn ich (unter Windows) den Befehl 'dir' eigebe, schmiert mir der Client ab und bringt die Meldung:
Zur Sicherheit lasse ich mir mal die Ausgabe mitPython Interpreter hat geschrieben:Traceback (most recent call last):
File "pyshell.py", line 84, in ?
XmlRpcShell(sys.argv[1:])
File "pyshell.py", line 39, in __init__
self.client()
File "pyshell.py", line 77, in client
sys.stdout.write(self.client.cmd(cmd))
File "C:\Programme\Python24\lib\xmlrpclib.py", line 1096, in __call__
return self.__send(self.__name, args)
File "C:\Programme\Python24\lib\xmlrpclib.py", line 1383, in __request
verbose=self.__verbose
File "C:\Programme\Python24\lib\xmlrpclib.py", line 1147, in request
return self._parse_response(h.getfile(), sock)
File "C:\Programme\Python24\lib\xmlrpclib.py", line 1281, in _parse_response
p.feed(response)
File "C:\Programme\Python24\lib\xmlrpclib.py", line 527, in feed
self._parser.Parse(data, 0)
xml.parsers.expat.ExpatError: not well-formed (invalid token): line 5, column 23
Code: Alles auswählen
sys.stdout.write(line) # for testing purposes only
Keine Ahnung, woran das liegen könnte... habt ihr eine Idee?
Das zweite Problem ist, dass der Server abschmiert, sobald ich z.B. 'cmd.exe' eingebe. Man müsste mal testen, ob das unter Unixen bei '/bin/bash' auch passiert. Der Server hängt sich auch auf, wenn ich z.B. '%ProgramFiles\Python24\python.exe' aufrufen will und solche Sachen... Wie erzeugen die ihre Ausgabe? Kommt da nichts über stdout?
Code: Alles auswählen
import sys, os, subprocess, time, md5
from SimpleXMLRPCServer import SimpleXMLRPCServer
class XmlRpcShellServer(SimpleXMLRPCServer):
# overridings
allow_reuse_address = True
# don't stop serving until stop is True
stop = False
def serve_forever(self):
while not self.stop:
self.handle_request()
class XmlRpcShell:
# configuration
config = {
'protocol': "http",
'host': "localhost",
'port': 8080,
'stopcmd': "exit",
'timeout': 5
}
# access list
access = {
'user': "pass"
}
# constructor
def __init__(self, args):
# check arguments
if not len(args) > 0:
self.usage()
self.mode = args[0]
# check access
""" testing """
self.user = "droptix" # for testing purposes until 'access list' works
if self.mode == "server":
self.server()
elif self.mode == "client":
self.client()
else:
self.usage()
# server function(s)
def cmd(self, cmd):
if cmd == self.config['stopcmd']:
self.server.stop = True
return "stop server"
else:
cmdtime = time.time()
output = ""
process = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
runtime = time.time() - cmdtime
line = process.stdout.read() # hanging up here...
sys.stdout.write(line) # for testing purposes only
if line == "" or runtime >= self.config['timeout']:
break
output += line
return output # e.g. cmd='dir' exits with: xml.parsers.expat.ExpatError: not well-formed (invalid token): line 5, column 23
# server
def server(self):
print "booting"
self.server = XmlRpcShellServer((self.config['host'], self.config['port']))
print "registering functions"
self.server.register_function(self.cmd)
print "server booted"
self.server.serve_forever()
def client(self):
from xmlrpclib import Server
self.client = Server("%s://%s:%s" % (self.config['protocol'], self.config['host'], self.config['port']))
cmd = None
while not cmd == self.config['stopcmd']:
if sys.platform == "win32":
prompt = ">"
else:
prompt = "$ "
cmd = raw_input("%s@%s%s" % (self.user, self.config['host'], prompt))
sys.stdout.write(self.client.cmd(cmd))
# usage
def usage(self):
print "USAGE MESSAGE"
sys.exit(1)
if __name__ == "__main__":
XmlRpcShell(sys.argv[1:])