Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: can't concat str to bytes #3

Open
zilveer opened this issue Sep 2, 2017 · 6 comments
Open

TypeError: can't concat str to bytes #3

zilveer opened this issue Sep 2, 2017 · 6 comments

Comments

@zilveer
Copy link

zilveer commented Sep 2, 2017

Hello,
I am running the tutorial found at: https://nclib.readthedocs.io/en/latest/netcat.html and have issues to run it.
Can you please tell me what the issue could be?

This is the commands I run as well as the error message shown.

>>> import nclib
>>> nc = nclib.Netcat(('127.0.0.1', 50001), udp=True, verbose=True)
>>> nc.send('\x00\x0dHello, world!')
======== Sending (15) ========
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/dist-packages/nclib/netcat.py", line 504, in se
nd
    self._log_send(s)
  File "/usr/local/lib/python3.6/dist-packages/nclib/netcat.py", line 230, in _l
og_send
    self._log_something(data, self.echo_send_prefix)
  File "/usr/local/lib/python3.6/dist-packages/nclib/netcat.py", line 213, in _l
og_something
    self._print_lines(data, prefix)
  File "/usr/local/lib/python3.6/dist-packages/nclib/netcat.py", line 237, in _p
rint_lines
    print(prefix + line)
TypeError: can't concat str to bytes

Thanks for your reply.

@rhelmot
Copy link
Owner

rhelmot commented Sep 2, 2017

Hi! If you are using python 3, you have to send bytes objects instead of strings. So you should say nc.send(b'\x00\x0dHello, world!'). I will update the example accordingly.

@zilveer
Copy link
Author

zilveer commented Sep 2, 2017

Hi,
in python2.7 I get this error message:

zilveer@9eb9:~$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import nclib
>>> nc = nclib.Netcat(('127.0.0.1', 50001), udp=True, verbose=True)
>>> nc.send('\x00\x0dHello, world!')
======== Sending (15) ========
Hello, world!
>>> nc.recv()
======== Receiving 4096B or until timeout (default) ========
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/nclib/netcat.py", line 335, in re
cv
    raise NetcatError('Socket error!')
nclib.errors.NetcatError: Socket error!

and in python3 (3.6.2) I get this error:

zilveer@9eb9:~$ python3
Python 3.6.2 (default, Jul 20 2017, 08:43:29)
[GCC 5.4.1 20170519] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import nclib
>>> nc = nclib.Netcat(('127.0.0.1', 50001), udp=True, verbose=True)
>>> nc.send(b'\x00\x0dHello, world!')
======== Sending (15) ========
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/dist-packages/nclib/netcat.py", line 504, in se
nd
    self._log_send(s)
  File "/usr/local/lib/python3.6/dist-packages/nclib/netcat.py", line 230, in _l
og_send
    self._log_something(data, self.echo_send_prefix)
  File "/usr/local/lib/python3.6/dist-packages/nclib/netcat.py", line 213, in _l
og_something
    self._print_lines(data, prefix)
  File "/usr/local/lib/python3.6/dist-packages/nclib/netcat.py", line 236, in _p
rint_lines
    for line in s.split('\n'):
TypeError: a bytes-like object is required, not 'str'

So I still get the errors :/

@rhelmot
Copy link
Owner

rhelmot commented Sep 2, 2017

I was unable to reproduce your py2 bug, but I have fixed the string literals to be bytestring literals so that py3 will work. I'm not sure what to tell you about your py2 issue! It looks like something weird going on with your operating system and its ability to let you use a UDP socket.

@zilveer
Copy link
Author

zilveer commented Sep 2, 2017

@rhelmot I use Ubuntu 16.04, maybe Ubuntu doesnt have full support for python, I dont know? Is Debian better?

When I set udp=False I get the same issue.

What I want to achieve with nclib, correct me if I am wrong, is to:

  1. is to initiate a netcat connection, in my case to TCP
  2. Send a command through TCP instance
  3. Get the result to python.

Isnt nclib for that purpose?

@rhelmot
Copy link
Owner

rhelmot commented Sep 2, 2017

Yeah absolutely, that's 100% the use case. I'm using ubuntu 16.10 right now and it's definitely working for me.

image

The only thing I can think of is for you to look at what strace has to say about what's happening under the covers in python.

@zilveer
Copy link
Author

zilveer commented Sep 2, 2017

Ok thanks for confirm to my questions.
But I still get the same issue, both on Ubuntu 16.04, Centos 7 and Windows 7.

Really strange error:

======== Receiving 4096B or until timeout (default) ========
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/nclib/netcat.py", line 335, in re
cv
    raise NetcatError('Socket error!')
nclib.errors.NetcatError: Socket error!

Could it be something wrong on the Socket library ?

I have never used strace so I dont know how to use it:/

Btw, should this python code do something similiar to nclib?

import socket

def netcat(host, port, content):
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, int(port)))
    s.sendall(content.encode())
	s.sendall(content.encode())
    s.shutdown(socket.SHUT_WR)
    while True:
        data = s.recv(4096)
        if not data:
            break
        print("Rec: ".repr(data))
    print("Closed Connection")
    s.close()
	
netcat('127.0.0.1', 50001, 'Testinh')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants