面试题答案
一键面试import threading
import requests
def fetch_url(url):
try:
response = requests.get(url)
print(f"URL: {url}, Status Code: {response.status_code}")
except requests.RequestException as e:
print(f"Error fetching {url}: {e}")
if __name__ == "__main__":
urls = [
'https://www.example.com',
'https://www.github.com'
]
threads = []
for url in urls:
thread = threading.Thread(target=fetch_url, args=(url,))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()