【学习笔记】Python网络编程(四)完善socket模拟ssh协议

server端代码:

import socket,commands
host =  ''
port = 1053
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((host,port))
s.listen(1)
while 1:
    conn,addr = s.accept()
    while 1:
        data = conn.recv(1024)
        cc,bb = commands.getstatusoutput(data)
        if len(bb.strip()) != 0:
                conn.sendall(bb)
        else:
                conn.sendall("DONE")

client端:

import socket,os
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
host = "www.xiaoran.com"
port = 1053
s.connect((host,port))
while 1:
        cc = raw_input("plase input your cmd:").strip()
        if len(cc) == 0:continue
        s.sendall(cc)
        data = s.recv(1024)
        print data
s.close()


你可能感兴趣的:(server,网络编程,import)