Merlin
Merlin

Reputation: 25639

Get output from curl command in python.

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" 

Upvotes: 0

Views: 3397

Answers (1)

Sunitha
Sunitha

Reputation: 12015

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'

Upvotes: 4

Related Questions