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
  • 24,552
  • 41
  • 131
  • 206

1 Answers1

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'
Sunitha
  • 11,777
  • 2
  • 20
  • 23