首页 > 代码库 > 【学习笔记】Python网络编程(四)完善socket模拟ssh协议

【学习笔记】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()


本文出自 “秋风习习” 博客,请务必保留此出处http://aitiankong.blog.51cto.com/8923090/1568214

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