2

My code is:

self.ip = self.bot.get('https://icanhazip.com').content.rstrip().decode()

I want to replace https://icanhazip.com with https://ipinfo.io, but I don't know how to extract the ip returned:

$ curl "ipinfo.io"
{
  "ip": "xx.xx.xx.xx",
  "city": "",
  "region": "",
  "country": "xx",
  "loc": "xx.xxx.xx.xxx",
  "org": "xxxxxxx Communication Company (Private Joint Stock)"
}
0

1 Answer 1

1

It's not clear which HTTP library you're using in self.bot.get, but for a generic one (where content returns a string), you can parse the JSON response manually with json.loads:

import json
# ...
ip = json.loads(self.bot.get('https://ipinfo.io').content).get('ip')

If you're using requests, you can read the JSON response directly with json() method on the response:

ip = self.bot.get('https://ipinfo.io').json().get('ip')
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks how can i sure which HTTP library im using in 'self.bot.get'
Take a look at self.bot definition, probably in your class __init__ method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.