最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python在線編譯器的簡單原理及簡單實現(xiàn)代碼

 更新時間:2018年02月02日 13:40:25   作者:superboycxx  
這篇文章主要介紹了python在線編譯器的簡單原理及簡單實現(xiàn)代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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



原理:將post請求的代碼數(shù)據寫入了服務器的一個文件,然后用服務器的python編譯器執(zhí)行返回結果

實現(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)建臨時文件夾,返回臨時文件夾路徑 
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 是 父進程等待子進程完成,返回子進程向標準輸出的輸出結果 
    # stderr是標準輸出的類型 
    outdata = decode(subprocess.check_output([EXEC, fpath], stderr=subprocess.STDOUT, timeout=5)) 
  except subprocess.CalledProcessError as e: 
    # e.output是錯誤信息標準輸出 
    # 錯誤返回的數(shù)據 
    r["code"] = 'Error' 
    r["output"] = decode(e.output) 
    return r 
  else: 
    # 成功返回的數(shù)據 
    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了。我們可以對輸出結過做進一步的處理,我這只是為了解一下原理,就沒繼續(xù)了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 想學python 這5本書籍你必看!

    想學python 這5本書籍你必看!

    想學python,這5本書籍你必看!本文為大家推薦了學習python的5本書籍,5本經典書籍,感興趣的小伙伴們可以參考一下
    2018-12-12
  • python中dump與dumps實現(xiàn)序列化

    python中dump與dumps實現(xiàn)序列化

    這篇文章就來介紹python中dump與dumps實現(xiàn)序列化,文章將圍繞dump與dumps實現(xiàn)序列化展開內容且簡精,需要的朋友可以參考一下,希望對你有所幫助
    2021-10-10
  • 基于tkinter中ttk控件的width-height設置方式

    基于tkinter中ttk控件的width-height設置方式

    這篇文章主要介紹了基于tkinter中ttk控件的width-height設置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • openCV實現(xiàn)圖像融合的示例代碼

    openCV實現(xiàn)圖像融合的示例代碼

    圖像融合是兩幅圖片疊加在一起,本文主要介紹了openCV實現(xiàn)圖像融合的示例代碼,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 關于python 讀取csv最快的Datatable的用法,你都學會了嗎

    關于python 讀取csv最快的Datatable的用法,你都學會了嗎

    大家都知道Datatable與眾不同就是快,還有一點大家需要注意使用Datatable庫需要python3.6及以上版本,接下來通過本文給大家介紹了python 讀取csv最快的Datatable的用法,需要的朋友可以參考下
    2021-10-10
  • 淺談flask源碼之請求過程

    淺談flask源碼之請求過程

    這篇文章主要介紹了淺談flask源碼之請求過程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • 使用Python控制攝像頭拍照并發(fā)郵件

    使用Python控制攝像頭拍照并發(fā)郵件

    這篇文章主要介紹了使用Python控制攝像頭拍照并發(fā)郵件的相關知識,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-04-04
  • Python Web框架Flask中使用百度云存儲BCS實例

    Python Web框架Flask中使用百度云存儲BCS實例

    這篇文章主要介紹了Python Web框架Flask中使用百度云存儲BCS實例,本文調用了百度云存儲Python SDK中的相關類,需要的朋友可以參考下
    2015-02-02
  • Mac上Python使用ffmpeg完美解決方案(避坑必看!)

    Mac上Python使用ffmpeg完美解決方案(避坑必看!)

    ffmpeg是一個強大的開源命令行多媒體處理工具,下面這篇文章主要給大家介紹了關于Mac上Python使用ffmpeg完美解決方案的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2023-02-02
  • 如何讓利用Python+AI使靜態(tài)圖片動起來

    如何讓利用Python+AI使靜態(tài)圖片動起來

    這篇文章主要介紹了如何讓利用Python+AI使靜態(tài)圖片動起來,基于的GAN生成對抗網絡圍繞主題實現(xiàn)靜態(tài)圖片動起來的效果。具有一定的參考價值,需要的小伙伴可以參考一下
    2022-06-06

最新評論

马龙县| 尖扎县| 安康市| 开原市| 阿拉善盟| 舒城县| 济宁市| 谢通门县| 精河县| 屯昌县| 金华市| 万州区| 高碑店市| 阜南县| 海晏县| 连城县| 蕲春县| 阿图什市| 玛沁县| 枣强县| 栾城县| 张掖市| 深泽县| 灵台县| 扶风县| 华容县| 苍溪县| 海宁市| 绍兴市| 独山县| 漳浦县| 彝良县| 株洲市| 闵行区| 定边县| 平湖市| 天水市| 垦利县| 建水县| 鄂托克前旗| 富锦市|