Class in Python

Python: Curl Command

This tutorials will explain how to use curl command in python and extract data from the endpoints.


# import libraries
import json,os

# popen command to run command, followed by read to extract data in human readable form
curl_output = os.popen("curl http://date.jsontest.com").read()


print(curl_output)

Output: 
{
   "date": "10-09-2023",
   "milliseconds_since_epoch": 1696821725479,
   "time": "03:22:05 AM"
}


type(curl_output)

Output:
<class 'str'>

#Output of curl command is string and can be converted to json
curl_json = json.loads(curl_output)


# Desired field then can be extracted as per requirement
epoch = curl_json.get("milliseconds_since_epoch")
print(epoch)