question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeError: can't concat str to bytes?

See original GitHub issue

I got this TypeError: can't concat str to bytes idk why,i just trying simple code like this

import routeros_api as routers

connection = routers.RouterOsApiPool('IP', username='username', password='password')
api = connection.get_api()
api.get_binary_resource('/').call('ping', { 'address': '192.168.56.1', 'count': '4' }) #this ip i got from example

This Example i Used

And full traceback:

Traceback (most recent call last):
  File "mikro.py", line 5, in <module>
    api.get_binary_resource('/').call('ping', { 'address': '192.168.56.1', 'count': '4' })
  File "/home/sengkunibot/.local/lib/python3.7/site-packages/routeros_api/resource.py", line 39, in call
    additional_queries=additional_queries).get()
  File "/home/sengkunibot/.local/lib/python3.7/site-packages/routeros_api/resource.py", line 45, in call_async
    additional_queries=additional_queries)
  File "/home/sengkunibot/.local/lib/python3.7/site-packages/routeros_api/api_communicator/encoding_decorator.py", line 12, in call
    path, command, arguments, queries, additional_queries)
  File "/home/sengkunibot/.local/lib/python3.7/site-packages/routeros_api/api_communicator/async_decorator.py", line 6, in call
    tag = self.inner.send(*args, **kwargs)
  File "/home/sengkunibot/.local/lib/python3.7/site-packages/routeros_api/api_communicator/exception_decorator.py", line 11, in send
    return self.inner.send(*args, **kwargs)
  File "/home/sengkunibot/.local/lib/python3.7/site-packages/routeros_api/api_communicator/key_cleaner_decorator.py", line 11, in send
    queries=encoded_queries, additional_queries=additional_queries)
  File "/home/sengkunibot/.local/lib/python3.7/site-packages/routeros_api/api_communicator/base.py", line 19, in send
    self.send_command(command)
  File "/home/sengkunibot/.local/lib/python3.7/site-packages/routeros_api/api_communicator/base.py", line 37, in send_command
    self.base.send_sentence(command.get_api_format())
  File "/home/sengkunibot/.local/lib/python3.7/site-packages/routeros_api/sentence.py", line 59, in get_api_format
    formated.append(b'=' + key + b'=' + value)
TypeError: can't concat str to bytes

My server:

  • Ubuntu 16.04.5 LTS (GNU/Linux 4.4.0-31-generic x86_64)
  • Python3.7

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
okazdalcommented, Jul 8, 2019

I had the same issue. I modified sentence.py file from library. Added lines with **. Seems to fix this but I don’t know about any other side effects

def get_api_format(self):
    formated = [self.path + self.command]
    for key, value in self.attributes.items():
        **if type(value) == str:**
            **value = str.encode(value)**
        formated.append(b'=' + key + b'=' + value)
    for query in self.queries:
        formated.extend(query.get_api_format())
    if self.tag is not None:
        formated.append(b'.tag=' + self.tag)
    return formated
2reactions
stevehaskewcommented, Apr 11, 2019

Two parts to the answer:

Simple way to make it work is to change the fields to bytes, like this:

import routeros_api as routers

connection = routers.RouterOsApiPool('IP', username='username', password='password')
api = connection.get_api()
api.get_binary_resource('/').call('ping', { 'address': b'192.168.56.1', 'count': b'4' }) #this ip i got from example

However, it does raise the question about whether under Python3 that we should be handling better.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Can't concat bytes to str - Stack Overflow
subprocess.check_output() returns a bytestring. In Python 3, there's no implicit conversion between unicode ( str ) objects and bytes objects.
Read more >
TypeError : cant concat str to bytes ( Solved )
The main or root cause of this error is that you are concatenating the string type to the bytes type. Mainly when you...
Read more >
can't concat str to bytes - When configuring vlans on switch : r ...
The (b"... means that you want to work with bytes. You need to convert the str(n) to bytes too. Try - tn.write(b"vlan "...
Read more >
TypeError: can't concat str to bytes` #25 - GitHub
the reason is str and bytes can't combine in python3, but it can be in python2. so we need to change str to...
Read more >
makecat.py fails under python 3: TypeError: can't concat bytes ...
This is wrong -- workingcatname.encode('ascii', 'xmlcharrefreplace') will create a bytes object, and this cannot be concatenated with the '_exclude.txt' string.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found