Python Run code Helper online

Source: Internet
Author: User
The Python Code run helper allows you to enter Python code online and execute the code from a Python script run natively. The principle is as follows:

Enter the code on the Web page:

Click the Run button and the code is sent to the running assistant of the Python code that is running on the computer;

The Python Code run helper Saves the code as a temporary file and then invokes the Python interpreter to execute the code;

The Web page displays code execution results:

Download

Right click, save target as: learning.py

Alternate Download Address: learning.py

Full 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 Tru E print (' Your current Python is%d.%d. Please use Python 3.4. ' Percent (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.executableport = 39093HOST = ' Loca l.liaoxuefeng.com:%d '% porttemp = tempfile.mkdtemp (suffix= ' _py ', prefix= ' learn_python_ ') INDEX = 0def Main (): httpd = Mak E_server (' 127.0.0.1 ', PORT, application) print (' Ready to Python code on PORT%d ... '% port) httpd.serve_forever () def get_ Name (): Global Index 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 wrote to:%s '% Fpath) returnFpathdef Decode (s): Try:return s.decode (' Utf-8 ') except Unicodedecodeerror:return s.decode (' GBK ') def application (envi Ron, start_response): host = Environ.get (' http_host ') method = Environ.get (' request_method ') path = Environ.get (' Path_ INFO ') If method = = ' GET ' and path = = '/': Start_response (' $ OK ', [(' Content-type ', ' text/html ')]) return [b '<title>Learning Python</title>' If method = = ' GET ' and path = = '/env ': Start_response (' $ OK ', [(' Content-type ', ' text/html ')]) L = [B '<title>Env</title>' 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 (). Startswi Th (' application/x-www-form-urlencoded '): Start_response (' All 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 (' [' + '], [(' Content-type ', ' Application/json ')]) ret Urn [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:s Tart_response (' Bad Request ', [(' Content-type ', ' Application/json ')] return [b ' {' Error ': ' Invalid_origin '} '] Headers.append ((' Access-control-allow-origin ', Origin)) start_response (' OK ', headers) R = dict () Try:fpath = write_p Y (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= ' execution timeout ') except subprocess. Calledprocesserror as E:r = dict (error= ' error ', output= ' execute error ') print (' Execute done. ') return [Json.dumps (R). Encode (' UTF -8 ')]if __name__ = = ' __main__ ': Main ()

Run

Run the command in the directory where the learning.py is stored:
Copy the Code code as follows:


C:\users\michael\downloads> python learning.py

If you see ready for Python code on port 39093 ... indicates a successful run, do not close the command-line window, and minimize it to run in the background:

Try the effect

Requires a browser that supports HTML5:

IE >= 9
Firefox
Chrome
Sarafi

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.