【问题标题】:getchar in python returns permission denied (andorid 8.0)python中的getchar返回权限被拒绝(android 8.0)
【发布时间】:2018-09-11 11:01:12
【问题描述】:

我正在开发termuxandroid 8.0。

我正在使用getchar的以下实现:

class _Getch:
    """Gets a single character from standard input.  Does not echo to the
screen."""
    def __init__(self):
        try:
            self.impl = _GetchWindows()
        except ImportError:
            self.impl = _GetchUnix()

    def __call__(self): return self.impl()


class _GetchUnix:
    def __init__(self):
        import tty, sys

    def __call__(self):
        import sys, tty, termios
        fd = sys.stdin.fileno()
        old_settings = termios.tcgetattr(fd)
        try:
            tty.setraw(sys.stdin.fileno())
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)

tcsetattr 的调用返回权限被拒绝。我猜是一个新的security feature

Traceback (most recent call last):
  File "piano.py", line 103, in <module>
    char=getch()
  File "piano.py", line 21, in __call__
    def __call__(self): return self.impl()
  File "piano.py", line 33, in __call__
    tty.setraw(sys.stdin.fileno())
  File "/data/data/com.termux/files/usr/lib/python3.6/tty.py", line 28, in setraw
    tcsetattr(fd, when, mode)
termios.error: (13, 'Permission denied')

如何克服这个问题? (readchar 包报同样的错误)

谢谢。

【问题讨论】:

  • 请向我们展示完整的错误回溯!
  • 有一个关于它的错误报告:github.com/termux/termux-packages/issues/1359
  • 是的,这是真的。我看到了。我希望有一个实用的解决方案,例如向应用程序添加权限或使用其他方法获取单个字符。虽然,改变操作模式似乎是一种自然的开始方式。

标签: android python getchar termux


【解决方案1】:

嗯,这是我的解决方案。

class _GetchUnix:
    def __init__(self):
        import tty, sys

    def __call__(self):
        import subprocess,sys
        t=subprocess.check_output(['bash','-c','read -s -n1 ans; echo $ans'],stdin=sys.stdin)
        return chr(t[0])

【讨论】:

  • android 8.0 上似乎没有 bash;我在手机上运行了这段代码,得到了No such file or directory: 'bash'
  • 我在 termux 工作,这是一个更类似于常规 linux 操作系统(如 Ubuntu)的环境。你做什么工作?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-12
  • 2014-02-05
  • 1970-01-01
  • 1970-01-01
  • 2020-12-31
  • 1970-01-01
相关资源
最近更新 更多