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

无法通过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?

  • 写回答

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())

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?