python rpc 服务

Linux 跳过ssh和telnet 连接操作服务器
基于socket 的应答式
服务端

#!/usr/bin/env python
# -* - coding: UTF-8 -* -
import socket
import commands


class Rpcservice :
    def __init__(self,ip,port):
        self.ip = ip
        self.port = port

    def Fser(self , ip , port) :
        sock = socket.socket ( socket.AF_INET , socket.SOCK_STREAM )
        sock.bind ( (self.ip , self.port) )
        sock.listen ( 1 )
        while True :
            conn , addr = sock.accept ()
            while True :
                r = conn.recv ( 4096 )
                if r == 'Hi Siri' :
                    conn.send ( b"遇见对的人,请继续。" )
                    conn.send ( b"Yes" )
                    while True :
                        cmd = conn.recv ( 4096 )
                        if cmd == 'exit' :
                            conn.sendall ( "期待下次再见。" )
                            break
                        else :
                            status , output = commands.getstatusoutput ( cmd )
                            conn.sendall ( output )
                            if output is not None :
                                continue
                            conn.close ()
                elif r != 'Hi Siri' and len ( r ) > 0 :
                    conn.send ( b"有内鬼,终止交易!!!" )
                    conn.send ( b"No" )
                    continue
                else :
                    break
                    conn.close ()


if __name__ == "__main__" :
    ip = "192.168.1.30"
    port = 61888
    rpcservice = Rpcservice ( ip , port )
    rpcservice.Fser ( ip , port )

客户端

# -* - coding: UTF-8 -* -
import socket


class Rpcclent :
    def __init__(self , ip , port) :
        self.ip = ip
        self.port = port

    def Rclent(self , ip , port) :
        # type: (object, object) -> object

        sock = socket.socket ( socket.AF_INET , socket.SOCK_STREAM )
        sock.connect ( (self.ip , self.port) )
        while True :
            dd = raw_input ( "请输入暗号,quit退出\n" )
            if dd == 'quit' :
                sock.close ()
                break
            else :
                sock.sendall ( dd )
                print sock.recv ( 4096 )
                an = sock.recv ( 4096 )
                if an == 'Yes' :
                    while True :
                        cmd = raw_input ( "请输入命令,exit退出\n" )
                        sock.sendall ( cmd )
                        print sock.recv ( 4096 )
                        if cmd == 'exit' :
                            sock.close ()

                            break


if __name__ == "__main__" :
    ip = "192.168.1.30"
    port = 61888
    rpcclent = Rpcclent ( ip , port )
    rpcclent.Rclent ( ip , port )

先在服务端运行起来后,客户端运行效果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值