<p dir="auto">Hello, when I use a flask, I found that the request will execute multiple responses, I don't know where the problem is</p>

<p dir="auto">main.py:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import server

def print_hi(name):
    print(f'Hi, {name}') 


if __name__ == '__main__':
    # print_hi('PyCharm')
    server.start_server()
"><pre class="notranslate"><code class="notranslate">import server

def print_hi(name):
    print(f'Hi, {name}') 


if __name__ == '__main__':
    # print_hi('PyCharm')
    server.start_server()

</code></pre></div>
<p dir="auto">server.py:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import json

from flask import Flask, request
from threading import Thread
import logger

import alertManager
import captureutil


app = Flask('')
@app.route('/webhook', methods=['POST', 'GET'])
def post_message():
  try:
    jsonRequest=request.args.get(&quot;jsonRequest&quot;)
    data = request.data
    body_unicode = data.decode('utf-8')
    logger.log_handler(f&quot;Trigger response begin ---&quot;)

    if request.method == 'POST':
      if jsonRequest == &quot;true&quot;:
        if check_json(body_unicode):
          json_data = json.loads(data)
          logger.log_handler(f&quot; post jsonData:{json_data}&quot;)
          sendJsonFunc(json_data)
          return 'success', 200
        else:
          logger.log_handler(f&quot; post text :{body_unicode}&quot;)
          sendTextFunc(body_unicode)
          return 'success', 200
      else:
        logger.log_handler(f&quot; post text :{body_unicode}&quot;)
        sendTextFunc(body_unicode)
        return 'success', 200
    else:
      logger.log_handler(f&quot; get text :{body_unicode}&quot;)
      sendTextFunc(body_unicode)
      return 'success', 200
  except Exception as e:
    logger.log_handler(&quot;[X] Exception Occured : &quot;, e)
    return 'failure', 500



@app.route('/')
def main():
  return 'Your bot is alive!'



def run():
  logger.log_handler(&quot;run 新的一次启动&quot;)
  app.run(host='127.0.0.1', port=5000)


def start_server_async():
  server = Thread(target=run)
  server.start()

def start_server():
  logger.log_handler(&quot;start_server : NewServer Begin&quot;)
  app.run(host='127.0.0.1', port=5000)

def check_json(input_str):
  try:
    json.loads(input_str)
    return True
  except:
    return False

def sendJsonFunc(json_data):
  return &quot;&quot;
    # alertManager.sendJsonMsg(json_data)

def sendTextFunc(TextMsg):
  return &quot;&quot;
    # alertManager.sendTextMsg(TextMsg)"><pre class="notranslate"><code class="notranslate">import json

from flask import Flask, request
from threading import Thread
import logger

import alertManager
import captureutil


app = Flask('')
@app.route('/webhook', methods=['POST', 'GET'])
def post_message():
  try:
    jsonRequest=request.args.get("jsonRequest")
    data = request.data
    body_unicode = data.decode('utf-8')
    logger.log_handler(f"Trigger response begin ---")

    if request.method == 'POST':
      if jsonRequest == "true":
        if check_json(body_unicode):
          json_data = json.loads(data)
          logger.log_handler(f" post jsonData:{json_data}")
          sendJsonFunc(json_data)
          return 'success', 200
        else:
          logger.log_handler(f" post text :{body_unicode}")
          sendTextFunc(body_unicode)
          return 'success', 200
      else:
        logger.log_handler(f" post text :{body_unicode}")
        sendTextFunc(body_unicode)
        return 'success', 200
    else:
      logger.log_handler(f" get text :{body_unicode}")
      sendTextFunc(body_unicode)
      return 'success', 200
  except Exception as e:
    logger.log_handler("[X] Exception Occured : ", e)
    return 'failure', 500



@app.route('/')
def main():
  return 'Your bot is alive!'



def run():
  logger.log_handler("run 新的一次启动")
  app.run(host='127.0.0.1', port=5000)


def start_server_async():
  server = Thread(target=run)
  server.start()

def start_server():
  logger.log_handler("start_server : NewServer Begin")
  app.run(host='127.0.0.1', port=5000)

def check_json(input_str):
  try:
    json.loads(input_str)
    return True
  except:
    return False

def sendJsonFunc(json_data):
  return ""
    # alertManager.sendJsonMsg(json_data)

def sendTextFunc(TextMsg):
  return ""
    # alertManager.sendTextMsg(TextMsg)
</code></pre></div>

<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/9263032/162741376-6569a261-e9f0-4a70-be16-38e7bc069e65.png"><img width="1001" alt="1" src="https://user-images.githubusercontent.com/9263032/162741376-6569a261-e9f0-4a70-be16-38e7bc069e65.png" style="max-width: 100%;"></a></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/9263032/162741380-0afdcf8b-cf91-45c9-8da7-f791a157aa70.png"><img width="1003" alt="2" src="https://user-images.githubusercontent.com/9263032/162741380-0afdcf8b-cf91-45c9-8da7-f791a157aa70.png" style="max-width: 100%;"></a></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/9263032/162741385-cc3ad23f-9c04-4e9b-9630-242a756a8a23.png"><img width="857" alt="3" src="https://user-images.githubusercontent.com/9263032/162741385-cc3ad23f-9c04-4e9b-9630-242a756a8a23.png" style="max-width: 100%;"></a></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/9263032/162741386-de955c9a-0d8d-4229-a649-8d7397fa0930.png"><img width="1083" alt="4" src="https://user-images.githubusercontent.com/9263032/162741386-de955c9a-0d8d-4229-a649-8d7397fa0930.png" style="max-width: 100%;"></a></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/9263032/162742875-80487b03-5479-43d1-b396-2fc584352fa9.png"><img src="https://user-images.githubusercontent.com/9263032/162742875-80487b03-5479-43d1-b396-2fc584352fa9.png" alt="5" style="max-width: 100%;"></a></p>
<p dir="auto">With the following example:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from flask import Flask

app = Flask(__name__)


@app.route('/', methods=['POST'])
def hello_world():
    return 'Hello World!'


if __name__ == '__main__':
    app.run()"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">flask</span> <span class="pl-k">import</span> <span class="pl-v">Flask</span>

<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-v">Flask</span>(<span class="pl-s1">__name__</span>)


<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">route</span>(<span class="pl-s">'/'</span>, <span class="pl-s1">methods</span><span class="pl-c1">=</span>[<span class="pl-s">'POST'</span>])</span>
<span class="pl-k">def</span> <span class="pl-en">hello_world</span>():
    <span class="pl-k">return</span> <span class="pl-s">'Hello World!'</span>


<span class="pl-k">if</span> <span class="pl-s1">__name__</span> <span class="pl-c1">==</span> <span class="pl-s">'__main__'</span>:
    <span class="pl-s1">app</span>.<span class="pl-en">run</span>()</pre></div>
<p dir="auto">When you set the request body to <code class="notranslate">{}</code> with Postman or any HTTP clients, the first request will return 200, while the second request will return a 405 error response. The log shows the request method is <code class="notranslate">{}POST</code>:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="&quot;{}POST / HTTP/1.1&quot; 405"><pre class="notranslate"><code class="notranslate">"{}POST / HTTP/1.1" 405
</code></pre></div>
<p dir="auto">Notice the request body became the part of the request method.</p>