Last active
December 3, 2020 00:32
-
-
Save simora/329451d942dc6c74d029fbab05434027 to your computer and use it in GitHub Desktop.
A simple python script to start learning python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
""" | |
The following script will help you learn python by poking at a simple url | |
that returns a known value. You can identify what you want to extract, | |
format as necessary and print it to illustrate your mastery. | |
""" | |
# A variable to use defined that the top of the script | |
URL = 'http://ifconfig.co/json' | |
""" | |
This is a simple http request which returns a response object. | |
The response object has a function called json which returns a python native | |
data type, typically a dictionary or a list of dictionaries, decoded from the json response. | |
""" | |
response = requests.get(URL).json() | |
print(f"The following is the JSON reponse") | |
print(json.dumps(response,indent=2)) | |
print("") | |
print(f"IP Address : {response['ip']}") | |
# the variable[str] syntax is how you access values fron a dictionary by identifying the key as a string | |
# accessing a key that doesn't exist will throw an exception. what exception will it throw? | |
print(f"Hostname : {response['hostname']}") | |
try: | |
print(f"Hotname : {response['hotname']}") | |
except Exception as e: | |
print(f"Exception caught") | |
print(f"Type : {type(e)}") | |
response['hotname'] = 'Lisa' | |
print(f"Hotname : {response['hotname']}") | |
print("This could be our chance Wyatt") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment