Drop more obsolete xmlrpc code
This commit is contained in:
parent
04750e69d3
commit
4c35d77854
@ -15,19 +15,13 @@
|
||||
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
||||
# Boston, MA 02111-1307, USA.
|
||||
|
||||
import socket
|
||||
import os
|
||||
import threading
|
||||
import traceback
|
||||
import xmlrpclib
|
||||
import sys
|
||||
import httplib
|
||||
import urllib
|
||||
import fcntl
|
||||
import tempfile
|
||||
|
||||
import gobject
|
||||
import SimpleXMLRPCServer
|
||||
import SimpleHTTPServer
|
||||
import SocketServer
|
||||
|
||||
@ -299,178 +293,3 @@ class GlibURLDownloader(gobject.GObject):
|
||||
if remove:
|
||||
os.remove(self._fname)
|
||||
self._outf = None
|
||||
|
||||
|
||||
class GlibXMLRPCRequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
|
||||
""" GlibXMLRPCRequestHandler
|
||||
|
||||
The stock SimpleXMLRPCRequestHandler and server don't allow any way to pass
|
||||
the client's address and/or SSL certificate into the function that actually
|
||||
_processes_ the request. So we have to store it in a thread-indexed dict.
|
||||
"""
|
||||
|
||||
def do_POST(self):
|
||||
_add_authinfo(self.client_address)
|
||||
try:
|
||||
SimpleXMLRPCServer.SimpleXMLRPCRequestHandler.do_POST(self)
|
||||
except socket.timeout:
|
||||
pass
|
||||
except socket.error, e:
|
||||
print "Error (%s): socket error - '%s'" % (self.client_address, e)
|
||||
except:
|
||||
print "Error while processing POST:"
|
||||
traceback.print_exc()
|
||||
_del_authinfo()
|
||||
|
||||
class GlibXMLRPCServer(GlibTCPServer,
|
||||
SimpleXMLRPCServer.SimpleXMLRPCDispatcher):
|
||||
"""GlibXMLRPCServer
|
||||
|
||||
Use nonblocking sockets and handle the accept via glib rather than
|
||||
blocking on accept().
|
||||
"""
|
||||
|
||||
def __init__(self, addr, requestHandler=GlibXMLRPCRequestHandler,
|
||||
logRequests=0, allow_none=False):
|
||||
self.logRequests = logRequests
|
||||
if sys.version_info[:3] >= (2, 5, 0):
|
||||
SimpleXMLRPCServer.SimpleXMLRPCDispatcher.__init__(
|
||||
self, allow_none, encoding="utf-8")
|
||||
else:
|
||||
SimpleXMLRPCServer.SimpleXMLRPCDispatcher.__init__(self)
|
||||
GlibTCPServer.__init__(self, addr, requestHandler)
|
||||
|
||||
def _marshaled_dispatch(self, data, dispatch_method = None):
|
||||
"""Dispatches an XML-RPC method from marshalled (XML) data.
|
||||
|
||||
XML-RPC methods are dispatched from the marshalled (XML) data
|
||||
using the _dispatch method and the result is returned as
|
||||
marshalled data. For backwards compatibility, a dispatch
|
||||
function can be provided as an argument (see comment in
|
||||
SimpleXMLRPCRequestHandler.do_POST) but overriding the
|
||||
existing method through subclassing is the prefered means
|
||||
of changing method dispatch behavior.
|
||||
"""
|
||||
|
||||
params, method = xmlrpclib.loads(data)
|
||||
|
||||
# generate response
|
||||
try:
|
||||
if dispatch_method is not None:
|
||||
response = dispatch_method(method, params)
|
||||
else:
|
||||
response = self._dispatch(method, params)
|
||||
# wrap response in a singleton tuple
|
||||
response = (response,)
|
||||
response = xmlrpclib.dumps(response, methodresponse=1)
|
||||
except xmlrpclib.Fault, fault:
|
||||
response = xmlrpclib.dumps(fault)
|
||||
except:
|
||||
print "Exception while processing request:"
|
||||
traceback.print_exc()
|
||||
|
||||
# report exception back to server
|
||||
response = xmlrpclib.dumps(
|
||||
xmlrpclib.Fault(1, "%s:%s" % (sys.exc_type, sys.exc_value))
|
||||
)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
class GlibHTTP(httplib.HTTP):
|
||||
"""Subclass HTTP so we can return it's connection class' socket."""
|
||||
def connect(self, host=None, port=None):
|
||||
httplib.HTTP.connect(self, host, port)
|
||||
self._conn.sock.setblocking(0)
|
||||
|
||||
class GlibXMLRPCTransport(xmlrpclib.Transport):
|
||||
"""Integrate the request with the glib mainloop rather than blocking."""
|
||||
##
|
||||
# Connect to server.
|
||||
#
|
||||
# @param host Target host.
|
||||
# @return A connection handle.
|
||||
|
||||
def __init__(self, use_datetime=0):
|
||||
self.verbose = None
|
||||
if sys.version_info[:3] >= (2, 5, 0):
|
||||
xmlrpclib.Transport.__init__(self, use_datetime)
|
||||
|
||||
def make_connection(self, host):
|
||||
"""Use our own connection object so we can get its socket."""
|
||||
# create a HTTP connection object from a host descriptor
|
||||
host, extra_headers_, x509_ = self.get_host_info(host)
|
||||
return GlibHTTP(host)
|
||||
|
||||
##
|
||||
# Send a complete request, and parse the response.
|
||||
#
|
||||
# @param host Target host.
|
||||
# @param handler Target PRC handler.
|
||||
# @param request_body XML-RPC request body.
|
||||
# @param verbose Debugging flag.
|
||||
# @return Parsed response.
|
||||
|
||||
def start_request(self, host, handler, request_body, verbose=0,
|
||||
reply_handler=None, error_handler=None, user_data=None):
|
||||
"""Do the first half of the request by sending data to the remote
|
||||
server. The bottom half bits get run when the remote server's response
|
||||
actually comes back."""
|
||||
# issue XML-RPC request
|
||||
|
||||
h = self.make_connection(host)
|
||||
if verbose:
|
||||
h.set_debuglevel(1)
|
||||
|
||||
self.send_request(h, handler, request_body)
|
||||
self.send_host(h, host)
|
||||
self.send_user_agent(h)
|
||||
self.send_content(h, request_body)
|
||||
|
||||
# Schedule a GIOWatch so we don't block waiting for the response
|
||||
gobject.io_add_watch(h._conn.sock, gobject.IO_IN, self._finish_request,
|
||||
h, host, handler, verbose, reply_handler, error_handler, user_data)
|
||||
|
||||
def _finish_request(self, source, condition, h, host, handler, verbose,
|
||||
reply_handler=None, error_handler=None, user_data=None):
|
||||
"""Parse and return response when the
|
||||
remote server actually returns it."""
|
||||
if not (condition & gobject.IO_IN):
|
||||
return True
|
||||
|
||||
try:
|
||||
errcode, errmsg, headers = h.getreply()
|
||||
except socket.error, err:
|
||||
if err[0] != 104:
|
||||
raise socket.error(err)
|
||||
else:
|
||||
if error_handler:
|
||||
gobject.idle_add(error_handler, err, user_data)
|
||||
return False
|
||||
|
||||
if errcode != 200:
|
||||
raise xmlrpclib.ProtocolError(host + handler, errcode,
|
||||
errmsg, headers)
|
||||
self.verbose = verbose
|
||||
response = self._parse_response(h.getfile(), h._conn.sock)
|
||||
if reply_handler:
|
||||
# Coerce to a list so we can append user data
|
||||
response = response[0]
|
||||
if not isinstance(response, list):
|
||||
response = [response]
|
||||
response.append(user_data)
|
||||
gobject.idle_add(reply_handler, *response)
|
||||
return False
|
||||
|
||||
class _Method:
|
||||
"""Right, so python people thought it would be funny to make this
|
||||
class private to xmlrpclib.py..."""
|
||||
# some magic to bind an XML-RPC method to an RPC server.
|
||||
# supports "nested" methods (e.g. examples.getStateName)
|
||||
def __init__(self, send, name):
|
||||
self.__send = send
|
||||
self.__name = name
|
||||
def __getattr__(self, name):
|
||||
return _Method(self.__send, "%s.%s" % (self.__name, name))
|
||||
def __call__(self, *args, **kwargs):
|
||||
return self.__send(self.__name, *args, **kwargs)
|
||||
|
Loading…
Reference in New Issue
Block a user