网页实现在线编程1

PYTHON在线编程的实现:

网页部分的基本实现

这部分没有什么好说的,本篇主讲功能所以网页结构十分简约

index.html

<!DOCTYPE html>
<html>
<head>
    <title>在线Python编程</title>
</head>
<body>
    <h1>在线Python编程</h1>
    <textarea id="code" rows="10" cols="50"></textarea><br>
    <button onclick="executeCode()">执行代码</button>
    <div id="output"></div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        function executeCode() {
            var code = $('#code').val();
            $.post('/execute', {code: code}, function(data) {
                $('#output').text(data);
            });
        }
    </script>
</body>
</html>

一个h1的标题+一个textarea文本框+一个button就解决了+一个预留输出结果的div就行

用js上传一下文本框中的内容就行

注意:创建一个templates文件夹,将其放在templates文件夹内

后端部分的基本实现

框架使用flask,编程的基础实现使用execjs,pip下载一下

pip install Flask PyExecJS

app.py

from flask import Flask, render_template, request
import execjs

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/execute', methods=['POST'])
def execute():
    code = request.form['code']
    
    try:
        ctx = execjs.compile("""
            function executePythonCode(code) {
                return eval(code);
            }
        """)
        result = ctx.call('executePythonCode', code)
        return str(result)
    except Exception as e:
        return str(e)

if __name__ == '__main__':
    app.run(host="127.0.0.1",port=4999,debug=True)

注意:此文件与template文件夹平级

#此代码由chatgpt提供,毛病一大堆...

可以看见这里就两个路由,一个/index主路由和一个/execute

执行在线编程的就是/execute路由,用post获取一下index.html中文件夹的内容,然后使用execjs的compile来eval()一下code。

注意:使用execjs需要下载一个nodejs还要配置一下环境变量,否则任何代码都是TypeError:缺少对象

运行一下ChatGPT所给的这些代码,你会明白什么才叫炸裂

使用实例:

#code
a=[1,2]
b=[3,4]
c=a+b
#output,注:这玩意自动输出了最后一个变量
1,23,4
#code
print(1)
#output
ReferenceError: print is not defined

放在整个炸裂圈也是非常炸裂的存在,这不由让人大喊:泰裤辣!

对index.html的改进1

<!DOCTYPE html>
<html>
<head>
    <title>在线Python编程</title>
</head>
<body>
    <h1>在线Python编程</h1>
    <textarea id="code" rows="10" cols="50"></textarea><br>
    <button onclick="executeCode_0()">执行代码(路线1:js的eval())</button>
    <button onclick="executeCode_1()">执行代码(路线2:解释器运行)</button>
    <div id="output"></div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        function executeCode_0() {
            var code = $('#code').val();
            $.post('/execute_0', {code: code}, function(data) {
                $('#output').text(data);
            });
        }
        function executeCode_1() {
            var code = $('#code').val();
            $.post('/execute_1', {code: code}, function(data) {
                $('#output').text(data);
            });
        }
    </script>
</body>
</html>

对后端app.py的改进1

def index():
    return render_template('index.html')

@app.route('/execute_0', methods=['POST'])
def execute_0():
    code = request.form['code']
    try:
        ctx = execjs.compile("""
            function executePythonCode(code) {
                return eval(code);
            }
        """)
        result = ctx.call('executePythonCode', code)
        return str(result)
    except Exception as e:
        return str(e)

@app.route('/execute_1', methods=['POST'])
def execute_1():
    code = request.form['code']
    code=str(code)
    path=sys.path[0]
    file=path+"/test.py"
    with open(file,"w",encoding="UTF-8") as f:
        f.write(code)
    cmd = "python "+file
    out = os.popen(cmd)
    try:
        exec(code)
    except Exception as e:
        return str(e)
    return out.read()

if __name__ == '__main__':
    app.run(host="127.0.0.1",port=4999,debug=True)

虽然屎山没删(毕竟要留着下饭不是),但新增了一种执行的方法(看出问题的先别骂,看完再骂)

原理是将代码放在一个同目录的test.py中调用cmd执行,再获取执行结果

至于为什么放置了一个try;except语句那是因为上述办法没法报错...

这是个大漏洞,对方完全可以以此控制服务器的(反正我用的是云服还是别人借的)

测试一下nice!能用!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值