首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在fritzconnect中发送call_action

如何在fritzconnect中发送call_action
EN

Stack Overflow用户
提问于 2018-09-23 20:35:36
回答 1查看 827关注 0票数 3

各位,

我使用Python3和fritzconnection连接到我的Fritzbox。获取信息非常有用,但也有一些命令需要发送信息才能让Fritzbox做出具体的响应。我该怎么做?

以下是我迄今所做的工作:

代码语言:javascript
复制
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方法:

代码语言:javascript
复制
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)

如果我也将执行方法放在这里,我想我假设是正确的,因为参数提供给了该方法。

代码语言:javascript
复制
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

下面是parse_response的必要的fritzconnection方法

代码语言:javascript
复制
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想要的是:

代码语言:javascript
复制
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的语法是如何正确的。

EN

回答 1

Stack Overflow用户

发布于 2019-03-18 17:23:31

信息由关键字参数发送(正如您所做的那样)。而不是使用True,而是使用1\0启用或禁用WLAN。一些FritzBoxes还使用WLANConfiguration:2作为5 5GHz频段,WLANConfiguration:3用于来宾网络。

代码语言:javascript
复制
fc.call_action('WLANConfiguration:1', 'SetEnable', NewEnable=1)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52470038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档