server.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import json
  2. import os
  3. import sys
  4. from pprint import pprint
  5. import bottle
  6. from bottle import redirect
  7. from bottle import request
  8. from bottle import response
  9. from bottle import route, run
  10. from bottle import template
  11. from bottle import error
  12. import config
  13. from filepath2text import route_filename
  14. from utils import get_dir_next_num
  15. bottle.BaseRequest.MEMFILE_MAX = 8 * 1024 * 1024 # bytes
  16. save_path = '../upload'
  17. if not os.path.isdir(save_path):
  18. os.mkdir(save_path)
  19. file_next_num = get_dir_next_num(save_path, which="files")
  20. def start_word2html_app(kill_mathtype=True):
  21. if kill_mathtype:
  22. os.system("taskkill /f /im MathType.exe")
  23. os.system("taskkill /f /im WINWORD.EXE")
  24. os.system("taskkill /f /im ConsoleApplication1.exe")
  25. os.system("start {}".format(config.word2html_exe)) # start 在新窗口中打开
  26. def set_response_header(response):
  27. response.set_header("Access-Control-Allow-Headers", "x-requested-with,content-type,Authorization")
  28. response.set_header("Access-Control-Allow-Methods", "POST,GET,OPTIONS")
  29. response.set_header("Access-Control-Allow-Origin", "*")
  30. @route('/', method='GET')
  31. @route('/', method='POST')
  32. @route('/', method='OPTIONS')
  33. def index():
  34. set_response_header(response)
  35. return redirect("/ocr_page")
  36. @route('/ocr_page', method='GET')
  37. @route('/ocr_page', method='POST')
  38. @route('/ocr_page', method='OPTIONS')
  39. def ocr_page():
  40. set_response_header(response)
  41. return template("index.html")
  42. @route('/ocr', method='GET')
  43. @route('/ocr', method='POST')
  44. @route('/ocr', method='OPTIONS')
  45. def do_ocr():
  46. global file_next_num
  47. set_response_header(response)
  48. upload = request.files.get('mydata')
  49. if upload:
  50. print("upload file: {}".format(upload.raw_filename))
  51. num_str = str(file_next_num)
  52. file_next_num += 1
  53. cur_save_path = save_path
  54. upload.raw_filename = num_str + os.path.splitext(upload.raw_filename)[1]
  55. upload.save(cur_save_path, overwrite=True) # 把文件保存到save_path路径下
  56. filename = os.path.join(cur_save_path, upload.raw_filename)
  57. filename = os.path.abspath(filename)
  58. try:
  59. text = route_filename(filename)
  60. # pprint(text)
  61. if text:
  62. status = "OK"
  63. print("parse is over\n")
  64. else:
  65. status = "ERROR"
  66. text = "无法解析文件"
  67. # print([text])
  68. # pprint([text])
  69. except:
  70. status = "ERROR"
  71. text = "无法解析文件"
  72. else:
  73. status = "ERROR"
  74. text = "request.files.get('mydata') is None, 请检查文件名是否包含中文"
  75. res = {"status": status, "text": text}
  76. return json.dumps(res, ensure_ascii=False)
  77. if __name__ == "__main__":
  78. start_word2html_app()
  79. if len(sys.argv) == 1: # 默认在本地运行
  80. ip, port = config.LocalCfg.ip, config.LocalCfg.port
  81. run(host=ip, port=port, server='tornado')
  82. else:
  83. if sys.argv[1] == 'local':
  84. ip, port = config.LocalCfg.ip, config.LocalCfg.port
  85. run(host=ip, port=port, server='tornado')
  86. elif sys.argv[1] == 'testing':
  87. ip, port = config.TestingCfg.ip, config.TestingCfg.port
  88. run(host=ip, port=port, server='tornado')
  89. elif sys.argv[1] == 'production':
  90. ip, port = config.ProductionCfg.ip, config.ProductionCfg.port
  91. # run(host=ip, port=port, server='tornado', debug=True, reloader=True)
  92. run(host=ip, port=port, debug=True)
  93. else:
  94. print('cmd should be: python server.py local')
  95. print('or: python server.py testing')
  96. print('or: python server.py production')