使用字符串

时间:2017-12-22 16:53:04

标签: python python-3.x xml-rpc

我的用例是:

  • 客户端通过xmlrpc发送数据
  • xmlrpc服务器检索这些数据,以便使用它作为参数
  • 调用该函数

此处转载的代码会出现以下错误:

Traceback (most recent call last):
  File "rpcclient.py", line 37, in <module>
    s.on(pinaddr)
  File "/usr/lib/python3.5/xmlrpc/client.py", line 1092, in __call__
    return self.__send(self.__name, args)
  File "/usr/lib/python3.5/xmlrpc/client.py", line 1432, in __request
    verbose=self.__verbose
  File "/usr/lib/python3.5/xmlrpc/client.py", line 1134, in request
    return self.single_request(host, handler, request_body, verbose)
  File "/usr/lib/python3.5/xmlrpc/client.py", line 1150, in single_request
    return self.parse_response(resp)
  File "/usr/lib/python3.5/xmlrpc/client.py", line 1322, in parse_response
    return u.close()
  File "/usr/lib/python3.5/xmlrpc/client.py", line 655, in close
    raise Fault(**self._stack[0])
xmlrpc.client.Fault: <Fault 1: "<class 'SystemError'>:<built-in function setup> returned NULL without setting an error">

我的测试和尝试:

  1. 来自下面的服务器代码:print(type(pin_address))返回 class&#39; str&#39; 所以这很好。
  2. 如果我调用功能硬编码服务器端: GPIO.setup(&#34; P8_45&#34;,GPIO.OUT)它也很好
  3. 如果我使用print(s.on(&#34; P8_45&#34;)):没关系。它在代码中留下了评论
  4. 如果我强迫pinaddr =&#34; p8_45&#34;客户端,它失败了。它在代码中留下了评论
  5. CLI行是:

    python3 rpcclient.py -s 192.168.1.2 on -p p8_45
    

    客户端代码

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    import xmlrpc.client
    import argparse
    import time
    
    RPC_PORT = 1234
    RPC_HOSTNAME = "####"
    
    parser = argparse.ArgumentParser()
    parser.add_argument("-s", "--server", help="Specify ACME Hostname")
    parser.add_argument("-p", "--pinaddress", help="Specify which pin to use")
    parser.add_argument("command", help="ACME Command, use 'help' to get all commands")
    parser.add_argument("args", nargs='?', help="ACME Command Arguments")
    args = parser.parse_args()
    
    if args.server:
        serveraddr = "%s:%d" % (args.server, RPC_PORT)
    else:
        serveraddr = "%s:%d" % (RPC_HOSTNAME, RPC_PORT)
    
    s = xmlrpc.client.ServerProxy("http://%s/ci" % serveraddr)
    
    if args.pinaddress:
        pinaddr = "%s" % (args.pinaddress)
    #    pinaddr = "p8_45"  
        print ("using pinaddr = ", pinaddr)
    
    if args.command == "version":
        print(s.version())
    
    if args.command == "on":
        print(s.on(pinaddr))
    #    print(s.on("P8_45"))   this will be accepted
    
    if args.command == "off":
        print(s.off(pinaddr))
    

    服务器代码

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-
    '''
    Python RPC Daemon
    '''
    from xmlrpc.server import SimpleXMLRPCServer
    from xmlrpc.server import SimpleXMLRPCRequestHandler
    import subprocess
    import Adafruit_BBIO.GPIO as GPIO    
    VERSION = "123"
    
    def run_cmd(cmd, arg=""):
        try:
            return subprocess.Popen([cmd, arg], \
                                    stdout=subprocess.PIPE, \
                                    stderr=subprocess.PIPE).communicate()
        except:
            return False, False
    
    class RequestHandler(SimpleXMLRPCRequestHandler):
        rpc_paths = ('/ci',)
    
    # Create server
    server = SimpleXMLRPCServer(("0.0.0.0", 1234),
                                requestHandler=RequestHandler)
    server.register_introspection_functions()
    
    # Info function, get ACME info string
    def version():
        return VERSION
    server.register_function(version)
    
    def on(pin_address):
        print(type(pin_address))
        GPIO.setup( (pin_address) , GPIO.OUT)
        return ("on for ", (pin_address))
    
    server.register_function(on)
    
    def off(pin_address):
        GPIO.setup(pin_address,GPIO.IN)
        return ("off for ", pin_address)
    
    server.register_function(off)
    
    # Run the server's main loop
    server.serve_forever()
    

1 个答案:

答案 0 :(得分:0)

实际上这是来自一个空间字符,它没有从服务器端正确解析。