The Python code running assistant allows you to input Python code online, and then execute the code using a Python script running on the local machine. the Python code running assistant allows you to input Python code online, then run the code through a Python script running on the local machine. The principle is as follows:
Enter code on the webpage:
Click the Run button to send the code to the Python code running assistant running on the local machine;
The Python code execution assistant saves the code as a temporary file and then calls the Python interpreter to execute the code;
The code execution result is displayed on the webpage:
Download
Right-click and choose "Save as: learning. py ".
Standby: learning. py
Complete code:
#! /Usr/bin/env python3 #-*-coding: UTF-8-*-r '''learning. pyA Python 3 tutorial from http://www.liaoxuefeng.comUsage:python3 Learning. py '''import sysdef 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 Falseif not check_version (): exit (1) import OS, io, json, subprocess, tempfilefrom urllib import parsefrom wsgiref. simple_server import make_serverEXEC = sys.exe cutablePORT = 39093 HOST = 'local .liaoxuefeng.com: % d' % PORTTEMP = tempfile. mkdtemp (suffix = '_ PY', prefix = 'learn _ python _') INDEX = 0def main (): httpd = make_server ('2017. 0.0.1 ', PORT, application) print ('Ready for Python code on port % d... '% PORT) httpd. serve_forever () def get_name (): global INDEX = INDEX + 1 return 'test _ % d' % INDEXdef 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 fpathdef decode (s): try: return s. decode ('utf-8') encoding T 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 ('2017 OK ', [('content-type ', 'text/html')]) return [B'Learning Python'] If method = 'get' and path ='/env': start_response ('2018 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 ('2017 Bad request', [('content-type', 'application/json')]) return [B '{"error": "invalid_origin"}'] headers. append ('Access-Control-Allow-origin', Origin) start_response ('2017 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) wait T subprocess. calledProcessError as e: r = dict (error = 'exception', output = decode (e. output) before T subprocess. timeoutExpired as e: r = dict (error = 'timeout', output = 'execution timeout') doesn't subprocess. calledProcessError as e: r = dict (error = 'error', output = 'execution error') print ('execute done. ') return [json. dumps (r ). encode ('utf-8')] if _ name _ = '_ main _': main ()
Run
Run the following command in the directory where learning. py is stored:
The code is as follows:
C: \ Users \ michael \ Downloads> python learning. py
If you see Ready for Python code on port 39093..., it indicates that the operation is successful. do not close the command line window and try to run it in the background:
Try the results
HTML5 browsers:
IE> = 9
Firefox
Chrome
Sarafi