Junior
//Task condition //Write code that will output the response codes for HTTP requests to two URLs //the main page of Google and the main page of shtrafovnet.ru) //Requests should be made in parallel.
sobes.tech AI
Answer from AI
To perform parallel HTTP requests to two URLs, you can use multithreading or asynchronous calls. Below is an example in Python using the concurrent.futures module and the requests library:
import requests
from concurrent.futures import ThreadPoolExecutor
def fetch_status(url):
try:
response = requests.get(url)
print(f"URL: {url} - Status Code: {response.status_code}")
except Exception as e:
print(f"Error requesting {url}: {e}")
urls = ["https://www.google.com", "https://shtrafovnet.ru"]
with ThreadPoolExecutor(max_workers=2) as executor:
executor.map(fetch_status, urls)
This code creates two parallel requests and outputs the response codes.