Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e62037

Browse files
committed
added rpython
1 parent 3beff41 commit 4e62037

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

Demo/sockets/rpython.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#! /usr/local/bin/python
2+
3+
# Remote python client.
4+
# Execute Python commands remotely and send output back.
5+
6+
import sys
7+
import string
8+
from socket import *
9+
10+
PORT = 4127
11+
BUFSIZE = 1024
12+
13+
def main():
14+
if len(sys.argv) < 3:
15+
print "usage: rpython host command"
16+
sys.exit(2)
17+
host = sys.argv[1]
18+
port = PORT
19+
i = string.find(host, ':')
20+
if i >= 0:
21+
port = string.atoi(port[i+1:])
22+
host = host[:i]
23+
command = string.join(sys.argv[2:])
24+
s = socket(AF_INET, SOCK_STREAM)
25+
s.connect((host, port))
26+
s.send(command)
27+
s.shutdown(1)
28+
reply = ''
29+
while 1:
30+
data = s.recv(BUFSIZE)
31+
if not data: break
32+
reply = reply + data
33+
print reply,
34+
35+
main()

Demo/sockets/rpythond.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#! /usr/local/bin/python
2+
3+
# Remote python server.
4+
# Execute Python commands remotely and send output back.
5+
# WARNING: This version has a gaping security hole -- it accepts requests
6+
# from any host on the Internet!
7+
8+
import sys
9+
from socket import *
10+
import StringIO
11+
import traceback
12+
13+
PORT = 4127
14+
BUFSIZE = 1024
15+
16+
def main():
17+
if len(sys.argv) > 1:
18+
port = int(eval(sys.argv[1]))
19+
else:
20+
port = PORT
21+
s = socket(AF_INET, SOCK_STREAM)
22+
s.bind('', port)
23+
s.listen(1)
24+
while 1:
25+
conn, (remotehost, remoteport) = s.accept()
26+
print 'connected by', remotehost, remoteport
27+
request = ''
28+
while 1:
29+
data = conn.recv(BUFSIZE)
30+
if not data:
31+
break
32+
request = request + data
33+
reply = execute(request)
34+
conn.send(reply)
35+
conn.close()
36+
37+
def execute(request):
38+
stdout = sys.stdout
39+
stderr = sys.stderr
40+
sys.stdout = sys.stderr = fakefile = StringIO.StringIO()
41+
try:
42+
try:
43+
exec request in {}, {}
44+
except:
45+
print
46+
traceback.print_exc(100)
47+
finally:
48+
sys.stderr = stderr
49+
sys.stdout = stdout
50+
return fakefile.getvalue()
51+
52+
main()

0 commit comments

Comments
 (0)