在启动问题上以root身份运行Python

问题描述 投票:0回答:1

如果这个问题已经被问到了,通常会提前道歉,但我已经花了几个小时尝试以前的答案,所以我很迷茫。

我有下面的python脚本,在pi zero上运行,以检测RFID卡,并作为HID键盘来输入密码。它不安全,我知道,但它的工作完美。

当我单独SSH进入pi并启动脚本时,它可以工作。我试图让它在启动时启动,但效果不是很好。我已经尝试了bashrc,init.d和任何其他我在努力中发现的方法,所以任何其他帮助将是非常感激的。

我也试过通过bash脚本来筛选,担心我的脚本需要一个活动的shell,没有用。

这是我的脚本。

import time
import RPi.GPIO as GPIO

import MFRC522
import signal
import datetime
now = datetime.datetime.now()
continue_reading = True

NULL_CHAR = chr(0)
def writetofile(scr):
    f = open("logs.txt", "a")
    tim = now.strftime("%Y-%m-%d %H:%M:%S")
    if scr == "error":
        f.write("Got error at ")
        f.write(tim)
        f.write("\r\n")
    else:
        f.write("Got Success with ")
        f.write(scr)
        f.write(" at ")
        f.write(tim)
        f.write("\r\n")

# function to send the data
def write_report(report):
    with open('/dev/hidg0', 'rb+') as fd:
        fd.write(report.encode())
def login():
    ##writes my password, commands removed for stack :)


def uidToString(uid):
    mystring = ""
    for i in uid:
        mystring = format(i, '02X') + mystring
    return mystring


def end_read(signal, frame):
    global continue_reading
    print("Ctrl+C captured, ending read.")
    continue_reading = False
    GPIO.cleanup()


signal.signal(signal.SIGINT, end_read)


MIFAREReader = MFRC522.MFRC522()


while continue_reading:

    # Scan for cards
    (status, TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

    # If a card is found
    if status == MIFAREReader.MI_OK:
        print ("Card detected")
                # Get the UID of the card
        (status, uid) = MIFAREReader.MFRC522_SelectTagSN()
        # If we have the UID, continue
        if status == MIFAREReader.MI_OK:
            carduid = uidToString(uid)
            if carduid == '75AC5685':
                print("KeyRing")
                writetofile("Keyring")
                login()
            elif carduid == 'D539F1FD':
                print("Keycard")
                writetofile("Keycard")
                login()
            print("Card read UID: %s" % carduid)
        else:
            print("Authentication error")
            writetofile("error")

谢谢!

python raspberry-pi rfid
1个回答
0
投票

感谢@lxop的帮助,不过这不是我的问题。

对不起,浪费了这么多帖子,我的错误是我在引用文件时用了~目录,显然没有登录。现在一切正常了。不过还是要谢谢你

© www.soinside.com 2019 - 2024. All rights reserved.