-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
root
authored and
root
committed
Nov 13, 2024
1 parent
b7370fa
commit 2c57dcb
Showing
12 changed files
with
620 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/python3 | ||
"""a Python script that fetches `https://alx-intranet.hbtn.io/status` | ||
requirements: use the package `urllib`, a `with` statement | ||
and have (tabulation before -)""" | ||
import urllib.request | ||
|
||
|
||
if __name__ == "__main__": | ||
url = urllib.request.Request("https://alx-intranet.hbtn.io/status") | ||
with urllib.request.urlopen(url) as response: | ||
content = response.read() | ||
|
||
print("Body response:") | ||
print("\t- type: {}".format(type(content))) | ||
print("\t- content: {}".format(content)) | ||
print("\t- utf8 content: {}".format(content.decode("utf-8"))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/python3 | ||
"""a Python script that takes in a URL, | ||
sends a request to the URL and | ||
displays the value of the variable `X-Request-Id` in the response header""" | ||
import sys | ||
import urllib.request | ||
|
||
|
||
if __name__ == "__main__": | ||
url = sys.argv[1] | ||
|
||
request = urllib.request.Request(url) | ||
with urllib.request.urlopen(request) as response: | ||
print(dict(response.headers).get("X-Request-Id")) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/python3 | ||
"""Uses the GitHub API to display a GitHub ID | ||
based on given credentials (username and password). | ||
Usage: ./10-my_github.py <GitHub username> <GitHub password> | ||
- Uses Basic Authentication (personal access token | ||
as `password`) to access the ID info.""" | ||
import sys | ||
import requests | ||
from requests.auth import HTTPBasicAuth | ||
|
||
|
||
if __name__ == "__main__": | ||
auth_ = HTTPBasicAuth(sys.argv[1], sys.argv[2]) | ||
r = requests.get("https://api.github.com/user", auth=auth_) | ||
print(r.json().get("id")) | ||
|
||
# import sys | ||
# import requests | ||
|
||
|
||
# if __name__ == "__main__": | ||
# username = sys.argv[1] | ||
# password = sys.argv[2] | ||
|
||
# url = 'https://api.github.com/user' | ||
# response = requests.get(url, auth=(username, password)) | ||
|
||
# if response.status_code == 200: | ||
# user_data = response.json() | ||
# user_id = user_data.get('id') | ||
# print(user_id) | ||
# else: | ||
# print(None) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
#!/usr/bin/python3 | ||
"""Lists the 10 most recent commits on a given GitHub repository. | ||
Usage: ./100-github_commits.py <repository name> <repository owner> | ||
""" | ||
import sys | ||
import requests | ||
|
||
|
||
if __name__ == "__main__": | ||
url = "https://api.github.com/repos/{}/{}/commits".format( | ||
sys.argv[2], sys.argv[1]) | ||
|
||
response = requests.get(url) | ||
commits = response.json() | ||
try: | ||
for i in range(10): | ||
print("{}: {}".format( | ||
commits[i].get("sha"), | ||
commits[i].get("commit").get("author").get("name"))) | ||
except IndexError: | ||
pass | ||
|
||
# import requests | ||
# import sys | ||
|
||
# if __name__ == "__main__": | ||
# repository_name = sys.argv[1] | ||
# owner_name = sys.argv[2] | ||
|
||
# url = f'https://api.github.com/repos/{owner_name}/{repository_name}' | ||
# response = requests.get(url) | ||
|
||
# if response.status_code == 200: | ||
# repository_data = response.json() | ||
# try: | ||
# for i in range(10): | ||
# print("{}: {}".format( | ||
# commits[i].get("sha"), | ||
# commits[i].get("commit").get("author").get("name"))) | ||
# except IndexError: | ||
# pass | ||
# else: | ||
# print("Error:", response.status_code) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/python3 | ||
"""a Python script that takes in a URL and an email, | ||
sends a `POST` request to the passed URL with the email as a parameter, | ||
and displays the body of the response (decoded in `utf-8`)""" | ||
import sys | ||
import urllib.parse | ||
import urllib.request | ||
|
||
|
||
if __name__ == "__main__": | ||
url = sys.argv[1] | ||
value = {"email": sys.argv[2]} | ||
data = urllib.parse.urlencode(value).encode("utf-8") | ||
|
||
request = urllib.request.Request(url, data=data, method="POST") | ||
with urllib.request.urlopen(request) as response: | ||
print(response.read().decode("utf-8")) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/usr/bin/python3 | ||
"""a Python script that takes in a URL, | ||
sends a request to the URL and | ||
displays the body of the response (decoded in `utf-8`).""" | ||
import sys | ||
import urllib.error | ||
import urllib.request | ||
|
||
|
||
if __name__ == "__main__": | ||
url = sys.argv[1] | ||
|
||
request = urllib.request.Request(url) | ||
try: | ||
with urllib.request.urlopen(request) as response: | ||
print(response.read().decode("utf-8")) | ||
except urllib.error.HTTPError as e: | ||
print("Error code: {}".format(e.code)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/python3 | ||
"""a Python script that fetches `https://intranet.hbtn.io/status`""" | ||
import requests | ||
|
||
|
||
if __name__ == "__main__": | ||
r = requests.get("https://alx-intranet.hbtn.io/status") | ||
print("Body response:") | ||
print("\t- type: {}".format(type(r.text))) | ||
print("\t- content: {}".format(r.text)) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/python3 | ||
"""a Python script that takes in a URL, | ||
sends a request to the URL and | ||
displays the value of the variable `X-Request-Id` in the response header""" | ||
import sys | ||
import requests | ||
|
||
|
||
if __name__ == "__main__": | ||
url = sys.argv[1] | ||
|
||
r = requests.get(url) | ||
print(r.headers.get("X-Request-Id")) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#!/usr/bin/python3 | ||
"""a Python script that takes in a URL and an email address, | ||
sends a POST request to the passed URL with the email as a parameter, | ||
and finally displays the body of the response.""" | ||
import sys | ||
import requests | ||
|
||
|
||
if __name__ == "__main__": | ||
url = sys.argv[1] | ||
value = {"email": sys.argv[2]} | ||
|
||
r = requests.post(url, data=value) | ||
print(r.text) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/python3 | ||
"""a Python script that takes in a URL, | ||
sends a request to the URL and | ||
displays the body of the response.""" | ||
import sys | ||
import requests | ||
|
||
|
||
if __name__ == "__main__": | ||
url = sys.argv[1] | ||
|
||
r = requests.get(url) | ||
if r.status_code >= 400: | ||
print("Error code: {}".format(r.status_code)) | ||
else: | ||
print(r.text) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/python3 | ||
"""a Python script that takes in a letter and | ||
sends a `POST` request to `http://0.0.0.0:5000/search_user` | ||
with the letter as a parameter.""" | ||
import sys | ||
import requests | ||
|
||
|
||
if __name__ == "__main__": | ||
url = "http://0.0.0.0:5000/search_user" | ||
letter = sys.argv[1] if len(sys.argv) > 1 else "" | ||
|
||
payload = {'q': letter} | ||
r = requests.post(url, data=payload) | ||
|
||
try: | ||
response = r.json() | ||
if response: | ||
print("[{}] {}".format(response.get("id"), response.get("name"))) | ||
else: | ||
print("No result") | ||
except ValueError: | ||
print("Not a valid JSON") |
Oops, something went wrong.