惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
Webroot Blog
Webroot Blog
GbyAI
GbyAI
S
SegmentFault 最新的问题
Cyberwarzone
Cyberwarzone
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 【当耐特】
S
Secure Thoughts
酷 壳 – CoolShell
酷 壳 – CoolShell
AWS News Blog
AWS News Blog
Engineering at Meta
Engineering at Meta
S
Security Affairs
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
Hugging Face - Blog
Hugging Face - Blog
Google DeepMind News
Google DeepMind News
Spread Privacy
Spread Privacy
T
Threatpost
Forbes - Security
Forbes - Security
C
Cisco Blogs
Scott Helme
Scott Helme
Attack and Defense Labs
Attack and Defense Labs
Simon Willison's Weblog
Simon Willison's Weblog
腾讯CDC
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
Last Week in AI
Last Week in AI
Recorded Future
Recorded Future
小众软件
小众软件
V
Vulnerabilities – Threatpost
美团技术团队
人人都是产品经理
人人都是产品经理
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
Hacker News - Newest:
Hacker News - Newest: "LLM"
I
Intezer
月光博客
月光博客
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 司徒正美
C
Cybersecurity and Infrastructure Security Agency CISA
Martin Fowler
Martin Fowler
博客园 - 聂微东

邢平cn's blog

甲骨文密钥 ssh 连不上且忘记登录密码解决方法 让 traefik 代替你的 waf(使用 CrowdSec)+ nginx + acme.sh + mtls-auth 使用 rclone 和 alist 加密和备份你的数据到公有云 邓晓芒《黑格尔辩证法讲演录》读书笔记 如何白嫖和使用 GitHub models 的 LLM(gpt4o 和 Llama-3.1-405b) 提升部署在 cloudflare、vercel 或 netlify 的网站在中国国内的访问速度和稳定性 免费通过 NS1 利用监控宝平台实现实时基于不同运营商的故障转移 记录某一测速网站的 js 逆向的过程 列举几个免费域名和权威 dns 服务器 Hexo 博客折腾 docker 学习笔记 尝试在闲置的 surface pro 4 上运行安卓应用 国内各个 as 自治系统[转] 『现学现忘』Git基础 — 19、在Git中进行忽略文件操作 让在 vpc 中的 aws lambda 函数不通过 nat 获得访问互联网的能力 cron 笔记 curl 遇到的坑 你的常用域名被其他 vercel 账号使用的解决方法 解决升级 umamiv2.11 时出现的 P3009 错误 v2ray+ws+tls 太慢解决方法 在存在父子文件结构情况下使用 python import test
在 python 中运行 bash 实时输出结果(windows)
xingpingcn · 2023-04-01 · via 邢平cn's blog

在 windows 系统下的 python 中使用环境变量运行 bash

python

在 windows 的 cmd 中是不能运行 bash 的,我们需要利用 git 工具的 bash 来运行,但是用 subprocess.run()会出问题,例如不能使用已经添加到环境变量的命令,如 nvm、adb 等;因此改用 subprocess.Popen()

批量输入并且最后输出所有结果

bash_path 用于定位你的 git 工具的 bash,参数 cwd= 用于定位你的工作目录,和在文件夹中右键 git bash here 是一样的效果。

输入的命令可以带有空格,但是必须在最后加上 \n

import subprocess

bash_path = r'F:\Program Files\Git\bin\bash.exe'

subp = subprocess.Popen(bash_path,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding='utf8',cwd='$your_path')

#输入的命令可以带有空格,但是必须在最后加上`\n`

subp.stdin.write('nvm -v\n')

subp.stdin.flush()

subp.stdin.write('nvm -v\n')

subp.stdin.flush()

subp.stdin.write('nvm -v\n')

subp.stdin.flush()

#不要忘记了关闭subp,否则会阻塞

subp.stdin.close()

print(subp.stdout.read())

实时输入并输出结果

需要用到多线程 threading 和队列 queue 来实时获取输出结果。最后也不要忘记调用子进程的 close() 方法。

import subprocess

import queue

import threading,time

bash_path = r'F:\Program Files\Git\bin\bash.exe'

def thread_for_listen(subp,q):

for line in iter(subp.stdout.readline,''):

q.put(line)

subp = subprocess.Popen(bash_path,stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding='utf8',cwd='./public')

q = queue.Queue()

p1 = threading.Thread(target =thread_for_listen,args=(subp,q))

p1.start()

for i in range(5):

subp.stdin.write('nvm -v\n')

subp.stdin.flush()

print(q.get(),end='')

time.sleep(0.3)

print('wake up after 0.3s')

subp.stdin.close()

q.task_done()

1.1.11

wake up after 0.3s

1.1.11

wake up after 0.3s

1.1.11

wake up after 0.3s

1.1.11

wake up after 0.3s

1.1.11

wake up after 0.3s