“libnfc Django”如何使用Django运行外部python脚本?

2024-04-20 14:34:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用来自djangotutsme的django扩展来运行mysqlNFC.py脚本

执行此操作后./manage.py runscript mysqlNFC

错误消息

/usr/local/lib/python2.7/dist-packages/django/core/management/base.py:265: RemovedInDjango110Warning: OptionParser Usage for Django Management commands is deprecated, use ArgumentParser instead
      RemovedInDjango110Warning)

error     libnfc.driver.acr122_usb     Unable to claim USB interface (operation not permitted 
    nfs-list: EREROR: Unable to open NFC device: acr122_usb:001:007
no (valid module for script 'mysqlNFC' found Try running with a higher verbosity level like: -v2 or -v3

即使有了./manage.py unscript -v 2 mysqlNFC,它也比django模型更具优势

如何运行mysqlNFC.py脚本?在

在设置.py

^{pr2}$

脚本目录

scripts/
├── __init__.py
├── __init__.pyc
├── mysqlNFC.py
└── mysqlNFC.pyc

我试图运行的脚本mysqlNFC.py

#!usr/bin/python
import MySQLdb, datetime, shlex, subprocess, re, time
from nfcapp.models import USER, CLASS, TIMETABLE, ATTENDANCE

db = MySQLdb.connect("localhost","root","","AttendanceDB")
curs = db.cursor()

output = subprocess.check_output(["nfc-list"])
output = output.decode("utf8")

uid = re.search(r'((\w){2}\s\s){4}',output).group(0).strip().split("  ")
uid = ' '.join(uid).upper()

print ("\n\nUID is: ")
print (uid)

#verify connectivity to database 
sql = "SELECT * FROM USER WHERE USER.uid = '%s'" % (uid)

try:
        curs.execute(sql)
        result = curs.fetchone()
except:
        print "unable to get info from DB"


if result is None:
        print("UID non existant, would you like to add user?")
        #id change log
        id = raw_input("Please enter your id card no: ")
        name = raw_input("Please enter your name: ")
        lastname = raw_input("Please enter your surname: ")

        s = "Thank you, your id card is: %s, your name is: %s, your surname is %s" % (id, name,lastname)
        print(s)

        sql = "INSERT INTO USER (id,uid,name,surname) VALUES ('%s','%s','%s','%s')" % (id, uid,name,lastname)

        #cha

        try:
                curs.execute(sql)
                db.commit()

        except:
                print "unable to get info from DB"

在模型.py

由于冗余,下面没有提到一些类

from __future__ import unicode_literals
from django.db import models

##############################################################################
# Section: Maintain USER Objects --------------------------------------- #
##############################################################################
class USER(models.Model):
    # verbose is visible in any pages, but uid=models.chardiled is in the db
    uid = models.CharField(max_length = 20, verbose_name = 'uid')
    name = models.CharField(max_length = 15, verbose_name = 'name')
    surname = models.CharField(max_length = 15, verbose_name = 'surname')
    # in sqlite 
    # surname VARCHAR(15)

    def __str__(self):
        ''' Note: Return Human Readable Object '''
        return 'USER %s: %s: %s: %s:' % (self.id, self.uid, self.name, self.surname)

    #constsructers 
    @classmethod
    def initialise(cls, id, uid, name, surname):
        ''' Note: Overloading Instances Method '''
        return cls(id = id, uid = uid, name = name, surname = surname)

    class Meta:
        ''' Note: Model Meta Options '''
        verbose_name_plural = 'USER'
        db_table = 'USER'

在管理员py

from django.contrib import admin
from .models import USER, CLASS, TIMETABLE, ATTENDANCE
# Register your models here.

##############################################################################
# Section: Maintain USER Admin -------------------------------------------- #
##############################################################################
@admin.register(USER)
class UserAdmin(admin.ModelAdmin):
    ''' Note: Class to maintain USER Admin Interface '''
    list_display  = [ '__str__' ]
    fields        = [  ]
    search_fields = [  ]

    ordering = [ 'id' ]
    list_max_show_all = True
    inlines  = [  ]

Tags: tonamefrompyimportiddbyour