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" 
Merlin
  • 22,195
  • 35
  • 117
  • 197

1 Answers1

3

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'
Sunitha
  • 11,422
  • 2
  • 17
  • 22