从单个 python 线程中,如下所示,我收到错误“请在任何插槽中插入令牌”,它似乎看不到我的令牌。我将代码更改为不在多处理线程内部运行,它可以工作。为了将 PyKCS11 库排除在等式之外,我还通过使用 ctypes 并包装了在 opensc 中实现的标准 pkcs11 函数进行了测试,除了从 python 线程运行时,我仍然遇到相同的问题。什么会导致这个?
从 python 线程内部使用 pkcs11 失败:
from PyKCS11 import LowLevel
import sys
from multiprocessing import Thread
class MyThread(Thread):
def run(self):
lib = "/usr/local/lib/opensc-pkcs11.so" # place here your PKCS#11 library
pin = "12345678" # place here the pin of your token
a = LowLevel.CPKCS11Lib()
info = LowLevel.CK_INFO()
slotList = LowLevel.ckintlist()
loadRes = a.Load(lib, 1)
print "Load of library '%s' : %s " % (lib, str(loadRes) )
if not loadRes:
sys.exit(1)
print "C_GetInfo: rv=" , hex(a.C_GetInfo(info))
print "Library manufacturerID: ", info.GetManufacturerID()
# listing only slots with a token inside.
rv = a.C_GetSlotList(1, slotList)
if (rv != LowLevel.CKR_OK):
sys.exit(1)
if len(slotList) == 0:
print "Please insert a token in any slot"
sys.exit(1)
mythread = MyThread()
mythread.start()
mythread.join()
在 Thread 之外使用 pkcs11 可以:
from PyKCS11 import LowLevel
import sys
def run(self):
lib = "/usr/local/lib/opensc-pkcs11.so" # place here your PKCS#11 library
pin = "12345678" # place here the pin of your token
a = LowLevel.CPKCS11Lib()
info = LowLevel.CK_INFO()
slotList = LowLevel.ckintlist()
loadRes = a.Load(lib, 1)
print "Load of library '%s' : %s " % (lib, str(loadRes) )
if not loadRes:
sys.exit(1)
print "C_GetInfo: rv=" , hex(a.C_GetInfo(info))
print "Library manufacturerID: ", info.GetManufacturerID()
# listing only slots with a token inside.
rv = a.C_GetSlotList(1, slotList)
if (rv != LowLevel.CKR_OK):
sys.exit(1)
if len(slotList) == 0:
print "Please insert a token in any slot"
sys.exit(1)
run()
测试环境:
操作系统:OSX 优胜美地
pkcs11 中间件:opensc