web.py 文件源码

python
阅读 28 收藏 0 点赞 0 评论 0

项目:PYKE 作者: muddyfish 项目源码 文件源码
def submit_code(timeout=5):
    code = request.form.get("code", "")
    inp = request.form.get("input", "")
    print(code, inp)
    warnings = int(request.form.get("warnings", "0"), 10)
    use_hex = int(request.form.get("hex", "0"), 10)
    args = [sys.executable,
            'main.py',
            '--safe',
            '--',
            code]
    stderr = PIPE
    if warnings:
        args.insert(2, "--warnings")
        stderr = STDOUT
    if use_hex:
        args.insert(2, "--hex")
    with Popen(args,
               stdin=PIPE,
               stdout=PIPE,
               stderr=stderr,
               creationflags=is_windows and subprocess.CREATE_NEW_PROCESS_GROUP) as process:
        process.stdin.write(bytearray(inp, "utf-8"))
        process.stdin.close()
        response = ""
        try:
            process.wait(timeout)
        except TimeoutExpired:
            response = "Timeout running code.\n"
            if is_windows:
                os.kill(process.pid, signal.CTRL_BREAK_EVENT)
            else:
                process.send_signal(signal.SIGTERM)
            try:
                process.wait(2)
            except TimeoutExpired:
                response += "Really timed out code\n"
            process.kill()
        response += process.stdout.read().decode("cp1252", errors="replace")
    return response
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号