3

大家,

我使用 Python3 和 fritzconnection 连接到我的 Fritzbox。获取信息效果很好,但也有一些命令您必须发送信息才能让 Fritzbox 做出具体响应。我怎么做?

这是我到目前为止所做的:

from fritzconnection import FritzConnection
fc = FritzConnection(password='MyRoommateSucks')
print(fc.call_action('WANCommonInterfaceConfig', 'GetAddonInfos'))
print(fc.call_action('WLANConfiguration:1', 'SetEnable', NewEnable = True))

所以第一个电话工作正常,但第二个电话没有。然而,这是我在 FritzConnection 类中使用的方法call_action

def call_action(self, service_name, action_name, **kwargs):
    """Executes the given action. Raise a KeyError on unkown actions."""
    action = self.services[service_name].actions[action_name]
    return action.execute(**kwargs)

我想如果我也把execute方法放在这里,我想我是对的,因为参数是给方法的。

def execute(self, **kwargs):
    """
    Calls the FritzBox action and returns a dictionary with the arguments.
    """
    headers = self.header.copy()
    headers['soapaction'] = '%s#%s' % (self.service_type, self.name)
    data = self.envelope.strip() % self._body_builder(kwargs)
    url = 'http://%s:%s%s' % (self.address, self.port, self.control_url)
    auth = None
    if self.password:
        auth=HTTPDigestAuth(self.user, self.password)
    response = requests.post(url, data=data, headers=headers, auth=auth)
    # lxml needs bytes, therefore response.content (not response.text)
    result = self.parse_response(response.content)
    return result

这是 fritzconnection 中用于parse_response的必要方法

def parse_response(self, response):
    """
    Evaluates the action-call response from a FritzBox.
    The response is a xml byte-string.
    Returns a dictionary with the received arguments-value pairs.
    The values are converted according to the given data_types.
    TODO: boolean and signed integers data-types from tr64 responses
    """
    result = {}
    root = etree.fromstring(response)
    for argument in self.arguments.values():
        try:
            value = root.find('.//%s' % argument.name).text
        except AttributeError:
            # will happen by searching for in-parameters and by
            # parsing responses with status_code != 200
            continue
        if argument.data_type.startswith('ui'):
            try:
                value = int(value)
            except ValueError:
                # should not happen
                value = None
        result[argument.name] = value
    return result

这是 Fritzbox 想要的:

Actionname:         SetEnable
                    ('NewEnable', 'in', 'boolean')

Actionname:         GetInfo
                    ('NewAllowedCharsPSK', 'out', 'string')
                    ('NewAllowedCharsSSID', 'out', 'string')
                    ('NewBSSID', 'out', 'string')
                    ('NewBasicAuthenticationMode', 'out', 'string')
                    ('NewBasicEncryptionModes', 'out', 'string')
                    ('NewBeaconType', 'out', 'string')
                    ('NewChannel', 'out', 'ui1')
                    ('NewEnable', 'out', 'boolean')
                    ('NewMACAddressControlEnabled', 'out', 'boolean')
                    ('NewMaxBitRate', 'out', 'string')
                    ('NewMaxCharsPSK', 'out', 'ui1')
                    ('NewMaxCharsSSID', 'out', 'ui1')
                    ('NewMinCharsPSK', 'out', 'ui1')
                    ('NewMinCharsSSID', 'out', 'ui1')
                    ('NewSSID', 'out', 'string')
                    ('NewStandard', 'out', 'string')
                    ('NewStatus', 'out', 'string')

摘要:如果我想发送信息,我的 call_action 的正确语法如何。

4

1 回答 1

2

信息由关键字参数发送(正如您所做的那样)。而不是使用 True|False 使用 1|0 来启用或禁用 WLAN。一些 FritzBox 还对 5GHz 频段使用“WLANConfiguration:2”,对访客网络使用“WLANConfiguration:3”。

fc.call_action('WLANConfiguration:1', 'SetEnable', NewEnable=1)
于 2019-03-18T17:23:31.303 回答