#!/usr/bin/env/python # -*- coding:utf-8 -*- import logging from flask import Flask, render_template, send_from_directory from flask import request, redirect, Response from flask_cors import * from multiprocessing import Process, Queue import configs from structure.structure_mian import WordParseStructure import os, datetime, hashlib import time, json, random import pprint logger = logging.getLogger(__name__) logger.setLevel(level=logging.INFO) # log_file = os.path.join(r'./logs', 'structure_log.txt') # 日志地址 handler = logging.FileHandler(log_file, mode='a', encoding='utf-8', delay=True) handler.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) # app = Flask(__name__) app.debug = True CORS(app, supports_credentials=True) file1 = os.getcwd() + '\\res_folder' @app.route('/word_structure', methods=["GET","POST"]) def word_structure(): # logger.info("==request.POST.dict==>{}\n".format(request.form.to_dict())) mydata = request.json.get("sci_html_data", "") is_reparse = request.json.get("is_reparse", "0") print(mydata) time_str = datetime.datetime.strftime(datetime.datetime.now(), '%Y_%m_%d_%H_%M_%S') new_fpath = os.path.join(file1, str(time_str)+".html") re_f = open(new_fpath, 'w', encoding='utf-8') re_f.write(mydata) # try: res, paper_type = WordParseStructure(mydata, "", int(is_reparse)).structure() # logger.info("初步解析成功") pprint.pprint(res) return json.dumps(res, ensure_ascii=False) # except: # # 先保存文件 # now_time = datetime.datetime.now() # time_str = datetime.datetime.strftime(now_time, '%Y_%m_%d_%H_%M_%S') # aft_modify = (str(random.random())).encode("utf-8") # aft_name = hashlib.md5(aft_modify).hexdigest() + '__' + time_str + '.json' # # new_fpath = os.path.join(file1, aft_name) # re_f = open(new_fpath, 'w', encoding='utf-8') # json.dump(mydata, re_f) # # return "解析失败" # return json.dumps({"items": []}, ensure_ascii=False) @app.route('/ser_static/', methods=["GET"]) def ser_static(file_path): # endpoint的位置是函数接口名,不能用static,与flask内部变量重名 """ :param file_path: 图片的本地绝对路径 :return: """ return send_from_directory(configs.IMG_FOLDER, file_path) if __name__ == "__main__": app.run(host='192.168.1.140', port=11066, threaded=True, debug=True)