ยปCore Development>Code coverage>Tools/demo/rpythond.py

Python code coverage for Tools/demo/rpythond.py

#countcontent
1n/a#!/usr/bin/env python3
2n/a
3n/a"""
4n/aRemote python server.
5n/aExecute Python commands remotely and send output back.
6n/a
7n/aWARNING: This version has a gaping security hole -- it accepts requests
8n/afrom any host on the Internet!
9n/a"""
10n/a
11n/aimport sys
12n/afrom socket import socket, AF_INET, SOCK_STREAM
13n/aimport io
14n/aimport traceback
15n/a
16n/aPORT = 4127
17n/aBUFSIZE = 1024
18n/a
19n/adef main():
20n/a if len(sys.argv) > 1:
21n/a port = int(sys.argv[1])
22n/a else:
23n/a port = PORT
24n/a s = socket(AF_INET, SOCK_STREAM)
25n/a s.bind(('', port))
26n/a s.listen(1)
27n/a while True:
28n/a conn, (remotehost, remoteport) = s.accept()
29n/a print('connection from', remotehost, remoteport)
30n/a request = b''
31n/a while 1:
32n/a data = conn.recv(BUFSIZE)
33n/a if not data:
34n/a break
35n/a request += data
36n/a reply = execute(request.decode())
37n/a conn.send(reply.encode())
38n/a conn.close()
39n/a
40n/adef execute(request):
41n/a stdout = sys.stdout
42n/a stderr = sys.stderr
43n/a sys.stdout = sys.stderr = fakefile = io.StringIO()
44n/a try:
45n/a try:
46n/a exec(request, {}, {})
47n/a except:
48n/a print()
49n/a traceback.print_exc(100)
50n/a finally:
51n/a sys.stderr = stderr
52n/a sys.stdout = stdout
53n/a return fakefile.getvalue()
54n/a
55n/atry:
56n/a main()
57n/aexcept KeyboardInterrupt:
58n/a pass