Watch 2 Star 2 Fork 0

RCPI / pi4bPythonMulanPSL-2.0

Join us
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
这里是针对树莓派4B的远程控制类python程序代码 spread retract

https://gitee.com/rcpi/pi4b

Clone or download
learning.py 9.83 KB
Copy Edit Web IDE Raw Blame History
RCPI authored 2020-03-28 03:20 . 廖雪峰的在线调试本地文件
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
r'''
learning.py
A Python 3 tutorial from https://www.liaoxuefeng.com
Usage:
$ python3 learning.py
'''
# check #######################################################################
import ssl
import tempfile
import subprocess
import json
import io
import os
from http.server import HTTPServer, BaseHTTPRequestHandler, SimpleHTTPRequestHandler
from urllib import parse
import sys
from datetime import datetime
CERT_EXPIRES = '2020-08-07'
def check_version():
v = sys.version_info
if v.major == 3 and v.minor >= 6:
return
print('Your current python is %d.%d. Please use Python 3.6.' %
(v.major, v.minor))
exit(1)
def check_cert():
today = datetime.now().strftime('%Y-%m-%d')
if today >= CERT_EXPIRES:
print('This learning.py is expired. Please download a newer version.')
exit(1)
check_version()
check_cert()
# start server ################################################################
EXEC = sys.executable
PORT = 39093
TEMP = tempfile.mkdtemp(suffix='_py', prefix='learn_python_')
HTML_INDEX = r'''
<html>
<head><title>Learning Python</title></head>
<body>
<form method="post" action="/run">
<textarea name="code" style="width:90%;height: 600px"></textarea>
<p><button type="submit">Run</button></p>
</form>
</body>
</html>
'''
class LearningHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.close_connection = True
if self.path != '/':
return self.send_error(404)
self._sendHttpHeader('text/html')
self._sendHttpBody(HTML_INDEX)
def do_POST(self):
self.close_connection = True
if self.path != '/run':
return self.send_error(400)
print('Prepare code...')
body = self.rfile.read(int(self.headers['Content-length']))
qs = parse.parse_qs(body.decode('utf-8'))
if not 'code' in qs:
return self.send_error(400)
code = qs['code'][0]
r = dict()
try:
fpath = write_py(get_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.')
self._sendHttpHeader()
self._sendHttpBody(r)
def _sendHttpHeader(self, contentType='application/json'):
origin = self.headers['Origin'] or 'https://www.liaoxuefeng.com'
self.send_response(200)
self.send_header('Content-Type', contentType)
self.send_header('Access-Control-Allow-Origin', origin)
self.send_header('Access-Control-Allow-Methods', 'GET,POST')
self.send_header('Access-Control-Max-Age', '86400')
self.end_headers()
def _sendHttpBody(self, data):
body = b''
if isinstance(data, bytes):
body = data
elif isinstance(data, str):
body = data.encode('utf-8', errors='ignore')
else:
body = json.dumps(data).encode('utf-8', errors='ignore')
self.wfile.write(body)
def main():
certfile = write_cert()
httpd = HTTPServer(('127.0.0.1', PORT), LearningHTTPRequestHandler)
httpd.socket = ssl.wrap_socket(
httpd.socket, certfile=certfile, server_side=True)
print('Ready for Python code on port %d...' % PORT)
print('Press Ctrl + C to exit...')
httpd.serve_forever()
# functions ###################################################################
INDEX = 0
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')
# certificate #################################################################
def write_cert():
fpath = os.path.join(TEMP, 'local.liaoxuefeng.com.pem')
with open(fpath, 'w', encoding='utf-8') as f:
f.write(CERT_DATA)
return fpath
CERT_DATA = r'''
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEArUtX5vRI7RsCBEN8sIPskQyLJhsXzxQ947dcH2so0uuCYodm
wkuKwMtjCvP5nnM2PRaJMSD3ke4OA2BdD+CWGFmoXdD8yJbAOh6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3dXw6OV5AgMBAAGjggFPMIIBSzAdBgNVHQ4EFgQUf9OZ86BHDjEAVlYijrfMnt3K
AYowHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUwDgYDVR0PAQH/BAQD
AgGGMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggrBgEFBQcDAjASBgNVHRMBAf8ECDAG
AQH/AgEAMDQGCCsGAQUFBwEBBCgwJjAkBggrBgEFBQcwAYYYaHR0cDovL29jc3Au
ZGlnaWNlcnQuY29tMEIGA1UdHwQ7MDkwN6A1oDOGMWh0dHA6Ly9jcmwzLmRpZ2lj
ZXJ0LmNvbS9EaWdpQ2VydEdsb2JhbFJvb3RDQS5jcmwwTAYDVR0gBEUwQzA3Bglg
hkgBhv1sAQIwKjAoBggrBgEFBQcCARYcaHR0cHM6Ly93d3cuZGlnaWNlcnQuY29t
L0NQUzAIBgZngQwBAgEwDQYJKoZIhvcNAQELBQADggEBAK3dVOj5dlv4MzK2i233
lDYvyJ3slFY2X2HKTYGte8nbK6i5/fsDImMYihAkp6VaNY/en8WZ5qcrQPVLuJrJ
DSXT04NnMeZOQDUoj/NHAmdfCBB/h1bZ5OGK6Sf1h5Yx/5wR4f3TUoPgGlnU7EuP
ISLNdMRiDrXntcImDAiRvkh5GJuH4YCVE6XEntqaNIgGkRwxKSgnU3Id3iuFbW9F
UQ9Qqtb1GX91AJ7i4153TikGgYCdwYkBURD8gSVe8OAco6IfZOYt/TEwii1Ivi1C
qnuUlWpsF1LdQNIdfbW3TSe0BhQa7ifbVIfvPWHYOu3rkg1ZeMo6XRU9B4n5VyJY
RmE=
-----END CERTIFICATE-----
'''
# start main at last ##########################################################
if __name__ == '__main__':
main()

Comment ( 0 )

Sign in for post a comment

Python
1
https://gitee.com/rcpi/pi4b.git
git@gitee.com:rcpi/pi4b.git
rcpi
pi4b
pi4b
master

Help Search

205735 778617b6 1899542 205747 347fc4a1 1899542