Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > xmlrpc server on red hat 9 question

Reply
Thread Tools

xmlrpc server on red hat 9 question

 
 
Jason Kratz
Guest
Posts: n/a
 
      06-29-2003
After figuring out the other day (thanks to everyone here who helped)
how to get the dir lists I want on my unix machine I want to turn the
same thing into an XML-RPC service. Using Simple XMLRPCServer.py I
created my own version by adding my getdirs function and then I call the
code to open the server:

import os
import os.path
import SimpleXMLRPCServer
import xmlrpclib
import SocketServer
import BaseHTTPServer
import sys


class SimpleXMLRPCRequestHandler(BaseHTTPServer.BaseHTTP RequestHandler):
def do_POST(self):
"""Handles the HTTP POST request.

Attempts to interpret all HTTP POST requests as XML-RPC calls,
which are forwarded to the _dispatch method for handling.
"""

try:
# get arguments
data = self.rfile.read(int(self.headers["content-length"]))
params, method = xmlrpclib.loads(data)

# generate response
try:
response = self._dispatch(method, params)
# wrap response in a singleton tuple
response = (response,)
except:
# report exception back to server
response = xmlrpclib.dumps(
xmlrpclib.Fault(1, "%s:%s" % (sys.exc_type,
sys.exc_value))
)
else:
response = xmlrpclib.dumps(response, methodresponse=1)
except:
# internal error, report as HTTP server error
self.send_response(500)
self.end_headers()
else:
# got a valid XML RPC response
self.send_response(200)
self.send_header("Content-type", "text/xml")
self.send_header("Content-length", str(len(response)))
self.end_headers()
self.wfile.write(response)

# shut down the connection
self.wfile.flush()
self.connection.shutdown(1)

def _dispatch(self, method, params):
func = None
try:
# check to see if a matching function has been registered
func = self.server.funcs[method]
except KeyError:
if self.server.instance is not None:
# check for a _dispatch method
if hasattr(self.server.instance, '_dispatch'):
return self.server.instance._dispatch(method, params)
else:
# call instance method directly
try:
func = _resolve_dotted_attribute(
self.server.instance,
method
)
except AttributeError:
pass

if func is not None:
return apply(func, params)
else:
raise Exception('method "%s" is not supported' % method)

def log_request(self, code='-', size='-'):
"""Selectively log an accepted request."""

if self.server.logRequests:
BaseHTTPServer.BaseHTTPRequestHandler.log_request( self,
code, size)


def _resolve_dotted_attribute(obj, attr):
"""Resolves a dotted attribute name to an object. Raises
an AttributeError if any attribute in the chain starts with a '_'.
"""
for i in attr.split('.'):
if i.startswith('_'):
raise AttributeError(
'attempt to access private attribute "%s"' % i
)
else:
obj = getattr(obj,i)
return obj


class SimpleXMLRPCServer(SocketServer.TCPServer):

def __init__(self, addr, requestHandler=SimpleXMLRPCRequestHandler,
logRequests=1):
self.funcs = {}
self.logRequests = logRequests
self.instance = None
SocketServer.TCPServer.__init__(self, addr, requestHandler)

def register_instance(self, instance):

self.instance = instance

def register_function(self, function, name = None):

if name is None:
name = function.__name__
self.funcs[name] = function


def getdirs(path):
dirs=[]

for entry in os.listdir(path):
entry=os.path.join(path,entry)
if os.path.isdir(entry):
dirs.append(entry)
return dirs


server = SimpleXMLRPCServer(("localhost", 8080))
server.register_function(getdirs)
server.serve_forever()

Now...this seems to run just fine on my red hat 9 machine. Problem is I
cant connect to it from another machine using the following code:

import xmlrpclib

client = xmlrpclib.Server("http://luna:8080")
print client.getdirs('/');

I get a connection refused error. Interestingly enough this works just
dandy if run on the same box as the server piece. Anyone have any
ideas what i need to do to get this to work? I run the java servlet
runner Resin on the box and that always seems to work just fine on the
same port.

thanks,

Jason

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thinking of getting a Dell server with Red Hat ASx? Bruce Sinclair NZ Computing 9 06-22-2005 08:04 PM
Re: xmlrpc, httplib and SSL (HTTP 1.1 XMLRPC client) Etienne Posthumus Python 1 04-01-2004 05:13 PM
Red Hat Defines end of Red Hat Linux steve NZ Computing 14 11-06-2003 06:19 AM
Newbie. How to start Studio One 4.0 on Red Hat 9.0 Sphenxes Java 2 09-05-2003 01:02 PM
Red Hat java LoadLibrary Dependency Issues Jared Java 1 07-08-2003 05:51 PM



Advertisments