1. 程式人生 > >python線上編譯器的簡單原理及簡單實現

python線上編譯器的簡單原理及簡單實現

我們先來看一下效果(簡單的寫了一個):




原理:將post請求的程式碼資料寫入了伺服器的一個檔案,然後用伺服器的python編譯器執行返回結果 實現程式碼:
#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

# 建立臨時資料夾,返回臨時資料夾路徑
TempFile = tempfile.mkdtemp(suffix='_test', prefix='python_')
# 檔名
FileNum = int(time.time() * 1000)
# python編譯器位置
EXEC = sys.executable


# 獲取python版本
def get_version():
    v = sys.version_info
    version = "python %s.%s" % (v.major, v.minor)
    return version


# 獲得py檔名
def get_pyname():
    global FileNum
    return 'test_%d' % FileNum


# 接收程式碼寫入檔案
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


# 編碼
def decode(s):
    try:
        return s.decode('utf-8')
    except UnicodeDecodeError:
        return s.decode('gbk')

    # 主執行函式


def main(code):
    r = dict()
    r["version"] = get_version()
    pyname = get_pyname()
    fpath = write_file(pyname, code)
    try:
        # subprocess.check_output 是 父程序等待子程序完成,返回子程序向標準輸出的輸出結果
        # stderr是標準輸出的型別
        outdata = decode(subprocess.check_output([EXEC, fpath], stderr=subprocess.STDOUT, timeout=5))
    except subprocess.CalledProcessError as e:
        # e.output是錯誤資訊標準輸出
        # 錯誤返回的資料
        r["code"] = 'Error'
        r["output"] = decode(e.output)
        return r
    else:
        # 成功返回的資料
        r['output'] = outdata
        r["code"] = "Success"
        return r
    finally:
        # 刪除檔案(其實不用刪除臨時檔案會自動刪除)
        try:
            os.remove(fpath)
        except Exception as e:
            exit(1)

if __name__ == '__main__':
    code = "print(11);print(22)"
    print(main(code))
執行app.run()方法,通過post提交程式碼,就ok了。我們可以對輸出結過做進一步的處理,我這只是為了解一下原理,就沒繼續了。