zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    hdu 5087(次长上升子序列)
    hdu 5086(递推)
    hdu 5084(矩阵操作)
    hdu 5083(模拟)
    hdu 5082(水题)
    高数准备:
    ★ phpStudy安装SSL证书实现https链接
    phpStudy环境安装SSL证书教程
    Qt中切换窗口功能的实现
    LeetCode OJ:Reverse Linked List II(反转链表II)
  • 原文地址:https://www.cnblogs.com/cansun/p/8325946.html
Copyright © 2011-2022 走看看