-
Notifications
You must be signed in to change notification settings - Fork 0
/
tes.py
38 lines (30 loc) · 1.2 KB
/
tes.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
import requests
import concurrent.futures
import time
# Function to fetch data from the API
def fetch_data(thread_id):
try:
response = requests.get(
'https://bjporzn.cyou/home.html') # Set a timeout
if response.status_code == 200:
try:
data = response # Try parsing JSON
print(f"Thread {thread_id} - Fetched data: {data}")
except ValueError:
print(
f"Thread {thread_id} - Error: Received invalid JSON response")
else:
print(
f"Thread {thread_id} - Failed to fetch data. Status code: {response.status_code}")
except requests.exceptions.Timeout:
print(f"Thread {thread_id} - Request timed out")
except requests.exceptions.RequestException as e:
print(f"Thread {thread_id} - Error: {str(e)}")
# Create 100 threads to fetch data concurrently
def main():
while True:
with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor:
futures = [executor.submit(fetch_data, i) for i in range(100)]
concurrent.futures.wait(futures)
if __name__ == "__main__":
main()