Zhofang commited on
Commit
6b0c2aa
·
verified ·
1 Parent(s): 9fed31b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -8
app.py CHANGED
@@ -7,11 +7,11 @@ from flask import Flask, Response, render_template, stream_with_context
7
 
8
  app = Flask(__name__)
9
  random.seed() # Initialize the random number generator
10
- global reqs
11
- reqs = 0
12
 
 
 
13
 
14
- @app.route('/dstat')
15
  def index():
16
  with open("index.html", "r") as f:
17
  html = f.read()
@@ -21,10 +21,16 @@ def index():
21
  def chart_data():
22
  def generate_random_data():
23
  while True:
 
 
 
 
24
  json_data = json.dumps(
25
- {'time': datetime.now().strftime('%M:%S'), 'value': reqs})
26
  yield f"data:{json_data}\n\n"
27
- time.sleep(1)
 
 
28
 
29
  response = Response(stream_with_context(generate_random_data()), mimetype="text/event-stream")
30
  response.headers["Cache-Control"] = "no-cache"
@@ -33,9 +39,13 @@ def chart_data():
33
 
34
  @app.route("/attack")
35
  def attack():
36
- global reqs
37
- reqs = reqs + 1
38
- return "200"
 
 
 
 
39
 
40
 
41
  if __name__ == '__main__':
 
7
 
8
  app = Flask(__name__)
9
  random.seed() # Initialize the random number generator
 
 
10
 
11
+ # Initialize a dictionary to store requests per second
12
+ requests_per_second = {}
13
 
14
+ @app.route('/')
15
  def index():
16
  with open("index.html", "r") as f:
17
  html = f.read()
 
21
  def chart_data():
22
  def generate_random_data():
23
  while True:
24
+ current_time = datetime.now().strftime('%M:%S')
25
+ if current_time not in requests_per_second:
26
+ requests_per_second[current_time] = 0
27
+
28
  json_data = json.dumps(
29
+ {'time': current_time, 'value': requests_per_second[current_time]})
30
  yield f"data:{json_data}\n\n"
31
+ time.sleep(1) # Wait for 1 second
32
+ # Reset the count for the next second
33
+ requests_per_second[current_time] = 0
34
 
35
  response = Response(stream_with_context(generate_random_data()), mimetype="text/event-stream")
36
  response.headers["Cache-Control"] = "no-cache"
 
39
 
40
  @app.route("/attack")
41
  def attack():
42
+ # Increment the request count for the current second
43
+ current_time = datetime.now().strftime('%M:%S')
44
+ if current_time in requests_per_second:
45
+ requests_per_second[current_time] += 1
46
+ else:
47
+ requests_per_second[current_time] = 1
48
+ return "200"
49
 
50
 
51
  if __name__ == '__main__':