点击(此处)折叠或打开
- #socket_server.py
- import socket
- import os
- import sys
- def work():
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.bind(('0.0.0.0',1000))
- sock.listen(5)
- while True:
- try:
- conn, addr = sock.accept()
- ret = conn.recv(2048)
- result = os.popen(ret).read()
- conn.send(result)
- except KeyboardInterrupt:
- print 'Now we will exit'
- sys.exit(0)
- sock.close()
- if __name__ == '__main__':
- work()
点击(此处)折叠或打开
- #socket_client.py
- import socket
- def socket_send(command):
- sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sock.connect(('192.168.1.55', 1000))
- sock.send(command)
- result = sock.recv(2048)
- sock.close()
- return result
- if __name__ == '__main__':
- print socket_send('ls')