【问题标题】:Code isn't uploading but printing in console代码未上传,但在控制台中打印
【发布时间】:2018-11-22 09:21:29
【问题描述】:

我已经编写了这段代码,它应该上传一些东西,但它不上传任何东西,只是在我的控制台中打印命令的结果,并且不上传任何东西到网站

import requests, os, re, subprocess
from bs4 import BeautifulSoup

command = 'netsh interface ip show addresses "Ethernet" | findstr /c:IP'
outcome = str(subprocess.Popen(command, shell=True).wait())
#soup = BeautifulSoup(outcome,'lxml')

#outcome = outcome.replace('0','')

#print str(subprocess.call(command, shell=True))

requests.post("example.com", {'field1': outcome})

和控制台输出:

IP Address:                           192.168.1.23

上传到网站0

我试过了:

import requests, os, re, subprocess
from bs4 import BeautifulSoup

command = 'netsh interface ip show addresses "Ethernet" | findstr /c:IP'
#subprocess.communicate(command, shell=True)
subprocess.Popen(command, shell=True).communicate()
outcome = subprocess.PIPE
#soup = BeautifulSoup(outcome,'lxml')

#outcome = outcome.replace('0','')

#print str(subprocess.call(command, shell=True))

requests.post("example.com", {'field1': outcome})

和控制台输出:

IP Address:                           192.168.1.23

哪些上传到网站-1

我想要它做的是将该 IP 地址结果上传到网站。

【问题讨论】:

  • 此代码中的任何内容都不应打印我能看到的任何内容。如何获得“成果”?
  • @roganjosh 好吧,它应该将结果上传到网站,但它会在我执行脚本的控制台中打印它。如果有什么不同,我正在使用 python2.7
  • @roganjosh 我编辑了问题,请看看你能做什么

标签: python python-2.7 python-requests


【解决方案1】:

您没有正确使用子流程。我建议你使用subprocess.Popen.communicate,并传入stdout=subprocess.PIPE 来实际获取子进程的stdout。

p = subprocess.Popen(["my", "command"], stdout=subprocess.PIPE)
stdout, _ = p.communicate()

【讨论】:

  • 我不工作。我收到以下错误subprocess.communicate(command, shell=True) AttributeError: 'module' object has no attribute 'communicate'
猜你喜欢
  • 2012-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-09
  • 1970-01-01
  • 2021-05-10
相关资源
最近更新 更多