我正在使用Python3.6,当我尝试使用pip3安装"模块"时,我面临下面提到的问题"pip配置了需要TLS/SSL的位置,但是Python中的ssl模块不可用"
请帮我解决这个问题
child = pexpect.spawn ('/bin/bash')
child.sendline('ls')
print(child.readline())
print child.before, child.after
Run Code Online (Sandbox Code Playgroud)
我在输出中得到的所有代码都是
ls
ls
Run Code Online (Sandbox Code Playgroud)
但是当我的代码是
child = pexpect.spawn('ls')
print(child.readline())
print child.before, child.after
Run Code Online (Sandbox Code Playgroud)
然后它可以工作,但只适用于前两个打印.我使用错误的发送命令吗?我试过发送,写入,发送线,再也找不到了.
寻求意味着让Fabric自动(而不是用户交互式)与shell命令交互(而不仅仅是密码请求,还有没有"stdin/interactive override"的请求用户输入apt-get install -y).
这个问题以及这些Fabric文档表明Fabric只能"将交互性"推回到运行Fabric程序的人类用户.寻求在没有任何人类存在的情况下完全自动化.还没有一个"真正的"当前问题需要解决,只是为可能的未来障碍做准备.
如果Fabric不能自动独占处理所有stdin /提示,那么与pexpect(或类似的替代机制)结合可能很有用吗?希望它不需要是"任何/或"类型的东西.为什么不在适当的情况下(如果适用)在同一程序/自动化中同时利用(pexpect和Fabric)?
我需要做这样的帖子,但是我需要创建一个可以给出输入并多次输出的子进程.该帖子的接受答案有良好的代码......
from subprocess import Popen, PIPE, STDOUT
p = Popen(['grep', 'f'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
grep_stdout = p.communicate(input=b'one\ntwo\nthree\nfour\nfive\nsix\n')[0]
print(grep_stdout.decode())
# four
# five
Run Code Online (Sandbox Code Playgroud)
......我想继续这样:
grep_stdout2 = p.communicate(input=b'spam\neggs\nfrench fries\nbacon\nspam\nspam\n')[0]
print(grep_stdout2.decode())
# french fries
Run Code Online (Sandbox Code Playgroud)
但是,我得到以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/subprocess.py", line 928, in communicate
raise ValueError("Cannot send input after starting communication")
ValueError: Cannot send input after starting communication
Run Code Online (Sandbox Code Playgroud)
如果我理解正确的话,proc.stdin.write()方法不能让你收集输出.什么是保持线路开放以进行持续输入/输出的最简单方法?
编辑:====================
它看起来像是pexpect一个有用的库,我正在尝试做什么,但我无法让它工作.这是对我实际任务的更完整的解释.我hfst用来获得个别(俄语)单词的语法分析.以下演示了它在bash shell中的行为:
$ hfst-lookup analyser-gt-desc.hfstol
> ?????
????? ?????+N+Neu+Inan+Sg+Acc …Run Code Online (Sandbox Code Playgroud) 我试图在Python中找到一种方法来运行其他程序:
这是我到目前为止所得到的...方法1:
def method1(command):
## subprocess.communicate() will give us the stdout and stderr sepurately,
## but we will have to wait until the end of command execution to print anything.
## This means if the child process hangs, we will never know....
proc=subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, executable='/bin/bash')
stdout, stderr = proc.communicate() # record both, but no way to print stdout/stderr in real-time
print ' ######### REAL-TIME ######### '
######## Not Possible
print ' ########## RESULTS ########## …Run Code Online (Sandbox Code Playgroud) 我试图通过我已经定义的记录器获得pexepct stdout日志.下面是代码
import logging
import pexpect
import re
import time
# this will be the method called by the pexpect object to log
def _write(*args, **kwargs):
content = args[0]
# let's ignore other params, pexpect only use one arg AFAIK
if content in [' ', '', '\n', '\r', '\r\n']:
return # don't log empty lines
for eol in ['\r\n', '\r', '\n']:
# remove ending EOL, the logger will add it anyway
content = re.sub('\%s$' % eol, '', content)
return logger.info(content) …Run Code Online (Sandbox Code Playgroud) 我正在使用以下代码将一些输入传递给具有pexpect的进程:
p = pexpect.spawn('cat', timeout=5.0 )
p.maxread = 5000
p.setecho(False) # prevent the process from echoing stdin back to us
INPUT_LEN = 1024
p.sendline('a'*INPUT_LEN)
print p.readline() # pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
Run Code Online (Sandbox Code Playgroud)
当INPUT_LEN <1024时,一切正常,但是对于> = 1024个字符,进程没有收到完整输入,导致在p.readline()上引发"pexpect.TIMEOUT"错误.
我尝试将输入分成小于1024个字符的片段,但这有同样的问题:
p = pexpect.spawn('cat', timeout=5.0 )
p.maxread = 5000
p.setecho(False)
INPUT_LEN = 1024
p.send('a'*1000)
p.sendline('a'*(INPUT_LEN-1000))
print p.readline() # pexpect.TIMEOUT: Timeout exceeded in read_nonblocking().
Run Code Online (Sandbox Code Playgroud)
有谁知道如何使用超过1024个字符的输入使pexpect工作?我试着查看源代码,但它似乎只是在调用os.write(...).
(作为旁注,我注意到当我从shell运行"cat"并尝试使用"Cmd + V"粘贴> = 1024个字符时发生相同的截断错误.但是,如果我运行"pbpaste,一切正常|猫".)
谢谢!
更新: 对"os.write()"的调用返回1025,表示写入成功,但os.read()返回"\ x07"(单个字符BEL),然后在下一次调用时挂起,导致超时.
将os.write()调用分为两个1024字节的write(),由os.fsync()调用分隔,不会改变任何东西.
我正在尝试运行一个基本的Pexpect脚本:
import pexpect
ftp_process = pexpect.spawn('ftp')
ftp_process.interact()
Run Code Online (Sandbox Code Playgroud)
当代码直接从终端运行时,代码按预期工作.如果我使用PyCharm的run/debug运行代码,我会收到以下错误:
Traceback (most recent call last):
File "/path/to/code/test.py", line 3, in <module>
ftp_process.interact()
File "/usr/local/lib/python3.4/site-packages/pexpect/__init__.py", line 1645, in interact
mode = tty.tcgetattr(self.STDIN_FILENO)
termios.error: (25, 'Inappropriate ioctl for device')
Run Code Online (Sandbox Code Playgroud)
似乎Pexpect如何与PyCharm的运行/调试窗口交互,默认情况下不起作用.有没有办法通过特定的PyCharm设置来解决这个问题?如果没有,还有其他方法可以解决这个问题吗?
编辑
上面的代码只是一个缩短的例子,导致了问题.Pexpect的的其他的能力(如expect(),sendline()等)仍需要的话.
pexpect ×10
python ×10
fabric ×2
python-3.x ×2
subprocess ×2
deployment ×1
expect ×1
io ×1
libraries ×1
macos ×1
paramiko ×1
pip ×1
pty ×1
pycharm ×1
python-2.7 ×1
ssh ×1
stdin ×1
stdout ×1
tty ×1