The simple principle and implementation code of the python online compiler, And the python Compiler
Let's take a look at the effect (a simple ONE ):
Principle: write the code data of the post request to a file on the server, and then execute the returned result using the python compiler of the server.
Implementation Code:
#flaskrun.py # -*- coding: utf-8 -*- # __author__="ZJL" from flask import Flask from flask import request from flask import Response import json import zxby app = Flask(__name__) def Response_headers(content): resp = Response(content) resp.headers['Access-Control-Allow-Origin'] = '*' return resp @app.route('/') def hello_world(): return Response_headers('hello world!!!') @app.route('/run', methods=['POST']) def run(): if request.method == 'POST' and request.form['code']: code = request.form['code'] print(code) jsondata = zxby.main(code) return Response_headers(str(jsondata)) @app.errorhandler(403) def page_not_found(error): content = json.dumps({"error_code": "403"}) resp = Response_headers(content) return resp @app.errorhandler(404) def page_not_found(error): content = json.dumps({"error_code": "404"}) resp = Response_headers(content) return resp @app.errorhandler(400) def page_not_found(error): content = json.dumps({"error_code": "400"}) resp = Response_headers(content) return resp @app.errorhandler(405) def page_not_found(error): content = json.dumps({"error_code": "405"}) resp = Response_headers(content) return resp @app.errorhandler(410) def page_not_found(error): content = json.dumps({"error_code": "410"}) resp = Response_headers(content) return resp @app.errorhandler(500) def page_not_found(error): content = json.dumps({"error_code": "500"}) resp = Response_headers(content) return resp if __name__ == '__main__': app.run(debug=True)
# Zxby. py #-*-coding: UTF-8-*-# _ author __= "ZJL" import OS, sys, subprocess, tempfile, time # create a temporary folder, the Temporary Folder path TempFile = tempfile is returned. mkdtemp (suffix = '_ test', prefix = 'python _') # file name FileNum = int (time. time () * 1000) # python compiler location EXEC = sys.exe cutable # Get python version def get_version (): v = sys. version_info version = "python % s. % s "% (v. major, v. minor) return version # obtain the py file name def get_pyname (): global FileNum return 'test _ % d' % FileNum # receive code written to the file def write_file (pyname, code ): fpath = OS. path. join (TempFile, '% s. py '% pyname) with open (fpath, 'w', encoding = 'utf-8') as f: f. write (code) print ('file path: % s' % fpath) return fpath # encoding def decode (s): try: return s. decode ('utf-8') encoding t UnicodeDecodeError: return s. decode ('gbk') # main execution function def main (code): r = dict () r ["version"] = get_version () pyname = get_pyname () fpath = write_file (pyname, code) try: # subprocess. check_output is the parent process that waits for the completion of the Child process and returns the output result from the child process to the standard output # stderr is the standard output type outdata = decode (subprocess. check_output ([EXEC, fpath], stderr = subprocess. STDOUT, timeout = 5) wait t subprocess. calledProcessError as e: # e. output is the standard output of Error information # r ["code"] = 'error' r ["output"] = decode (e. output) return r else: # data returned successfully r ['output'] = outdata r ["code"] = "Success" return r finally: # delete a file (in fact, temporary files do not need to be deleted and will be automatically deleted) try: OS. remove (fpath) failed t Exception as e: exit (1) if _ name _ = '_ main _': code = "print (11 ); print (22) "print (main (code ))
Run app. run () and submit code through post. We can further process the output. I just want to understand the principle and will not continue.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.