python 实现socket
Socket
server client
服务器端 监听
socket IP+端口 网络通信
本地 网络
stream tcp dgram udp
ftp服务器 传文件 传命令
socket.server 多线程-执行
import socket
from subprocess import Popen, PIPE
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
cmd = conn.recv(1024)
p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
result_stdout = p.stdout.read()
result_stderr = p.stderr.read()
if result_stdout:
conn.send(result_stdout)
elif result_stderr:
conn.send(result_stderr)
if not cmd: break
conn.close()
相关文章