Python online running code assistant and python code Assistant
The Python code execution assistant allows you to input Python code online and then run the code using 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, tempfil Efrom 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 ret Urn '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 _ METHO D') path = environ. get ('path _ info') if method = 'get' and PATH = '/': start_response ('2017 OK ', [('content-type ', 'text/html')]) return [B '
Run
Run the following command in the directory where learning. py is stored:
Copy codeThe 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