0

我已经编写了一个服务器来通过 dbus 在 avahi 上发布。相同的代码在 Python 2.7 中完美运行,但在 Python 3.7 中抛出错误

ERROR:dbus.connection:Unable to set arguments (dbus.Int32(-1), dbus.Int32(-1), dbus.UInt32(0), 'SETup', '_http._tcp', dbus.String('local'), 'surabhi-Latitude.local', dbus.UInt16(11732), dbus.Array(['f=abc', 't=test'], signature=dbus.Signature('ay'))) according to signature 'iiussssqaay': <class 'TypeError'>: an integer is required (got type str)
Traceback (most recent call last):
  File "myserver.py", line 17, in <module>
    avahiservice = AvahiService("SETup", "_http._tcp", current_port)
  File "/home/surabhi/Documents/set_buildv2/set_buildv2/avahiTest/service.py", line 42, in __init__
    dbus.Array(self.txt, signature='ay')) # TXT field, this is empty at the moment
  File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 70, in __call__
    return self._proxy_method(*args, **keywords)
  File "/usr/lib/python3/dist-packages/dbus/proxies.py", line 145, in __call__
    **keywords)
  File "/usr/lib/python3/dist-packages/dbus/connection.py", line 641, in call_blocking
    message.append(signature=signature, *args)
TypeError: an integer is required (got type str)

这是我的代码:

class AvahiService:

    def __init__(self, service_name, service_type, port, keep_alive=False):
        """Announce a service over Avahi through dbus

        service_name: string with service's name
        service_type: string with service's type, eg. '_http._tcp'
        port: integer with port number
        keep_alive: whether to keep running this server until interruption.
                    Default is False. Use False if you run this script within your server,
                    use True if you are running this script as standalone, because the service
                    disappears as soon as the script stops otherwise.

        """
        self.bus = dbus.SystemBus()
        self.avahiserver = AvahiServer()
        self.path = self.avahiserver.EntryGroupNew()
        raw_server = self.bus.get_object('org.freedesktop.Avahi', self.path)
        self.server = dbus.Interface(raw_server, 'org.freedesktop.Avahi.EntryGroup')

        hostname, domainname = self.avahiserver.GetHostName(), self.avahiserver.GetDomainName()
                
        '''txt: TXT fields as array of string in a format of ["key1=value1", "key2=value2"], by default it's empty (ie. [])'''
        self.txt = ["f=abc", "t=test"]
        
        self.server.AddService(dbus.Int32(-1), # avahi.IF_UNSPEC
                               dbus.Int32(-1), # avahi.PROTO_UNSPEC
                               dbus.UInt32(0), # flags
                               service_name, # sname
                               service_type, # stype
                               domainname, # sdomain
                               "{}.{}".format(hostname, domainname), # shost
                               dbus.UInt16(port), # port
                               dbus.Array(self.txt, signature='ay')) # TXT field, this is empty at the moment
                               
        self.server.Commit()
        if keep_alive:
            while True:
                sleep(60)

当我将 txt 作为空字段传递时,它工作正常。

4

1 回答 1

0

为了解决这个问题,我在我的代码中添加了以下函数:

def string_to_byte_array(s):
    r = []
    for c in s:
        r.append(dbus.Byte(ord(c)))
    return r
def string_array_to_txt_array(t):
    l = []
    for s in t:
        l.append(string_to_byte_array(s))
    return l

并调用string_array_to_txt_array()而不是dbus.Array()

于 2020-11-19T10:06:12.660 回答