0

How do I set the result of curl command to a variable, (So in this the ip address.)

import subprocess; ip = subprocess.call("curl ident.me", shell=True)
120.12.12.12

ip returns "0" 
2

1 Answer 1

4

Better to use requests module than running curl through shell

>>> import requests
>>> r = requests.get('http://ident.me')
>>> r.text
'137.221.143.78'
>>> 

If for some reason, you cant use requests module, then try using subprocess.check_output

>>> ip = subprocess.check_output("curl -s ident.me".split())
>>> ip
'137.221.143.78'
Sign up to request clarification or add additional context in comments.

Comments

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.