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

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

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

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



原理:將post請求的代碼數(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()方法,通過post提交代碼,就ok了。我們可以對輸出結(jié)過做進(jìn)一步的處理,我這只是為了解一下原理,就沒繼續(xù)了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 想學(xué)python 這5本書籍你必看!

    想學(xué)python 這5本書籍你必看!

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

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

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

    基于tkinter中ttk控件的width-height設(shè)置方式

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

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

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

    關(guān)于python 讀取csv最快的Datatable的用法,你都學(xué)會(huì)了嗎

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

    淺談flask源碼之請求過程

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

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

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

    Python Web框架Flask中使用百度云存儲(chǔ)BCS實(shí)例

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

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

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

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

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

最新評(píng)論

南宫市| 常德市| 永清县| 湾仔区| 黄山市| 孟村| 新竹市| 武清区| 娄烦县| 霍林郭勒市| 莱阳市| 晋江市| 仙居县| 石嘴山市| 孟津县| 彭山县| 杭锦后旗| 兰坪| 饶平县| 宝鸡市| 如东县| 中山市| 孝昌县| 调兵山市| 丹凤县| 田阳县| 图木舒克市| 原阳县| 诸城市| 霸州市| 尤溪县| 邹平县| 靖宇县| 江阴市| 都兰县| 道真| 金湖县| 常德市| 成都市| 历史| 定西市|