-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathecho.py
40 lines (26 loc) · 789 Bytes
/
echo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import sys
import requests
import asyncio
from rich.console import Console
from rich.markdown import Markdown
console = Console()
async def fetch_content_async(url):
loop = asyncio.get_event_loop()
response = await loop.run_in_executor(None, requests.get, url)
return response
def fetch_content_sync(url):
response = requests.get(url)
return response
async def main():
if len(sys.argv) < 2:
console.print("Error: URL is missing!")
return
url = sys.argv[-1]
# sync request
response_sync = fetch_content_sync(url)
console.print(Markdown(response_sync.text))
# async
response_async = await fetch_content_async(url)
console.print(Markdown(response_async.text))
if __name__ == "__main__":
asyncio.run(main())