如何将html属性传递给Python脚本?

2024-09-21 05:38:56 发布

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

我有一个python脚本来验证ID号。我也有html表单,我想把一个字段的字符串传递给python脚本并返回答案。 我的剧本看起来像:

#!/usr/bin/python

import sys, string, re

def checkID():
    error=False
    if len(sys.argv) <> 2:
        print "Error: Missing argument (Usage: id ID_Number\n"
    else:
        idNumber=sys.argv[1]
        for oneDigit in idNumber:
            if not(oneDigit in string.digits):
                error=True
                break
        if error:
            print "Error: Illegal ID number: '%s'\n" % idNumber
        else:
            if len(idNumber) > 9:
                error=True
                print "Error: ID number too long: '%s'\n" % idNumber

    return error


badNumber = checkID(sys.argv[1])
newID = ''
if not badNumber:
    if len(sys.argv[1]) < 9:
        missingDigits = 9 - len(sys.argv[1])
        while missingDigits > 0:
            newID=newID+'0'
            missingDigits = missingDigits - 1
        newID = newID + sys.argv[1]
    else:
        newID = sys.argv[1]

theSum = 0
indexRange=[0,2,4,6]
for i in indexRange:
    digit = newID[i]
    num= int(digit)
    theSum = theSum + num
    num= int(newID[i+1]) * 2
    if num > 9:
        num1 = num / 10
        num2 = num % 10
        num = num1 + num2
    theSum = theSum + num
theSum = theSum + int(newID[8])
lastDigit = theSum % 10
if lastDigit <> 0:
    print '\nID number: "%s" is wrong\n' % str(newID)
else:
    print '\nID number: "%s" is valid\n' % str(newID)

因此Python将检查html传递的字符串并对其进行验证。 我该怎么做?你知道吗


Tags: idnumberlenifsyserrorelsenum

热门问题