centos CTRL+C创建期望值

jecbmhm3  于 2022-11-07  发布在  其他
关注(0)|答案(1)|浏览(167)

我正在我的树莓派2中开发一个小程序,使用CentOS 8和Python 3.6
该程序工作完美,但是,当我点击CTRL+C我得到这个奇怪的异常

^CTraceback (most recent call last):
  File "NBScanner/main.py", line 1, in <module>
    import requests
  File "/root/NBScanner/venv/lib/python3.6/site-packages/requests/__init__.py", line 43, in <module>
    import urllib3
  File "/root/NBScanner/venv/lib/python3.6/site-packages/urllib3/__init__.py", line 7, in <module>
    import logging
  File "/usr/lib/python3.6/logging/__init__.py", line 28, in <module>
    from string import Template
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 674, in exec_module
  File "<frozen importlib._bootstrap_external>", line 779, in get_code
  File "<frozen importlib._bootstrap_external>", line 487, in _compile_bytecode
KeyboardInterrupt

有时不,有时其他例外,我不明白为什么!
这是我的密码也许能帮到你

import requests
import sys

CHARMAP_LOWERCASE = {4: 'a', 5: 'b', 6: 'c', 7: 'd', 8: 'e', 9: 'f', 10: 'g', 11: 'h', 12: 'i', 13: 'j', 14: 'k',
                     15: 'l', 16: 'm', 17: 'n', 18: 'o', 19: 'p', 20: 'q', 21: 'r', 22: 's', 23: 't', 24: 'u', 25: 'v',
                     26: 'w', 27: 'x', 28: 'y', 29: 'z', 30: '1', 31: '2', 32: '3', 33: '4', 34: '5', 35: '6', 36: '7',
                     37: '8', 38: '9', 39: '0', 44: ' ', 45: '-', 46: '=', 47: '[', 48: ']', 49: '\\', 51: ';',
                     52: '\'', 53: '~', 54: ',', 55: '.', 56: '/'}
CHARMAP_UPPERCASE = {4: 'A', 5: 'B', 6: 'C', 7: 'D', 8: 'E', 9: 'F', 10: 'G', 11: 'H', 12: 'I', 13: 'J', 14: 'K',
                     15: 'L', 16: 'M', 17: 'N', 18: 'O', 19: 'P', 20: 'Q', 21: 'R', 22: 'S', 23: 'T', 24: 'U', 25: 'V',
                     26: 'W', 27: 'X', 28: 'Y', 29: 'Z', 30: '!', 31: '@', 32: '#', 33: '$', 34: '%', 35: '^', 36: '&',
                     37: '*', 38: '(', 39: ')', 44: ' ', 45: '_', 46: '+', 47: '{', 48: '}', 49: '|', 51: ':', 52: '"',
                     53: '~', 54: '<', 55: '>', 56: '?'}
CR_CHAR = 40
SHIFT_CHAR = 2
ERROR_CHARACTER = '?'

def barcode_reader():
    barcode_string_output = ''
    CHARMAP = CHARMAP_LOWERCASE
    with open('/dev/hidraw0', 'rb') as fp:
        while True:
            for char_code in [element for element in fp.read(8) if element > 0]:
                if char_code == CR_CHAR:
                    if barcode_string_output == "exit" or barcode_string_output == "^C":
                         sys.exit("Scanner Closed ..")
                    return barcode_string_output
                if char_code == SHIFT_CHAR:
                    CHARMAP = CHARMAP_UPPERCASE
                else:
                    barcode_string_output += CHARMAP.get(char_code, ERROR_CHARACTER)
                    CHARMAP = CHARMAP_LOWERCASE

def send_barcode(barcode):
    request= requests.post('http://localhost',data={'code':barcode})
    print(request)

def read_barcode():
    try:
        while True:
            upcnumber = barcode_reader()
            send_barcode(upcnumber)
            print(upcnumber)
            return upcnumber
    except KeyboardInterrupt:
        sys.exit("Scanner Closed ..")
    except Exception as err:
        print(err)

if __name__ == '__main__':
    barcode = read_barcode()

还有谢谢

rkue9o1l

rkue9o1l1#

如果你按下ctrl+c,这是正常的键盘中断在linux。即你退出/取消当前运行的进程。
终端甚至会使用关键字KeyboardInterrupt通知您。
此外,如果我没有弄错的话,这是普遍同意的退出/取消当前运行进程的键绑定。
编辑:很像@matszwecja说的,你可以使用RMB(鼠标右键)或Ctrl+Shift+C(只要你突出显示它)从Linux上大多数可用的终端复制。

相关问题