donglu5000
2019-06-17 13:27
浏览 145
已采纳

无法通过python变量进行二进制处理

In my python script I am trying to pass a variable to a Go binary to perform an operation and retrieve output from Go binary as variable and use it in my python script

This is my Go Program which I am using to create my binary and I am creating binary

package main

import (
    "fmt"
    "log"
    "os"
    "strconv"

    ps "github.com/mitchellh/go-ps"
)

var args_pid string
var xyz int

func main() {
args_pid = os.Args[1]

    first, err := strconv.ParseInt(args_pid, 10, 0)
    if err != nil {
        fmt.Println(err)
        os.Exit(2)
    }
    xyz = int(first)
    pp, _ := ps.FindProcess(xyz)
    log.Printf("The pid of passed process is %v
", pp.Pid())
}

This is my python script where I am using this binary

import os
args_pid = 1401
cmd = './process_pid args_pid'
so = os.popen(cmd).read()
print so

I get the following error: strconv.ParseInt: parsing "args_pid": invalid syntax

Can someone help me resolve this issue?

图片转代码服务由CSDN问答提供 功能建议

在我的python脚本中,我试图将变量传递给Go二进制文件以执行操作并从Go检索输出 将二进制文件作为变量并在我的python脚本中使用它

这是我用来创建二进制文件和Go程序的Go程序

   package main 
 
import(
“ fmt” 
“ log” 
“ os” 
“ strconv” 
 
 ps“ github.com/mitchellh/go-ps” 
)\  n 
var args_pid字符串
var xyz int 
 
func main(){
args_pid = os.Args [1] 
 
首先,错误:= strconv.ParseInt(args_pid,10,0)
如果错误!  = nil {
 fmt.Println(err)
 os.Exit(2)
} 
 xyz = int(first)
 pp,_:= ps.FindProcess(xyz)
 log.Printf(“ 通过的进程的pid是%v 
“,pp.Pid())
} 
   
 
 

这是我使用此二进制文件的python脚本< / p>

  import os 
args_pid = 1401 
cmd ='./process_pid args_pid'
so = os.popen(cmd).read()
print so 
    
 
 

我得到了关注 g错误: strconv.ParseInt:解析“ args_pid”:语法无效

有人可以帮助我解决此问题吗?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtl19910708 2019-06-17 13:32
    已采纳
    args_pid = 1401
    cmd = './process_pid {}'.format(args_pid)
    so = os.popen(cmd).read()
    print so
    

    You passed string args_pid to the Go binary instead of the actual value.


    When it comes to passing the answer, I'd go with something like this: your Go program could print the PID to stdout using fmt.Printf("%d", pp.Pid()).

    Then what your Python code does to retrieve the answer is ok. You just need to cast it to int: so = int(os.popen(cmd).read())

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题