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

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

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.