【RPC】前传

前传

本地程序用的go语言,想把main.go程序当中一些计算工作放到服务器上进行,而只需要把结果给我即可。由于平台上暂时不能运行Go代码,所以写的是python文件。

1、主要是使用ssh依赖进行连接,但是大概率是需要手动添加的,自动添加一直在报错。里面的go.mod也是要重新生成的,主要还是不太熟。

连接就需要账号密码和地址,配置好之后进行连接。test.py里面只有一个变量a,并且数值是固定的,只是需要输出而已,里面的username和pwd还有Ip是自己的信息。

go 复制代码
package main

import (
	"fmt"
	"log"
	"net"
	"time"
	"golang.org/x/crypto/ssh"
)

// 连接的配置
type ClientConfig struct {
	Host       string      //ip
	Port       int64       // 端口
	Username   string      //用户名
	Password   string      //密码
	Client     *ssh.Client //ssh client
	LastResult string      //最近一次运行的结果
}

func (cliConf *ClientConfig) createClient(host string, port int64, username, password string) {
	var (
		client *ssh.Client
		err    error
	)
	cliConf.Host = host
	cliConf.Port = port
	cliConf.Username = username
	cliConf.Password = password
	cliConf.Port = port
	//一般传入四个参数:user,[]ssh.AuthMethod{ssh.Password(password)}, HostKeyCallback,超时时间,
	config := ssh.ClientConfig{
		User: cliConf.Username,
		Auth: []ssh.AuthMethod{ssh.Password(password)},
		HostKeyCallback: func(hostname string, remote net.Addr, key ssh.PublicKey) error {
			return nil
		},
		Timeout: 10 * time.Second,
	}
	addr := fmt.Sprintf("%s:%d", cliConf.Host, cliConf.Port)
	//获取client
	if client, err = ssh.Dial("tcp", addr, &config); err != nil {
		log.Fatalln("error occurred:", err)
	}
	cliConf.Client = client
}
func (cliConf *ClientConfig) RunShell(shell string) string {
	var (
		session *ssh.Session
		err     error
	)
	//获取session,这个session是用来远程执行操作的
	if session, err = cliConf.Client.NewSession(); err != nil {
		log.Fatalln("error occurred:", err)
	}
	//执行shell
	if output, err := session.CombinedOutput(shell); err != nil {
		log.Fatalln("error occurred:", err)
	} else {
		cliConf.LastResult = string(output)
	}
	return cliConf.LastResult
}
func main() {
	cliConf := new(ClientConfig)
	cliConf.createClient("ip", 22, "username", "pwd")
	fmt.Println(cliConf.RunShell("cd gotest;python test.py"))
}

服务器里面的代码是放在gotest文件夹里面的,而每一次运行都需要cd gotest,没有办法记录,每次执行一条命令都会创建一条session,而一条session默认只能执行一条命令,并且两条命令不可以分开写。也就是:

go 复制代码
fmt.Println(cliConf.RunShell("cd gotest;python test.py"))

这一行代码,不可以写成

go 复制代码
cliConf.RunShell("cd gotest")
fmt.Println(cliConf.RunShell("python test.py"))

下图是一个运行结果:

但是我将来肯定需要往里面放数据,所以写test2.py文件:

相应的修改main.go文件里面的runshell语句为:

go 复制代码
fmt.Println(cliConf.RunShell("cd gotest;python test.py"))

但是没有办法进行输入,且一直会报错

后面朋友说可以通过命令行写参数,所以把test2.py和main.go里面都进行了修改

go 复制代码
fmt.Println(cliConf.RunShell("cd gotest;python test.py 2 3"))

结果也是可以正确输出的

但是可以看到仅仅是一个加法,花费的时间是很长的,数据来回的传输是很耗时的。

看到go有内置的RPC,是一个进行远程控制的。

相关推荐
迎風吹頭髮3 小时前
UNIX下C语言编程与实践58-UNIX TCP 连接处理:accept 函数与新套接字创建
c语言·网络·unix
猫头虎7 小时前
如何查看局域网内IP冲突问题?如何查看局域网IP环绕问题?arp -a命令如何使用?
网络·python·网络协议·tcp/ip·开源·pandas·pip
hello_25010 小时前
动手模拟docker网络-bridge模式
网络·docker·桥接模式
武文斌7710 小时前
项目学习总结:LVGL图形参数动态变化、开发板的GDB调试、sqlite3移植、MQTT协议、心跳包
linux·开发语言·网络·arm开发·数据库·嵌入式硬件·学习
爱吃喵的鲤鱼10 小时前
仿mudou——Connection模块(连接管理)
linux·运维·服务器·开发语言·网络·c++
爱吃小胖橘11 小时前
Unity网络开发--超文本传输协议Http(1)
开发语言·网络·网络协议·http·c#·游戏引擎
萧鼎11 小时前
Python schedule 库全解析:从任务调度到自动化执行的完整指南
网络·python·自动化
7哥♡ۣۖᝰꫛꫀꪝۣℋ12 小时前
网络层--数据链路层
网络·tcp/ip·智能路由器
_清浅12 小时前
计算机网络【第四章-网络层】
网络·计算机网络·智能路由器
沐浴露z12 小时前
【深入理解计算机网络08】网络层之IPv4
网络·计算机网络·网络编程·信息与通信·408