Skip to content

Commit

Permalink
Fix for ros#610: Added python-requests which provides a clean http li…
Browse files Browse the repository at this point in the history
…brary for HTTP/1.1 requests
  • Loading branch information
mgrrx committed May 31, 2016
1 parent 4f06033 commit f1f3340
Show file tree
Hide file tree
Showing 14 changed files with 102 additions and 61 deletions.
10 changes: 3 additions & 7 deletions clients/rospy/src/rospy/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,10 @@
except ImportError:
import urlparse

try:
import xmlrpc.client as xmlrpcclient #Python 3.x
except ImportError:
import xmlrpclib as xmlrpcclient #Python 2.x

import rospkg

import rosgraph.roslogging
import rosgraph.xmlrpc

import rospy.exceptions
import rospy.rostime
Expand Down Expand Up @@ -468,12 +464,12 @@ def validator(param_value, caller_id):
def xmlrpcapi(uri):
"""
@return: instance for calling remote server or None if not a valid URI
@rtype: xmlrpclib.ServerProxy
@rtype: rosgraph.xmlrpc.ServerProxy
"""
if uri is None:
return None
uriValidate = urlparse.urlparse(uri)
if not uriValidate[0] or not uriValidate[1]:
return None
return xmlrpcclient.ServerProxy(uri)
return rosgraph.xmlrpc.ServerProxy(uri)

6 changes: 1 addition & 5 deletions clients/rospy/src/rospy/impl/tcpros_pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@
import threading
import time

try:
from xmlrpc.client import ServerProxy # Python 3.x
except ImportError:
from xmlrpclib import ServerProxy # Python 2.x

from rosgraph.xmlrpc import ServerProxy
from rospy.core import logwarn, logerr, logdebug, rospyerr
import rospy.exceptions
import rospy.names
Expand Down
1 change: 1 addition & 0 deletions tools/rosgraph/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

<run_depend>python-netifaces</run_depend>
<run_depend>python-rospkg</run_depend>
<run_depend>python-requests</run_depend>

<test_depend>python-mock</test_depend>

Expand Down
9 changes: 3 additions & 6 deletions tools/rosgraph/src/rosgraph/impl/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,10 @@
import random
import logging
import traceback
try:
from xmlrpc.client import ServerProxy
except ImportError:
from xmlrpclib import ServerProxy
import socket

import rosgraph.masterapi
import rosgraph.xmlrpc

logger = logging.getLogger('rosgraph.graph')

Expand Down Expand Up @@ -368,7 +365,7 @@ def _node_refresh_businfo(self, node, api, bad_node=False):
@param node: node name
@type node: str
@param api: XML-RPC proxy
@type api: ServerProxy
@type api: rosgraph.xmlrpc.ServerProxy
@param bad_node: If True, node has connectivity issues and
should be treated differently
@type bad_node: bool
Expand Down Expand Up @@ -452,7 +449,7 @@ def _node_refresh(self, node, bad_node=False):
uri = self._node_uri_refresh(node)
try:
if uri:
api = ServerProxy(uri)
api = rosgraph.xmlrpc.ServerProxy(uri)
updated = self._node_refresh_businfo(node, api, bad_node)
except KeyError as e:
logger.warn('cannot contact node [%s] as it is not in the lookup table'%node)
Expand Down
6 changes: 1 addition & 5 deletions tools/rosgraph/src/rosgraph/masterapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,10 @@
the Master API is changed.
"""

try:
from xmlrpc.client import ServerProxy # Python 3.x
except ImportError:
from xmlrpclib import ServerProxy # Python 2.x

from . names import make_caller_id
from . rosenv import get_master_uri
from . network import parse_http_host_and_port
from . xmlrpc import ServerProxy

class MasterException(Exception):
"""
Expand Down
47 changes: 47 additions & 0 deletions tools/rosgraph/src/rosgraph/transport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
"""A replacement transport for Python xmlrpc library."""

try:
import xmlrpc.client as xmlrpc
except ImportError:
import xmlrpclib as xmlrpc

import requests


class RequestsTransport(xmlrpc.Transport):
"""Drop in Transport for xmlrpclib that uses Requests instead of httplib"""

user_agent = "Python XMLRPC with Requests (python-requests.org)"

def __init__(self, scheme, use_datetime=0):
xmlrpc.Transport.__init__(self, use_datetime)
self._scheme = scheme

def request(self, host, handler, request_body, verbose=0):
"""Make a xmlrpc request."""
headers = {'User-Agent': self.user_agent, 'Content-Type': 'text/xml'}
url = '{scheme}://{host}{handler}'.format(scheme=self._scheme,
host=host,
handler=handler)
try:
resp = requests.post(url, data=request_body, headers=headers)
except (ValueError, Exception):
raise
else:
try:
resp.raise_for_status()
except requests.RequestException as exc:
raise xmlrpc.ProtocolError(url, resp.status_code,
str(exc), resp.headers)
else:
return self.parse_response(resp)

def parse_response(self, resp):
"""
Parse the xmlrpc response.
"""
p, u = self.getparser()
p.feed(resp.text)
p.close()
return u.close()
21 changes: 21 additions & 0 deletions tools/rosgraph/src/rosgraph/xmlrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,22 @@
from SimpleXMLRPCServer import SimpleXMLRPCServer #Python 2.x
from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler #Python 2.x

try:
from xmlrpc.client import ServerProxy as _ServerProxy
except ImportError:
from xmlrpclib import ServerProxy as _ServerProxy
try:
from urllib.parse import splittype
except ImportError:
from urllib import splittype

try:
import socketserver
except ImportError:
import SocketServer as socketserver

import rosgraph.network
from rosgraph.transport import RequestsTransport

def isstring(s):
"""Small helper version to check an object is a string in a way that works
Expand All @@ -82,6 +92,17 @@ def log_message(self, format, *args):
if 0:
SimpleXMLRPCRequestHandler.log_message(self, format, *args)

class ServerProxy(_ServerProxy):
def __init__(self, uri, transport=None, encoding=None, verbose=0,
allow_none=0, use_datetime=0):
if transport is None:
scheme, _ = splittype(uri)
transport = RequestsTransport(scheme)

_ServerProxy.__init__(self, uri, transport=transport, encoding=encoding,
verbose=verbose, allow_none=allow_none,
use_datetime=use_datetime)

class ThreadingXMLRPCServer(socketserver.ThreadingMixIn, SimpleXMLRPCServer):
"""
Adds ThreadingMixin to SimpleXMLRPCServer to support multiple concurrent
Expand Down
9 changes: 5 additions & 4 deletions tools/roslaunch/src/roslaunch/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,16 @@
import socket
import sys
try:
from xmlrpc.client import MultiCall, ServerProxy
from xmlrpc.client import MultiCall
except ImportError:
from xmlrpclib import MultiCall, ServerProxy
from xmlrpclib import MultiCall

import rospkg

import rosgraph
import rosgraph.names
import rosgraph.network
import rosgraph.xmlrpc

from xml.sax.saxutils import escape
try:
Expand Down Expand Up @@ -288,9 +289,9 @@ def __eq__(self, m2):

def get(self):
"""
:returns:: XMLRPC proxy for communicating with master, ``xmlrpc.client.ServerProxy``
:returns:: XMLRPC proxy for communicating with master, ``rosgraph.xmlrpc.ServerProxy``
"""
return ServerProxy(self.uri)
return rosgraph.xmlrpc.ServerProxy(self.uri)

def get_multi(self):
"""
Expand Down
8 changes: 2 additions & 6 deletions tools/roslaunch/src/roslaunch/netapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,9 @@
Convience methods for manipulating XML-RPC APIs
"""

try:
from xmlrpc.client import ServerProxy
except ImportError:
from xmlrpclib import ServerProxy

import rosgraph
import rosgraph.network
import rosgraph.xmlrpc

_ID = '/roslaunch_netapi'
def get_roslaunch_uris():
Expand Down Expand Up @@ -83,7 +79,7 @@ def list_processes(roslaunch_uris=None):
procs = []
for uri in roslaunch_uris:
try:
r = ServerProxy(uri)
r = rosgraph.xmlrpc.ServerProxy(uri)
code, msg, val = r.list_processes()
if code == 1:
active, dead = val
Expand Down
10 changes: 4 additions & 6 deletions tools/roslaunch/src/roslaunch/remoteprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,9 @@
import os
import socket
import traceback
try:
from xmlrpc.client import ServerProxy
except ImportError:
from xmlrpclib import ServerProxy

import rosgraph
import rosgraph.xmlrpc
from roslaunch.core import printlog, printerrlog
import roslaunch.pmon
import roslaunch.server
Expand Down Expand Up @@ -240,10 +237,11 @@ def start(self):

def getapi(self):
"""
:returns: ServerProxy to remote client XMLRPC server, `ServerProxy`
:returns: rosgraph.xmlrpc.ServerProxy to remote client XMLRPC server,
`rosgraph.xmlrpc.ServerProxy`
"""
if self.uri:
return ServerProxy(self.uri)
return rosgraph.xmlrpc.ServerProxy(self.uri)
else:
return None

Expand Down
10 changes: 3 additions & 7 deletions tools/roslaunch/src/roslaunch/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,6 @@
from urllib.parse import urlparse
except ImportError:
from urlparse import urlparse
try:
from xmlrpc.client import ServerProxy
except ImportError:
from xmlrpclib import ServerProxy

import rosgraph.network as network
import rosgraph.xmlrpc as xmlrpc
Expand Down Expand Up @@ -263,7 +259,7 @@ def __init__(self, run_id, name, server_uri, pm):
self.name = name
self.pm = pm
self.server_uri = server_uri
self.server = ServerProxy(server_uri)
self.server = xmlrpc.ServerProxy(server_uri)

def _shutdown(self, reason):
"""
Expand Down Expand Up @@ -373,7 +369,7 @@ def start(self):
server_up = False
while not server_up and time.time() < timeout_t:
try:
code, msg, val = ServerProxy(self.uri).get_pid()
code, msg, val = xmlrpc.ServerProxy(self.uri).get_pid()
if val != os.getpid():
raise RLException("Server at [%s] did not respond with correct PID. There appears to be something wrong with the networking configuration"%self.uri)
server_up = True
Expand Down Expand Up @@ -502,7 +498,7 @@ def _register_with_server(self):
name = self.name
self.logger.info("attempting to register with roslaunch parent [%s]"%self.server_uri)
try:
server = ServerProxy(self.server_uri)
server = xmlrpc.ServerProxy(self.server_uri)
code, msg, _ = server.register(name, self.uri)
if code != 1:
raise RLException("unable to register with roslaunch server: %s"%msg)
Expand Down
10 changes: 6 additions & 4 deletions tools/rosmaster/src/rosmaster/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,25 @@
except ImportError:
from urlparse import urlparse
try:
from xmlrpc.client import ServerProxy
from urllib.parse import splittype
except ImportError:
from xmlrpclib import ServerProxy
from urllib import splittype

import rosgraph.xmlrpc

_proxies = {} #cache ServerProxys
def xmlrpcapi(uri):
"""
@return: instance for calling remote server or None if not a valid URI
@rtype: xmlrpc.client.ServerProxy
@rtype: rosgraph.xmlrpc.ServerProxy
"""
if uri is None:
return None
uriValidate = urlparse(uri)
if not uriValidate[0] or not uriValidate[1]:
return None
if not uri in _proxies:
_proxies[uri] = ServerProxy(uri)
_proxies[uri] = rosgraph.xmlrpc.ServerProxy(uri)
return _proxies[uri]


Expand Down
9 changes: 3 additions & 6 deletions tools/rosnode/src/rosnode/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,14 @@
import sys
import socket
import time
try:
from xmlrpc.client import ServerProxy
except ImportError:
from xmlrpclib import ServerProxy

try: #py3k
import urllib.parse as urlparse
except ImportError:
import urlparse

from optparse import OptionParser
from rosgraph.xmlrpc import ServerProxy
import rosgraph
import rosgraph.names
import rostopic
Expand Down Expand Up @@ -402,7 +399,7 @@ def cleanup_master_blacklist(master, blacklist):
"""
Remove registrations from ROS Master that do not match blacklist.
@param master: XMLRPC handle to ROS Master
@type master: xmlrpclib.ServerProxy
@type master: rosgraph.xmlrpc.ServerProxy
@param blacklist: list of nodes to scrub
@type blacklist: [str]
"""
Expand All @@ -428,7 +425,7 @@ def cleanup_master_whitelist(master, whitelist):
"""
Remove registrations from ROS Master that do not match whitelist.
@param master: XMLRPC handle to ROS Master
@type master: xmlrpclib.ServerProxy
@type master: nosgraph.xmlrpc.ServerProxy
@param whitelist: list of nodes to keep
@type whitelist: list of nodes to keep
"""
Expand Down
7 changes: 2 additions & 5 deletions tools/rostest/src/rostest/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,9 @@ def get_master():
@return: XML-RPC proxy to ROS master
@rtype: xmlrpclib.ServerProxy
"""
try:
import xmlrpc.client as xmlrpcclient #Python 3.x
except ImportError:
import xmlrpclib as xmlrpcclient #Python 2.x
from rosgraph.xmlrpc import ServerProxy
uri = rosgraph.get_master_uri()
return xmlrpcclient.ServerProxy(uri)
return ServerProxy(uri)

def is_subscriber(topic, subscriber_id):
"""
Expand Down

0 comments on commit f1f3340

Please sign in to comment.