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

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

今天沒事兒完了一下菜鳥教程的python編譯器發現挺有意思,想搞明白它的原理是啥,於是我輸入了以下程式碼:

import sys,os
print(sys.version_info)
print(sys.executable)
print(sys.path[0])
print(os.listdir(sys.path[0]))
with open("/usercode/file.py") as e:
	print(e.read())
結果是這樣的:

我個人推測,它是將post請求的程式碼資料寫入了伺服器的一個檔案,然後用伺服器的python編譯器執行返回結果

於是我自己簡單實現一個

目錄結構:

OnlineEXEC

|----zxby.py

|----app

        |----flaskrun.py

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("你好")"""
#     print(main(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)

開啟postman測試:

正確程式碼測試:

錯誤程式碼測試:


大致就是這樣的