python在線編譯器的簡(jiǎn)單原理及簡(jiǎn)單實(shí)現(xiàn)代碼
我們先來(lái)看一下效果(簡(jiǎn)單的寫了一個(gè)):
原理:將post請(qǐng)求的代碼數(shù)據(jù)寫入了服務(wù)器的一個(gè)文件,然后用服務(wù)器的python編譯器執(zhí)行返回結(jié)果
實(shí)現(xiàn)代碼:
#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 # 創(chuàng)建臨時(shí)文件夾,返回臨時(shí)文件夾路徑 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') # 主執(zhí)行函數(shù) def main(code): r = dict() r["version"] = get_version() pyname = get_pyname() fpath = write_file(pyname, code) try: # subprocess.check_output 是 父進(jìn)程等待子進(jìn)程完成,返回子進(jìn)程向標(biāo)準(zhǔn)輸出的輸出結(jié)果 # stderr是標(biāo)準(zhǔn)輸出的類型 outdata = decode(subprocess.check_output([EXEC, fpath], stderr=subprocess.STDOUT, timeout=5)) except subprocess.CalledProcessError as e: # e.output是錯(cuò)誤信息標(biāo)準(zhǔn)輸出 # 錯(cuò)誤返回的數(shù)據(jù) r["code"] = 'Error' r["output"] = decode(e.output) return r else: # 成功返回的數(shù)據(jù) r['output'] = outdata r["code"] = "Success" return r finally: # 刪除文件(其實(shí)不用刪除臨時(shí)文件會(huì)自動(dòng)刪除) try: os.remove(fpath) except Exception as e: exit(1) if __name__ == '__main__': code = "print(11);print(22)" print(main(code))
運(yùn)行app.run()方法,通過(guò)post提交代碼,就ok了。我們可以對(duì)輸出結(jié)過(guò)做進(jìn)一步的處理,我這只是為了解一下原理,就沒(méi)繼續(xù)了。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python正則表達(dá)式中的括號(hào)匹配問(wèn)題
這篇文章主要介紹了python正則表達(dá)式中的括號(hào)匹配問(wèn)題,需要的朋友可以參考下2014-12-12Anaconda+spyder+pycharm的pytorch配置詳解(GPU)
這篇文章主要介紹了Anaconda+spyder+pycharm的pytorch配置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10基于Python實(shí)現(xiàn)語(yǔ)音識(shí)別和語(yǔ)音轉(zhuǎn)文字
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)語(yǔ)音識(shí)別和語(yǔ)音轉(zhuǎn)文字功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-09-09Python做個(gè)自定義動(dòng)態(tài)壁紙還可以放視頻
這篇文章主要介紹了如何用Python做個(gè)可以放視頻自定義動(dòng)態(tài)壁紙,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-08-08如何實(shí)現(xiàn)Django Rest framework版本控制
這篇文章主要介紹了如何實(shí)現(xiàn)Django Rest framework版本控制,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07解決Python獲取字典dict中不存在的值時(shí)出錯(cuò)問(wèn)題
今天小編就為大家分享一篇解決Python獲取字典dict中不存在的值時(shí)出錯(cuò)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10Python實(shí)現(xiàn)將Excel轉(zhuǎn)換成為image的方法
今天小編就為大家分享一篇Python實(shí)現(xiàn)將Excel轉(zhuǎn)換成為image的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10