Skip to content

Commit

Permalink
Task
Browse files Browse the repository at this point in the history
  • Loading branch information
root authored and root committed Nov 13, 2024
1 parent b7370fa commit 2c57dcb
Show file tree
Hide file tree
Showing 12 changed files with 620 additions and 0 deletions.
16 changes: 16 additions & 0 deletions 0x11-python-network_1/0-hbtn_status.py
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")))
14 changes: 14 additions & 0 deletions 0x11-python-network_1/1-hbtn_header.py
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"))
33 changes: 33 additions & 0 deletions 0x11-python-network_1/10-my_github.py
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)
43 changes: 43 additions & 0 deletions 0x11-python-network_1/100-github_commits.py
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)
17 changes: 17 additions & 0 deletions 0x11-python-network_1/2-post_email.py
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"))
18 changes: 18 additions & 0 deletions 0x11-python-network_1/3-error_code.py
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))
10 changes: 10 additions & 0 deletions 0x11-python-network_1/4-hbtn_status.py
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))
13 changes: 13 additions & 0 deletions 0x11-python-network_1/5-hbtn_header.py
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"))
14 changes: 14 additions & 0 deletions 0x11-python-network_1/6-post_email.py
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)
16 changes: 16 additions & 0 deletions 0x11-python-network_1/7-error_code.py
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)
23 changes: 23 additions & 0 deletions 0x11-python-network_1/8-json_api.py
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")
Loading

0 comments on commit 2c57dcb

Please sign in to comment.