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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题