小编pyn*_*exj的帖子

如何防止 pexpect 回显密码?

默认情况下,pexpect.spawn()不会输出任何内容。但是当我指定logfile=sys.stdout它也会回显密码(例如 for ssh)。那么如何在spawn不回显密码的情况下查看与ed 进程的实时交互(就像ExpectTcl扩展)那样)?

示例:

# cat expect.py
import pexpect, sys

logfile = sys.stdout if len(sys.argv) == 2 else None

ssh = pexpect.spawn('ssh foo@localhost', logfile=logfile)
ssh.delaybeforesend = 1
ssh.expect('assword:')
ssh.sendline('123456')

ssh.expect('\r\n\\$')
ssh.sendline('exit')
ssh.expect(pexpect.EOF)
ssh.wait()
# python expect.py                 <-- no output
# python expect.py stdout
foo@localhost's password: 123456   <-- the password is visible
Last login: Tue Mar 22 10:32:49 2016 from localhost
$ exit
exit
Connection to localhost closed.
# …
Run Code Online (Sandbox Code Playgroud)

expect python-2.x pexpect

5
推荐指数
1
解决办法
3805
查看次数

Python paramiko:重定向 stderr 受 get_pty = True 的影响

我正在尝试实现一个 ssh 代理,除其他外,它允许我稍后在阻塞模式下执行命令,在该模式下,只要输出可用,就会从通道中读取输出。
\n这是我到目前为止所拥有的:

\n\n
from paramiko import client\n\nclass SSH_Agent:\n\n    def __init__(self, server_name, username = getpass.getuser(), password = None, connection_timeout = CONNECTION_TIMEOUT):\n        self.ssh_agent = client.SSHClient()\n        self.ssh_agent.set_missing_host_key_policy(client.AutoAddPolicy())\n        self.ssh_agent.connect(server_name, username = username, password = password if password is not None else username, timeout = connection_timeout)\n\n\n    def execute_command(self, command, out_streams = [sys.stdout], err_streams = [sys.stderr], poll_intervals = POLL_INTERVALS):\n        stdin, stdout, stderr = self.ssh_agent.exec_command(command)\n        channel = stdout.channel\n        stdin.close()\n        channel.shutdown_write()\n\n        while not channel.closed or channel.recv_ready() or channel.recv_stderr_ready():\n            got_data = False\n            output_channels = select.select([channel], [], [], …
Run Code Online (Sandbox Code Playgroud)

python ssh paramiko stderr io-redirection

5
推荐指数
1
解决办法
5157
查看次数

React与酶的异步并期待

我正在使用摩卡,酶和期待我的测试.我在我的组件中有一个函数,它运行另一个返回promise的函数,我不知道如何在运行第二个函数之前测试第一个函数的行为,然后测试promise(在.then之后得到错误)

第一功能:

handleUpdateInput (value) {
    const { access, onUpdateInput } = this.props
    const v = !value || typeof value === 'string' ? value : access(value)
    if (onUpdateInput) {
      onUpdateInput(value ? v : '')
    }

    this.setState({
      searchText: value
    })

    this.dataSourceUpdate(value)
}
Run Code Online (Sandbox Code Playgroud)

第二功能:

dataSourceUpdate (value) {
    const { promise, access } = this.props

    if (value === '') {
      this.autoCompleteData = []
      this.setState({ dataSource: [] })
    } else {
      promise(value)
        .then(res => {
          this.autoCompleteData = res.data
          this.setState({
            dataSource: this.autoCompleteData.map(access).slice(0, getMenuItemNumber(this.refs.customAutoComplete))
          })
        })
        .catch(() …
Run Code Online (Sandbox Code Playgroud)

javascript mocha.js reactjs enzyme

5
推荐指数
1
解决办法
288
查看次数

flask-输入数据比@ api.expect更严格吗?

在我的flask-restplus API中,我不仅要检查输入数据,如以下示例所示

resource_fields = api.model('Resource', {
    'name': fields.String(default = 'string: name', required = True),
    'state': fields.String(default = 'string: state'),
})

@api.route('/my-resource/<id>')
class MyResource(Resource):
    @api.expect(resource_fields, validate=True)
    def post(self):
        ...
Run Code Online (Sandbox Code Playgroud)

必须具有“名称”字段,并且可能具有“状态”字段,还必须检查是否没有其他字段(如果发生这种情况,则会引发错误)。还有其他装饰器吗?我可以通过自定义功能检查输入数据的正确性吗?

python flask flask-restplus

5
推荐指数
2
解决办法
3385
查看次数

Jestjs中spyOn()。and.callfake的替代方法

我以前使用spyOn().and.callFake过茉莉花,它对我的​​测试有很大帮助,现在我使用的是Jest,我已经在文档中找到了,jest.spyOn()但是没有callFake

我的问题:如何监视方法并使用Jest和调用Fake expect

javascript reactjs jestjs

5
推荐指数
1
解决办法
3156
查看次数

Shell 脚本 - 迭代空格分隔的单词/字符(在 zsh 中)

我在弄清楚如何在 shell 脚本中迭代空格分隔的单词/字符时遇到了一些麻烦。例如,我想迭代一个变量,该变量包含字母表中由空格分隔的字符。

注意:即使字母表变量包含空格分隔的字符串而不是字符,结果也应该相同,即“aa bb cc ...”而不是“abc ..”

我已经尝试了很多提供的替代方法: How to split a line into words in bash in a or more space?

示例

  local alphabet="a b c d e f g h i j k l m n o p q r s t u v w x y z"
  local index="0"
  for character in $alphabet; do
      index=$((++index))                                      
      echo "$index. $character"
      # Possibility to do some more stuff
  done 
Run Code Online (Sandbox Code Playgroud)

预期/期望输出

1. a
2. b
3. c
and …
Run Code Online (Sandbox Code Playgroud)

iteration shell zsh

4
推荐指数
1
解决办法
4572
查看次数

如何使用 Python 捕获 subprocess.call 错误?

我正在尝试下载特定的 Docker 映像,用户将在其中输入版本。但是,如果版本不存在,Docker 将抛出错误。

我正在使用subprocess.call管道从 Python 3 连接到终端。

示例代码:

from subprocess import call
containerName = input("Enter Docker container name: ")
swVersion = input("Enter software version: ")

call(["docker", "run", "--name", "{}".format(containerName), 
      "--detach", "--publish", "8080:8080", "user/software:{}".format(swVersion)])
Run Code Online (Sandbox Code Playgroud)

如果未找到版本,docker 将在终端中输出:

docker: Error response from daemon: manifest for user/software:8712378 not found.
Run Code Online (Sandbox Code Playgroud)

如何在 Python 脚本中捕获此错误?

类似的东西:

try:
    call(["docker", "run", "--name", "{}".format(containerName), "--detach", "--publish", "8080:8080", "user/software:{}".format(swVersion)])
except:
    # How do I catch the piped response code here?`
Run Code Online (Sandbox Code Playgroud)

python subprocess docker

4
推荐指数
2
解决办法
1万
查看次数

如何检查系统是否支持"Monotonic Clock"?

我需要在代码中处理超时场景,并且clock_gettime(CLOCK_MONOTONIC)如果系统支持Monotonic Clock ,则需要使用.

#ifdef CLOCK_MONOTONIC
    clock_gettime(CLOCK_MONOTONIC, & spec);
#else
    clock_gettime(CLOCK_REALTIME,  & spec);
#endif
Run Code Online (Sandbox Code Playgroud)

我不确定这是否足够.也就是说,系统是否可能CLOCK_MONOTONIC通过它定义并不真正支持单调时钟?或者检查是否支持单调时钟的可靠方法是什么?

c unix linux posix

4
推荐指数
1
解决办法
630
查看次数

如何检查安装的 Mercurial (hg) 使用的是 Python2 还是 Python3?

我想使用hg-gitpython pip 安装扩展,但我不确定是否应该使用pip(Python2)或pip3(Python3)。

hg version给出这个:

$ hg version
Mercurial Distributed SCM (version 4.8.2)
(see https://mercurial-scm.org for more information)

Copyright (C) 2005-2018 Matt Mackall and others
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Enabled extensions:

  fetch     internal
  graphlog  internal
  histedit  internal
  strip     internal
  mq        internal
  purge     internal
  rebase    internal
  record    internal
$
Run Code Online (Sandbox Code Playgroud)

我先尝试了一下pip3 install hg-git,安装后没用。然后我就用了 …

python mercurial hg-git

4
推荐指数
1
解决办法
1228
查看次数

GitHub Actions:如何将 toJSON() 结果传递给 shell 命令

因此,我正在与 Github Actions 合作进行端到端测试。我正在查看的设置是让一项作业检索要测试的 url 列表,而我的第二项作业使用该列表创建一个矩阵并测试所有这些。我的问题是,当我实际运行测试脚本时,必须从命令行完成,因为我使用的是 Playwright。因此我不能直接使用我的矩阵对象;我必须将其输出到 JSON 文件。问题是,当我将 toJSON 输出到我的文件时,它会创建无效的漂亮打印 JSON,这会破坏我的脚本。这是我的代码:

name: <name>

on:
    push:
    workflow_dispatch:

jobs:
    fetch_strategic_urls:
        runs-on: ubuntu-latest
        outputs:
            urls: ${{ steps.req-urls.outputs.urls }}
        steps:
            - name: Request Urls
              id: req-urls
              run: |
                  export RESPONSE=$(
                    curl -X GET -H "Accept: application/json" <api-endpoint>)
                  echo "::set-output name=urls::$RESPONSE"
    run_tests:
        runs-on: ubuntu-latest
        strategy:
            matrix:
                url: ${{needs.fetch_strategic_urls.outputs.urls}}
        needs: fetch_strategic_urls
        steps:
            - ...
            - ...
            - run: |
                  ls
                  echo '${{ toJSON(matrix.url) }}' >> props.json
                  cat props.json
                  npm test
              working-directory: E2E.Tests
Run Code Online (Sandbox Code Playgroud)

无论echo ${{matrix.url}} …

bash json yaml jsonparser github-actions

4
推荐指数
1
解决办法
1万
查看次数