socialwifi / RouterOS-api

Python API to RouterBoard devices produced by MikroTik.
MIT License
255 stars 98 forks source link

Set true value got "AttributeError: 'bool' object has no attribute 'encode" #26

Closed oortega closed 4 years ago

oortega commented 6 years ago

Hello,

I tried to set True value for existing record but I got this Error: AttributeError: 'bool' object has no attribute 'encode"

I do this:

dhcp_lease = api.get_resource('/ip/dhcp-server/lease')
ip_lease = dhcp_lease.get(address="192.168.3.219")
ip_lease_tmp = ip_lease[0]
static_lease = dhcp_lease.set(id=ip_lease_tmp["id"], dynamic=False)

This ouput of ip_lease

[{u'status': u'bound', u'client-id': u'1:8:d4:2b:1f:cf:4c', u'active-server': u'dhcp1', u'dhcp-option': u'', u'dynamic': u'false', u'active-mac-address': u'08:D4:2B:1F:CF:4C', u'server': u'dhcp1', u'disabled': u'false', u'address-lists': u'', u'host-name': u'android-9b69027d023f094f', u'active-client-id': u'1:8:d4:2b:1f:cf:4c', u'mac-address': u'08:D4:2B:1F:CF:4C', u'address': u'192.168.3.219', u'radius': u'false', u'active-address': u'192.168.3.219', u'last-seen': u'1m31s', u'expires-after': u'8m29s', u'id': u'*FF', u'blocked': u'false'}]

Traceback (most recent call last):
  File "/root/.pycharm_helpers/pydev/_pydevd_bundle/pydevd_exec.py", line 3, in Exec
    exec exp in global_vars, local_vars
  File "<input>", line 1, in <module>
  File "/var/waps/entornos/ahkin/local/lib/python2.7/site-packages/routeros_api/resource.py", line 19, in set
    return self.call('set', kwargs)
  File "/var/waps/entornos/ahkin/local/lib/python2.7/site-packages/routeros_api/resource.py", line 39, in call
    additional_queries=additional_queries).get()
  File "/var/waps/entornos/ahkin/local/lib/python2.7/site-packages/routeros_api/resource.py", line 58, in call_async
    arguments = self.transform_dictionary(arguments or {})
  File "/var/waps/entornos/ahkin/local/lib/python2.7/site-packages/routeros_api/resource.py", line 66, in transform_dictionary
    return dict(self.transform_item(item) for item in dictionary.items())
  File "/var/waps/entornos/ahkin/local/lib/python2.7/site-packages/routeros_api/resource.py", line 66, in <genexpr>
    return dict(self.transform_item(item) for item in dictionary.items())
  File "/var/waps/entornos/ahkin/local/lib/python2.7/site-packages/routeros_api/resource.py", line 70, in transform_item
    return (key, self.structure[key].get_mikrotik_value(value))
  File "/var/waps/entornos/ahkin/local/lib/python2.7/site-packages/routeros_api/api_structure.py", line 15, in get_mikrotik_value
    return string.encode()
AttributeError: 'bool' object has no attribute 'encode'

For the moment I delete old record and then add new record, is the best way?

Example:

static_lease = dhcp_lease.remove(id=ip_lease_tmp["id"])
new_ip_lease = dhcp_lease.add(address=ip_lease_tmp["address"], mac_address= ip_lease_tmp["mac-address"])

Any idea about? "Error: AttributeError: 'bool' object has no attribute 'encode"

Many thanks for your project.

Behoston commented 4 years ago
from routeros_api import api_structure

structure =  collections.defaultdict(api_structure.StringField)
structure['dynamic'] = api_structure.BooleanField
ip_lease = dhcp_lease.get(address="192.168.3.219", structure=structure)
...