python XMLRPC函数错误

python XMLRPC函数错误,python,function,connection,ros,Python,Function,Connection,Ros,我试图通过XML RPC连接两台计算机,但我总是收到“[Errno-2]名称或服务未知”消息。有时有效,有时无效,我不知道我做错了什么。下面是我正在运行的代码 客户端(在一台计算机上): 服务器(位于地址为“”的另一台计算机上) #/usr/bin/env python ''' 此文件在计算机中不确定地运行,并等待客户端 请求与机器人的连接 ''' #进口roslib; #进口罗西 #从传感器_msgs.msg导入压缩图像 #从传感器_msgs.msg导入图像 #从std_msgs.msg导入字

我试图通过XML RPC连接两台计算机,但我总是收到“[Errno-2]名称或服务未知”消息。有时有效,有时无效,我不知道我做错了什么。下面是我正在运行的代码

客户端(在一台计算机上):

服务器(位于地址为“”的另一台计算机上)

#/usr/bin/env python
'''
此文件在计算机中不确定地运行,并等待客户端
请求与机器人的连接
'''
#进口roslib;
#进口罗西
#从传感器_msgs.msg导入压缩图像
#从传感器_msgs.msg导入图像
#从std_msgs.msg导入字符串
从SimpleXMLRPCServer导入SimpleXMLRPCServer
导入子流程
从时间上导入睡眠
导入字符串
pub_data=None#从发布服务器获取数据
python_string=string
位置=['1','2','3','4','5','6','7','8','9','10']
已占用=['n','n','n','n','n','n','n','n','n','n']
标记=['n','n','n','n','n','n','n','n','n','n']
dateTime=['None','None','None','None','None','None','None','None','None',
“无”、“无”]
licensePlate=['None','None','None','None','None','None','None','None','None',
“无”、“无”、“无”]
gps=['None','None','None','None','None','None','None','None','None',
“无”、“无”]
def parking_回调(消息):
#解析字符串数据
infoList=msg.data.split(“”)
位置[int(infoList[0])-1]=infoList[0]
已占用[int(信息列表[0])-1]=信息列表[1]
标记的[int(infoList[0])-1]=infoList[2]
dateTime[int(infoList[0])-1]=infoList[3]
licensePlate[int(infoList[0])-1]=infoList[4]
def getLocationInfo(locationNum):
如果((locationNum>10)和(locationNum<1)):
打印“越界位置\n”
返回
索引=locationNum-1
description=('ID'、'occumped'、'flagged'、'dateTime',
‘牌照’、‘全球定位系统’)
描述数据=(位置[索引]、占用[索引],
标记的[索引],日期时间[索引],
牌照[索引]、全球定位系统[索引])
返回dict(zip(描述、描述数据))
def start_进程():
'''
允许客户端从此服务器检索数据的函数
'''
robot_地址=['http://G46VW:11311', '192.168.1.81']
打印(str(len(robot_地址))+“robots已连接\n”)
#robot_address.append(os.environ['ROS_MASTER_URI'])
robot_address.reverse()#将uri更改为第一个键
返回地址
#返回位置
def获取发布数据(已发布数据):
'''
从ROS检索已发布的数据并将其存储在
pub_数据变量
'''
#发布数据=发布的数据
通过
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
#rospy.init_节点('sendGUI')
#rospy.Subscriber('/parkininfo',字符串,parking\u回调)
导入平台
hostname=platform.node()
端口=12345
服务器=SimpleXMLRPCServer((主机名,端口))
#在这里运行订阅服务器
#rospy.Subscriber(“移动”、字符串、漫游器.set\u移动)
#运行订阅服务器的线程
#从线程导入线程
#spin_thread=thread(target=lambda:rospy.spin())
#spin_thread.start()
#存储服务器地址以供元客户端使用
start=“http://”
地址=开始+主机名+“:”+str(端口)
打印('机器人URI:'+地址)
uri_文件=打开(“机器人_文件”,“w”)
uri_file.write(地址)
uri_file.close()
#向客户端发送URI
#导入操作系统
#system(“rsync-v-e ssh-robot\u文件pototo@192.168.1.102:~/Desktop/gui)
服务器注册功能(启动进程,“启动进程”)
服务器注册函数(getLocationInfo,“getLocationInfo”)
#而不是rospy.is_shutdown():
尽管如此:
try:#处理程序其余部分的请求
打印('\n等待客户端请求…\n\n')
server.handle_请求()
除了BaseException:#rospy.ROSInterruptException:
打印('正在退出ROS节点')
#spin_thread.kill()
从系统导入退出
退出()
服务器是一个打算作为ROS节点运行的节点,但我在这里将其转换回普通python,以防您想要运行它。如果要运行它,请确保相应地更改URI


非常感谢

通常,该错误仅表示无法连接到指定的主机。你有没有试过敲击那些机器以确保它们正常工作?也可能是python无法解析.local主机(如果我没有弄错的话,这是Apple的bonjour协议的一部分),您可能需要使用IP地址来代替。

我明白了。这里的主机名是假的,我硬编码它只是为了作为一个例子,但它会因机器而异。我通常是这样得到主机名的。import platform hostname=platform.node()我确实尝试过使用IP地址而不是主机名,但这样做时它甚至无法连接。我将尝试在运行代码时持续ping机器,看看会发生什么。谢谢你的帮助!
from xmlrpclib import ServerProxy
import time
while True:
    try:
#        robot_file = open("robot_file", "r")
#        robot_uri = robot_file.readline()
        robot_uri = "http://pototo.local:12345"
        print("robot uri: " + robot_uri)
        server = ServerProxy(robot_uri)
#        robot_file.close()
#        os.remove("robot_file")
        print('removed robot_file')
        break
    except BaseException:
        print("trying to read robot uri")
        time.sleep(1)
        pass
print('processing request')
while True:
    try:
        print (server.getLocationInfo(2))
        time.sleep(1)
    except BaseException as e:
        print("trying to read request")
        print (e)
        time.sleep(1)
#!/usr/bin/env python
'''
This files runs indefinitelly  in a computer and waits for a client
to request connection to a/the robot(s)
'''

#import roslib;
#import rospy
#from sensor_msgs.msg import CompressedImage
#from sensor_msgs.msg import Image
#from std_msgs.msg import String

from SimpleXMLRPCServer import SimpleXMLRPCServer
import subprocess
from time import sleep
import string

pub_data = None    # gets the data from the publisher

python_string = string
location = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
occupied = ['n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n']
flagged = ['n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n', 'n']
dateTime = ['None','None','None','None','None','None','None','None',
            'None','None']
licensePlate = ['None','None','None','None','None','None','None',
                'None','None','None']
gps = ['None','None','None','None','None','None','None','None',
       'None','None']

def parking_callback(msg):

        #parses the string data
        infoList = msg.data.split(" ")
        location[int(infoList[0]) -1] = infoList[0]
        occupied[int(infoList[0]) -1] = infoList[1]
        flagged[int(infoList[0]) -1] = infoList[2]
        dateTime[int(infoList[0]) -1] = infoList[3]
        licensePlate[int(infoList[0]) -1] = infoList[4]

def getLocationInfo(locationNum):
    if ((locationNum > 10) and (locationNum < 1)):
        print "Out of bounds location\n"
        return
    index = locationNum - 1
    description = ('ID', 'occupied', 'flagged', 'dateTime',
                   'licensePlate', 'gps')
    descrip_data = (location[index], occupied[index],
                    flagged[index], dateTime[index],
                    licensePlate[index], gps[index])
    return dict(zip(description, descrip_data))

def start_process():
    '''
    Function that allows client to retrieve data from this server
    '''
    robot_address = ['http://G46VW:11311', '192.168.1.81']
    print(str(len(robot_address)) + ' robots are connected \n')
#    robot_address.append(os.environ['ROS_MASTER_URI'])
    robot_address.reverse()    # change uri as first key
    return robot_address
#    return location

def get_pub_data(published_data):
    '''
    Retrieves the published data from ROS and stores it on the
    pub_data variable
    '''
#   pub_data = published_data
    pass

if __name__ == '__main__':
    #rospy.init_node('sendGUI')
    #rospy.Subscriber('/parkingInfo', String, parking_callback)
    import platform
    hostname = platform.node()
    port = 12345
    server = SimpleXMLRPCServer((hostname, port))

    # run subscriber here
#    rospy.Subscriber("move", String, rover.set_move)

    # thread to run the subscriber
    #from threading import Thread
    #spin_thread = Thread(target=lambda: rospy.spin())
    #spin_thread.start()

    # store server address for meta client usage
    start = "http://"
    address = start + hostname + ":" + str(port)
    print('Robot URI: ' + address)
    uri_file = open("robot_file", "w")
    uri_file.write(address)
    uri_file.close()

    # send URI to client
#    import os
#    os.system("rsync -v -e ssh robot_file pototo@192.168.1.102:~/Desktop/gui")

    server.register_function(start_process, "start_process")
    server.register_function(getLocationInfo, "getLocationInfo")
    #while not rospy.is_shutdown():
    while True:
        try:       # handle requests for the rest of the program
            print('\nwaiting for a client to request...\n\n')
            server.handle_request()
        except BaseException: #rospy.ROSInterruptException:
            print('exiting ROS node')
#            spin_thread.kill()
            from sys import exit
            exit()