Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   xmlrpc server on red hat 9 question (http://www.velocityreviews.com/forums/t318975-xmlrpc-server-on-red-hat-9-question.html)

Jason Kratz 06-29-2003 04:27 AM

xmlrpc server on red hat 9 question
 
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



All times are GMT. The time now is 09:18 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57