• python:实现简单的ssh


    socket_server_ssh.py

    #!usr/bin/env python
    # -*- coding:utf-8 -*-

    __author__ = "Samson"

    import socket
    import os

    server = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    server.bind(("localhost",6969))
    server.listen()
    while True:
    conn,addr = server.accept()
    print("new conn:",addr)
    while True:
    data = conn.recv(1024)
    if not data:
    print("客户端已断开!")
    break
    print("执行指令:",data)
    cmd_res = os.popen(data.decode()).read()
    if len(cmd_res) == 0:
    print("Command has no output")
    conn.send(str(len(cmd_res.encode())).encode("utf-8"))#连续两条send可能会出现粘包,建议send后立即recv一下
    conn.send(cmd_res.encode("utf-8"))
    server.close()

    socket_server_client.py

    #!usr/bin/env python
    # -*- coding:utf-8 -*-

    __author__ = "Samson"
    import socket
    client = socket.socket()
    client.connect(("localhost",6969))
    while True:
    cmd = input(">>:").strip()
    if len(cmd) == 0:
    continue
    client.send(cmd.encode("utf-8"))
    cmd_res_size = client.recv(1024)#接受命令结果的长度
    print("命令结果大小:",cmd_res_size)
    received_size = 0
    while received_size < int(cmd_res_size.decode()):
    data = client.recv(1024)
    received_size += len(data)#接收到的数据可能小于1024
    print(data.decode())
    else:
    print("cmd res receive done!")

    client.close()
  • 相关阅读:
    MJExtension的使用
    Swift
    2月22号 UITableView
    1月25号 CALayer
    1月22号 animation—1
    1月22号 KVC KVO
    2016.01.22 简单动画
    2016.01.22 KVC&KVO
    2016.01.22 单例模式(Singleton)
    2016.01.21 UITabBarController
  • 原文地址:https://www.cnblogs.com/cansun/p/8325946.html
Copyright © 2020-2023  润新知