在 python 中使用 requests.request 方法时,响应未在本地主机中加载

Response not loading in localhost when using requests.request method in python

import tornado.web
import tornado.ioloop
from apiApplicationModel import userData
from cleanArray import Label_Correction
import json
import requests

colName=['age', 'resting_blood_pressure', 'cholesterol', 'max_heart_rate_achieved', 'st_depression', 'num_major_vessels', 'st_slope_downsloping', 'st_slope_flat', 'st_slope_upsloping', 'sex_male', 'chest_pain_type_atypical angina', 'chest_pain_type_non-anginal pain', 'chest_pain_type_typical angina', 'fasting_blood_sugar_lower than 120mg/ml', 'rest_ecg_left ventricular hypertrophy', 'rest_ecg_normal', 'exercise_induced_angina_yes', 'thalassemia_fixed defect', 'thalassemia_normal',
       'thalassemia_reversable defect']

class processRequestHandler(tornado.web.RequestHandler):
    def post(self):
        data_input_array = []
        for name in colName:
            x = self.get_body_argument(name, default=0)
            data_input_array.append(int(x))
        label = Label_Correction(data_input_array)
        finalResult = int(userData(label))
        output = json.dumps({"Giveput":finalResult})
        self.write(output)

class basicRequestHandler(tornado.web.RequestHandler):

  def get(self):
    self.render('report.html')

class staticRequestHandler(tornado.web.RequestHandler):

  def post(self):
      data_input_array = []
      for name in colName:
          x = self.get_body_argument(name, default=0)
          data_input_array.append(str(x))
      send_data = dict(zip(colName, data_input_array))
      print(send_data)
      print(type(send_data))
      url = "http://localhost:8887/output"
      headers={}
      response = requests.request('POST',url,headers=headers,data=send_data)
      print(response.text.encode('utf8'))
      print("DONE")

if __name__=='__main__':
  app = tornado.web.Application([(r"/",basicRequestHandler),
                                 (r"/result",staticRequestHandler),
                                 (r"/output",processRequestHandler)])
  print("API IS RUNNING.......")
  app.listen(8887)
  tornado.ioloop.IOLoop.current().start()

实际上我正在尝试创建一个 API 并且可以使用 API 的结果但是 页面一直在加载,没有显示任何响应。 响应应该是由 class processRequestHandlerpost 函数发送的 python 字典 使用调试器后,response = requests.request('POST',url,headers=headers,data=send_data) 之后的行不会执行。 class processRequestHandler 使用 POSTMAN 检查时工作正常。

requests.request 是一种阻塞 方法。这会阻止事件循环并阻止任何其他处理程序 运行。在 Tornado 处理程序中,您需要使用 Tornado 的 AsyncHTTPClient(或另一个非阻塞 HTTP 客户端,例如 aiohttp)。

async def post(self):
    ...
    response = await AsyncHTTPClient().fetch(url, method='POST', headers=headers, body=send_data)

有关详细信息,请参阅 the Tornado users's guide