1 | n/a | #!/usr/bin/env python3 |
---|
2 | n/a | |
---|
3 | n/a | """ |
---|
4 | n/a | Remote python client. |
---|
5 | n/a | Execute Python commands remotely and send output back. |
---|
6 | n/a | """ |
---|
7 | n/a | |
---|
8 | n/a | import sys |
---|
9 | n/a | from socket import socket, AF_INET, SOCK_STREAM, SHUT_WR |
---|
10 | n/a | |
---|
11 | n/a | PORT = 4127 |
---|
12 | n/a | BUFSIZE = 1024 |
---|
13 | n/a | |
---|
14 | n/a | def main(): |
---|
15 | n/a | if len(sys.argv) < 3: |
---|
16 | n/a | print("usage: rpython host command") |
---|
17 | n/a | sys.exit(2) |
---|
18 | n/a | host = sys.argv[1] |
---|
19 | n/a | port = PORT |
---|
20 | n/a | i = host.find(':') |
---|
21 | n/a | if i >= 0: |
---|
22 | n/a | port = int(port[i+1:]) |
---|
23 | n/a | host = host[:i] |
---|
24 | n/a | command = ' '.join(sys.argv[2:]) |
---|
25 | n/a | s = socket(AF_INET, SOCK_STREAM) |
---|
26 | n/a | s.connect((host, port)) |
---|
27 | n/a | s.send(command.encode()) |
---|
28 | n/a | s.shutdown(SHUT_WR) |
---|
29 | n/a | reply = b'' |
---|
30 | n/a | while True: |
---|
31 | n/a | data = s.recv(BUFSIZE) |
---|
32 | n/a | if not data: |
---|
33 | n/a | break |
---|
34 | n/a | reply += data |
---|
35 | n/a | print(reply.decode(), end=' ') |
---|
36 | n/a | s.close() |
---|
37 | n/a | |
---|
38 | n/a | main() |
---|