server3.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/usr/bin/env/python
  2. # -*- coding:utf-8 -*-
  3. import logging
  4. from flask import Flask, render_template, send_from_directory
  5. from flask import request, redirect, Response
  6. from flask_cors import *
  7. from multiprocessing import Process, Queue
  8. import configs
  9. from structure.structure_mian import WordParseStructure
  10. import os, datetime, hashlib
  11. import time, json, random
  12. import pprint
  13. logger = logging.getLogger(__name__)
  14. logger.setLevel(level=logging.INFO)
  15. #
  16. log_file = os.path.join(r'./logs', 'structure_log.txt') # 日志地址
  17. handler = logging.FileHandler(log_file, mode='a', encoding='utf-8', delay=True)
  18. handler.setLevel(logging.INFO)
  19. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  20. handler.setFormatter(formatter)
  21. logger.addHandler(handler)
  22. #
  23. app = Flask(__name__)
  24. app.debug = True
  25. CORS(app, supports_credentials=True)
  26. file1 = os.getcwd() + '\\res_folder'
  27. @app.route('/word_structure', methods=["GET","POST"])
  28. def word_structure():
  29. # logger.info("==request.POST.dict==>{}\n".format(request.form.to_dict()))
  30. mydata = request.json.get("sci_html_data", "")
  31. is_reparse = request.json.get("is_reparse", "0")
  32. print(mydata)
  33. time_str = datetime.datetime.strftime(datetime.datetime.now(), '%Y_%m_%d_%H_%M_%S')
  34. new_fpath = os.path.join(file1, str(time_str)+".html")
  35. re_f = open(new_fpath, 'w', encoding='utf-8')
  36. re_f.write(mydata)
  37. # try:
  38. res, paper_type = WordParseStructure(mydata, "", int(is_reparse)).structure()
  39. # logger.info("初步解析成功")
  40. pprint.pprint(res)
  41. return json.dumps(res, ensure_ascii=False)
  42. # except:
  43. # # 先保存文件
  44. # now_time = datetime.datetime.now()
  45. # time_str = datetime.datetime.strftime(now_time, '%Y_%m_%d_%H_%M_%S')
  46. # aft_modify = (str(random.random())).encode("utf-8")
  47. # aft_name = hashlib.md5(aft_modify).hexdigest() + '__' + time_str + '.json'
  48. #
  49. # new_fpath = os.path.join(file1, aft_name)
  50. # re_f = open(new_fpath, 'w', encoding='utf-8')
  51. # json.dump(mydata, re_f)
  52. # # return "解析失败"
  53. # return json.dumps({"items": []}, ensure_ascii=False)
  54. @app.route('/ser_static/<path:file_path>', methods=["GET"])
  55. def ser_static(file_path): # endpoint的位置是函数接口名,不能用static,与flask内部变量重名
  56. """
  57. :param file_path: 图片的本地绝对路径
  58. :return:
  59. """
  60. return send_from_directory(configs.IMG_FOLDER, file_path)
  61. if __name__ == "__main__":
  62. app.run(host='192.168.1.140', port=11066, threaded=True, debug=True)