1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import sys, os, socket


host = ''                
port = 50101

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
print("Server started on port: ", port)
s.listen(1)
while (1):
    conn, addr = s.accept()
    print 'New connection from ', addr
    while True:
        try:
            rc = conn.recv(2)
            if len(rc) == 0: raise IOError
            pipe = os.popen(rc)
            rl = pipe.readlines()
            fl = conn.makefile('w')
            fl.writelines(rl[:-1])
            fl.close()
        except IOError:
            conn.close()


Client:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = 'localhost'
port = input('Port: ')
s.connect((host, port))
while (1):
   cmd = raw_input('$ ')
   s.send(cmd) 
   file = s.makefile('r', 0)
   sys.stdout.writelines(file.readlines())
   file.close()