我是靠谱客的博主 自由外套,这篇文章主要介绍python代码在线运行工具怎么用_Python在线运行代码助手,现在分享给大家,希望可以做个参考。

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

r'''

learning.py

A Python 3 tutorial from http://www.liaoxuefeng.com

Usage:

python3 learning.py

'''

import sys

def check_version():

v = sys.version_info

if v.major == 3 and v.minor >= 4:

return True

print('Your current python is %d.%d. Please use Python 3.4.' % (v.major, v.minor))

return False

if not check_version():

exit(1)

import os, io, json, subprocess, tempfile

from urllib import parse

from wsgiref.simple_server import make_server

EXEC = sys.executable

PORT = 39093

HOST = 'local.liaoxuefeng.com:%d' % PORT

TEMP = tempfile.mkdtemp(suffix='_py', prefix='learn_python_')

INDEX = 0

def main():

httpd = make_server('127.0.0.1', PORT, application)

print('Ready for Python code on port %d...' % PORT)

httpd.serve_forever()

def get_name():

global INDEX

INDEX = INDEX + 1

return 'test_%d' % INDEX

def write_py(name, code):

fpath = os.path.join(TEMP, '%s.py' % name)

with open(fpath, 'w', encoding='utf-8') as f:

f.write(code)

print('Code wrote to: %s' % fpath)

return fpath

def decode(s):

try:

return s.decode('utf-8')

except UnicodeDecodeError:

return s.decode('gbk')

def application(environ, start_response):

host = environ.get('HTTP_HOST')

method = environ.get('REQUEST_METHOD')

path = environ.get('PATH_INFO')

if method == 'GET' and path == '/':

start_response('200 OK', [('Content-Type', 'text/html')])

return [b'Learning PythonRun']

if method == 'GET' and path == '/env':

start_response('200 OK', [('Content-Type', 'text/html')])

L = [b'ENV']

for k, v in environ.items():

p = '

%s = %s' % (k, str(v))

L.append(p.encode('utf-8'))

L.append(b'')

return L

if host != HOST or method != 'POST' or path != '/run' or not environ.get('CONTENT_TYPE', '').lower().startswith('application/x-www-form-urlencoded'):

start_response('400 Bad Request', [('Content-Type', 'application/json')])

return [b'{"error":"bad_request"}']

s = environ['wsgi.input'].read(int(environ['CONTENT_LENGTH']))

qs = parse.parse_qs(s.decode('utf-8'))

if not 'code' in qs:

start_response('400 Bad Request', [('Content-Type', 'application/json')])

return [b'{"error":"invalid_params"}']

name = qs['name'][0] if 'name' in qs else get_name()

code = qs['code'][0]

headers = [('Content-Type', 'application/json')]

origin = environ.get('HTTP_ORIGIN', '')

if origin.find('.liaoxuefeng.com') == -1:

start_response('400 Bad Request', [('Content-Type', 'application/json')])

return [b'{"error":"invalid_origin"}']

headers.append(('Access-Control-Allow-Origin', origin))

start_response('200 OK', headers)

r = dict()

try:

fpath = write_py(name, code)

print('Execute: %s %s' % (EXEC, fpath))

r['output'] = decode(subprocess.check_output([EXEC, fpath], stderr=subprocess.STDOUT, timeout=5))

except subprocess.CalledProcessError as e:

r = dict(error='Exception', output=decode(e.output))

except subprocess.TimeoutExpired as e:

r = dict(error='Timeout', output='执行超时')

except subprocess.CalledProcessError as e:

r = dict(error='Error', output='执行错误')

print('Execute done.')

return [json.dumps(r).encode('utf-8')]

if __name__ == '__main__':

main()

运行

在存放learning.py的目录下运行命令:

复制代码 代码如下:

C:UsersmichaelDownloads> python learning.py

如果看到Ready for Python code on port 39093...表示运行成功,不要关闭命令行窗口,最小化放到后台运行即可:

20160715152720.png

试试效果

需要支持HTML5的浏览器:

IE >= 9

Firefox

Chrome

Sarafi

article_wechat2021.jpg?1111

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

相关文章

相关视频

网友评论

文明上网理性发言,请遵守 新闻评论服务协议我要评论2条评论

user_avatar.jpg

JSRUN.NET· 2020-07-31 18:37:272楼

http://py.jsrun.net 在线运行python, 非常速度

user_avatar.jpg

JSRUN.NET· 2020-07-31 18:36:531楼

http://php.jsrun.net 在线运行php, 非常速度

user_avatar.jpg

立即提交

专题推荐5d1ef1e9e866e635.jpg独孤九贱-php全栈开发教程

全栈 100W+

主讲:Peter-Zhu 轻松幽默、简短易学,非常适合PHP学习入门

5d1ef236ca878949.jpg玉女心经-web前端开发教程

入门 50W+

主讲:灭绝师太 由浅入深、明快简洁,非常适合前端学习入门

5d1ef2477c7d7587.jpg天龙八部-实战开发教程

实战 80W+

主讲:西门大官人 思路清晰、严谨规范,适合有一定web编程基础学习

最后

以上就是自由外套最近收集整理的关于python代码在线运行工具怎么用_Python在线运行代码助手的全部内容,更多相关python代码在线运行工具怎么用_Python在线运行代码助手内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(47)

评论列表共有 0 条评论

立即
投稿
返回
顶部