Python implementation by SSH Socket

Python implementation by SSH Socket

Just read the Python Socket Itercast video, I thought Socket can send and receive information between the host, then add a command execution SSH is not it?

So start writing a try.

server:

import socket  
import os

server = socket.socket()
host = socket.gethostname()
port = 12345
server.bind((host,port))

server.listen(1)

while(True):
    conn,addr = server.accept()
    print("Connection Established.")
    while(True):
        cmd = conn.recv(1024)
        result = os.popen(cmd.decode()).read()
        conn.send(result.encode('utf-8'))
    print("Connection Interrupted.")

server.close()

Client:

import socket

client = socket.socket()
host = socket.gethostname()
port = 12345

client.connect((host,port))

while(True):
    cmd = input(">")
    client.send(cmd.encode('utf-8'))
    result = client.recv(4096).decode()
    print(result)

client.close()

result:

Experience:

This is a simple little program, there are many problems to be solved, such as how long the results of the command to do? Stick package how to do?

However Review Socket, and play is still very happy.

Guess you like

Origin www.cnblogs.com/rpish/p/12624194.html