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: a bytes-like object is required, not 'str' #40

Open
lolfr opened this issue Aug 18, 2023 · 8 comments
Open

TypeError: a bytes-like object is required, not 'str' #40

lolfr opened this issue Aug 18, 2023 · 8 comments

Comments

@lolfr
Copy link

lolfr commented Aug 18, 2023

Hi,

I'm trying to configure this script on a new Raspberry Pi 4.
I already made some changes (i.e. adding () to the print command) but I still get these errors :

Traceback (most recent call last):
  File "/home/pi/configs/gandi-live-dns/src/./gandi-live-dns.py", line 122, in <module>
    main(args.force, args.verbose)
  File "/home/pi/configs/gandi-live-dns/src/./gandi-live-dns.py", line 101, in main
    dynIP = get_dynip(config.ifconfig)
  File "/home/pi/configs/gandi-live-dns/src/./gandi-live-dns.py", line 24, in get_dynip
    print('Checking dynamic IP: ' , r._content.strip('\n'))
TypeError: a bytes-like object is required, not 'str'

I'm not a developper, just an enthusiast and after several hours trying to understand what is the issue, I'm near to giving up.
Did someone as a clue ? Or can help me ?

Best regards,

Laurent

@CPC67128
Copy link

Hi,
Which version of python are you running?
python --version
(to type in a shell)
Steve

@lolfr
Copy link
Author

lolfr commented Aug 19, 2023

Hi,

It's the 3.9.2.

@CPC67128
Copy link

Hi,
What did you put in config file in ifconfig part?
ifconfig = 'choose_from_above_or_run_your_own'

@lolfr
Copy link
Author

lolfr commented Aug 19, 2023

I do several tried, with :
https://ifconfig.co/ip
http://ifconfig.me/ip
http://whatismyip.akamai.com/

But I always get the same errors :
Traceback (most recent call last): File "/home/pi/gandi-live-dns/src/./gandi-live-dns.py", line 121, in <module> main(args.force, args.verbose) File "/home/pi/gandi-live-dns/src/./gandi-live-dns.py", line 100, in main dynIP = get_dynip(config.ifconfig) File "/home/pi/gandi-live-dns/src/./gandi-live-dns.py", line 25, in get_dynip print ('Checking dynamic IP: ' , r._content.strip('\n')) TypeError: a bytes-like object is required, not 'str'

Btw, thanks for the help. 👍

@CPC67128
Copy link

I may be wrong but my feeling is that the issue is with the configuration file.
Either it is not properly named, not reachable by the script or having a wrong encoding.
I am adding my configuration file if this can help somehow.
Clipboard01
If not, I must say I am stuck with ideas.

@nomwerp
Copy link

nomwerp commented Aug 20, 2023

in python3 the requests library will return unicode string

try adding .decode('utf-8')

    r = requests.get(ifconfig_provider)
   print 'Checking dynamic IP: ' , r._content.decode('utf-8').strip('\n')
return r.content.decode('utf-8').strip('\n')

there are at least 2 open pull requests for python3 suppport

for example #35

you can try running that one instead

@lolfr
Copy link
Author

lolfr commented Aug 22, 2023

Hi nomwerp,

I try to add the utf-8 details and… there is some improvement. I doesn't have the type-error message anymore. But now I've got a "IndexError: list index out of range" error.
The parsing seems causing the issues :

`if name == "main":
parser = argparse.ArgumentParser()
parser.add_argument('-v', '--verbose', help="increase output verbosity", action="store_true")
parser.add_argument('-f', '--force', help="force an update/create", action="store_true")
args = parser.parse_args()

main(args.force, args.verbose)`

I'm really new in python, and development in general (except css, html and some shortcut). It's a whole new univers for me to discover. ^_^“

Anyway, thanks for your help.

@lolfr
Copy link
Author

lolfr commented Aug 22, 2023

And CPC67128, thanks a lot for the help. 👍

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

3 participants