本文整理汇总了Python中pyndn.Data类的典型用法代码示例。如果您正苦于以下问题:Python Data类的具体用法?Python Data怎么用?Python Data使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Data类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: handleDataInterests
def handleDataInterests(self, prefix, interest, transport, prefixId):
# TODO: verification
# we match the components to the name, and any '_' components
# are discarded. Then we run a MongoDB query and append the
# object id to allow for excludes
chosenSchema = (s for s in self.schemaList
if s.dbPrefix.match(prefix)).next()
interestName = interest.getName()
responseName = Name(interestName)
nameFields = chosenSchema.matchNameToSchema(interestName)
self.log.info("Data requested with params:\n\t{}".format(nameFields))
allResults = []
segment = 0
try:
segmentComponent = interest.getName()[-4]
segment = segmentComponent.toSegment()
except RuntimeError:
pass
(startPos, results) = self._segmentResponseData(nameFields, segment)
for result in results:
dataId = result[u'_id']
self.log.debug("Found object {}".format(dataId))
allResults.append(result)
#responseName.append(str(dataId))
totalCount = results.count(False)
responseObject = {'count':totalCount, 'skip':startPos, 'results':allResults}
responseData = Data(responseName)
resultEncoded = BSON.encode(responseObject)
responseData.setContent(resultEncoded)
transport.send(responseData.wireEncode().buf())
示例2: onInterest
def onInterest(self, prefix, interest, transport, registeredPrefixId):
print "received interest"
initInterest = Name(interest.getName())
print "interest name:",initInterest.toUri()
d = Data(interest.getName().append(self.deviceComponent))
try:
print "start to set data's content"
currentString = ','.join(currentList)
d.setContent("songList of " +self.device+":"+currentString+ "\n")
self.face.registerPrefix(self.changePrefix,self.onInterest,self.onRegisterFailed)
except KeyboardInterrupt:
print "key interrupt"
sys.exit(1)
except Exception as e:
print e
d.setContent("Bad command\n")
finally:
self.keychain.sign(d,self.certificateName)
encodedData = d.wireEncode()
transport.send(encodedData.toBuffer())
print d.getName().toUri()
print d.getContent()
self.loop.close()
self.face.shutdown()
self.face = None
示例3: onDeviceListRequest
def onDeviceListRequest(self, prefix, interest, transport, registeeredPrefix):
interestName = interest.getName()
username = interestName.get(4).getValue().toRawStr()
#TODO guest check
prefix = Name("/home/user/"+username)
userHash = self._deviceUserAccessManager.getUserHash(prefix)
userHMACKey = HMACKey(0,0,userHash,"userHMACKey")
dump("device dict : ",self._deviceDict)
if ( self._accessControlManager.verifyInterestWithHMACKey(interest,userHMACKey) ):
dump("Verified")
deviceProfileList = []
for prefixStr in self._deviceDict.keys():
tmp = self._deviceDict[prefixStr]['deviceProfile'].__dict__
dump("tmp :",tmp)
if (type(tmp["_prefix"]) != str):
tmp["_prefix"] = tmp["_prefix"].toUri()
deviceProfileList.append(tmp)
data = Data(interestName)
data.setContent(json.dumps(deviceProfileList))
self._accessControlManager.signDataWithHMACKey(data,userHMACKey)
self.sendData(data,transport,sign=False)
else:
dump("Not verified")
示例4: benchmarkDecodeDataSeconds
def benchmarkDecodeDataSeconds(nIterations, useCrypto, encoding):
"""
Loop to decode a data packet nIterations times.
:param int nIterations: The number of iterations.
:param bool useCrypto: If true, verify the signature. If false, don't
verify.
:param Blob encoding: The wire encoding to decode.
"""
# Initialize the private key storage in case useCrypto is true.
identityStorage = MemoryIdentityStorage()
privateKeyStorage = MemoryPrivateKeyStorage()
keyChain = KeyChain(IdentityManager(identityStorage, privateKeyStorage),
SelfVerifyPolicyManager(identityStorage))
keyName = Name("/testname/DSK-123")
certificateName = keyName.getSubName(0, keyName.size() - 1).append(
"KEY").append(keyName[-1]).append("ID-CERT").append("0")
identityStorage.addKey(keyName, KeyType.RSA, Blob(DEFAULT_RSA_PUBLIC_KEY_DER))
start = getNowSeconds()
for i in range(nIterations):
data = Data()
data.wireDecode(encoding)
if useCrypto:
keyChain.verifyData(data, onVerified, onVerifyFailed)
finish = getNowSeconds()
return finish - start
示例5: generateData
def generateData(self, baseName):
'''
This appends the segment number to the data name, since repo-ng tends to expect it
'''
# just make up some data and return it
ts = (time.time())
segmentId = 0 # compatible with repo-ng test: may change to test segmented data
versionStr = baseName.get(-1).toEscapedString()
dataName = Name(baseName)
dataName.appendSegment(segmentId)
d = Data(dataName)
content = "(" + str(ts) + ") Data named " + dataName.toUri()
d.setContent(content)
d.getMetaInfo().setFinalBlockID(segmentId)
d.getMetaInfo().setFreshnessPeriod(-1)
if shouldSign:
self.keychain.sign(d, self.certificateName)
else:
d.setSignature(self.fakeSignature)
stats.insertDataForVersion(versionStr, {'publish_time':time.time()})
logger.debug('Publishing: '+d.getName().toUri())
return d
示例6: onInterest
def onInterest(prefix, interest, transport, prefixID):
interestCallbackCount[0] += 1
data = Data(interest.getName())
data.setContent("SUCCESS")
self.keyChain.sign(data, self.keyChain.getDefaultCertificateName())
encodedData = data.wireEncode()
transport.send(encodedData.toBuffer())
示例7: test_copy_fields
def test_copy_fields(self):
data = Data(self.freshData.getName())
data.setContent(self.freshData.getContent())
data.setMetaInfo(self.freshData.getMetaInfo())
self.credentials.signData(data)
freshDump = dumpData(data)
self.assertTrue(dataDumpsEqual(freshDump, initialDump), 'Freshly created data does not match original dump')
示例8: onPlayingCommand
def onPlayingCommand(self, prefix, interest, transport, prefixId):
print "receive interest"
interestName = Name(interest.getName())
commandComponent = interest.getName().get(self.prefix.size())
if commandComponent == Name.Component("stop"):
pass
if commandComponent == Name.Component("play"):
pass
else:
songName = commandComponent.toEscapedString()
print songName
songList = []
songList = songName.split('%2C')
print "songlist and its len",songList,len(songList)
for i in songList:
self.song = i
fmusic = i+str("-music.mp3")
self.ftxt = i + str("-os.txt")
self.ffreq = i + str("-freq.txt")
print "FMUSIC:",fmusic
self.thread = threading.Thread(target = self.mp.play_music, args = (fmusic,songList,self.q,))
self.thread.daemon = True
g = GetFile(self._repoCommandPrefix, i, self._face, self.getOnset)
#g = GetFile(self._repoCommandPrefix, ftxt, self._face, self.lightFunction)
g.oStream = open(fmusic,'wb')
g.start()
d = Data(interest.getName())
d.setContent("start to play: " + songName + "\n")
encodedData = d.wireEncode()
transport.send(encodedData.toBuffer())
示例9: onPlayingCommand
def onPlayingCommand(self, prefix, interest, transport, prefixId):
interestName = Name(interest.getName())
commandComponent = interest.getName().get(self.prefix.size())
if commandComponent == Name.Component("stop"):
pass
if commandComponent == Name.Component("play"):
pass
else:
songName = commandComponent.toEscapedString()
songList = []
songList = songName.split('%2C')
for i in songList:
fmusic = i + str("-music.mp3")
print "started getting music file", i
g = GetFile(self._repoCommandPrefix, i, self._face, self.getOnset, i)
g.oStream = open(fmusic,'wb')
g.start()
#self._getFiles.append(g)
d = Data(interest.getName())
d.setContent("start to play: " + songName + "\n")
encodedData = d.wireEncode()
transport.send(encodedData.toBuffer())
示例10: benchmarkDecodeDataSeconds
def benchmarkDecodeDataSeconds(nIterations, useCrypto, keyType, encoding):
"""
Loop to decode a data packet nIterations times.
:param int nIterations: The number of iterations.
:param bool useCrypto: If true, verify the signature. If false, don't
verify.
:param KeyType keyType: KeyType.RSA or EC, used if useCrypto is True.
:param Blob encoding: The wire encoding to decode.
:return: The number of seconds for all iterations.
:rtype: float
"""
# Initialize the KeyChain in case useCrypto is true.
keyChain = KeyChain("pib-memory:", "tpm-memory:")
# This puts the public key in the pibImpl used by the SelfVerifyPolicyManager.
keyChain.importSafeBag(SafeBag
(Name("/testname/KEY/123"),
Blob(DEFAULT_EC_PRIVATE_KEY_DER if keyType == KeyType.EC
else DEFAULT_RSA_PRIVATE_KEY_DER, False),
Blob(DEFAULT_EC_PUBLIC_KEY_DER if keyType == KeyType.EC
else DEFAULT_RSA_PUBLIC_KEY_DER, False)))
validator = Validator(ValidationPolicyFromPib(keyChain.getPib()))
start = getNowSeconds()
for i in range(nIterations):
data = Data()
data.wireDecode(encoding)
if useCrypto:
validator.validate(data,onVerifySuccess, onVerifyFailed)
finish = getNowSeconds()
return finish - start
示例11: onInterest
def onInterest(self, prefix, interest, face, interestFilterId, filter):
"""
Append the next segment number to the prefix and send a new data packet.
If the last packet is sent, then call self._onFinished().
"""
maxSegment = 2
if self._segment >= maxSegment:
# We have already called self._onFinished().
return
dump("Got interest", interest.toUri())
# Make and sign a Data packet for the segment.
self._segment += 1
data = Data(Name(prefix).appendSegment(self._segment))
content = "Segment number " + repr(self._segment)
data.content = content
self._keyChain.sign(data, self._certificateName)
face.putData(data)
dump("Sent data packet", data.name.toUri())
if self._segment >= maxSegment:
# We sent the final data packet.
self._onFinished()
示例12: test_malformed_certificate
def test_malformed_certificate(self):
# Copy the default certificate.
malformedCertificate = Data(
self._fixture._subIdentity.getDefaultKey().getDefaultCertificate())
malformedCertificate.getMetaInfo().setType(ContentType.BLOB)
self._fixture._keyChain.sign(
malformedCertificate, SigningInfo(self._fixture._identity))
# It has the wrong content type and a missing ValidityPeriod.
try:
CertificateV2(malformedCertificate).wireEncode()
self.fail("Did not throw the expected exception")
except CertificateV2.Error:
pass
else:
self.fail("Did not throw the expected exception")
originalProcessInterest = self._fixture._face._processInterest
def processInterest(interest, onData, onTimeout, onNetworkNack):
if interest.getName().isPrefixOf(malformedCertificate.getName()):
onData(interest, malformedCertificate)
else:
originalProcessInterest.processInterest(
interest, onData, onTimeout, onNetworkNack)
self._fixture._face._processInterest = processInterest
data = Data(Name("/Security/V2/ValidatorFixture/Sub1/Sub2/Data"))
self._fixture._keyChain.sign(data, SigningInfo(self._fixture._subIdentity))
self.validateExpectFailure(data, "Signed by a malformed certificate")
self.assertEqual(1, len(self._fixture._face._sentInterests))
示例13: test_hyper_relation
def test_hyper_relation(self):
# Set up the validator.
fetcher = CertificateFetcherOffline()
validator = ValidatorConfig(fetcher)
validator.load(
os.path.join(self._policyConfigDirectory, "hyperrelation_ruleset.conf"))
# Set up a Data packet and result object.
data = Data()
KeyLocator.getFromSignature(data.getSignature()).setType(KeyLocatorType.KEYNAME)
result = TestValidationResult(data)
data.setName(Name("/SecurityTestSecRule/Basic/Longer/Data2"))
KeyLocator.getFromSignature(data.getSignature()).setKeyName(
Name("/SecurityTestSecRule/Basic/Longer/KEY/123"))
result.checkPolicy(validator)
self.assertTrue(result._calledFailure and not result.calledContinue_)
KeyLocator.getFromSignature(data.getSignature()).setKeyName(
Name("/SecurityTestSecRule/Basic/KEY/123"))
result.checkPolicy(validator)
self.assertTrue(result._calledFailure and not result.calledContinue_)
data.setName(Name("/SecurityTestSecRule/Basic/Other/Data1"))
KeyLocator.getFromSignature(data.getSignature()).setKeyName(
Name("/SecurityTestSecRule/Basic/Longer/KEY/123"))
result.checkPolicy(validator)
self.assertTrue(result._calledFailure and not result.calledContinue_)
KeyLocator.getFromSignature(data.getSignature()).setKeyName(
Name("/SecurityTestSecRule/Basic/KEY/123"))
result.checkPolicy(validator)
self.assertTrue(result._calledFailure and not result.calledContinue_)
示例14: loadApplications
def loadApplications(self, directory = None, override = False):
if not directory:
directory = self._applicationDirectory
if override:
self._applications.clear()
if os.path.exists(directory):
for f in os.listdir(directory):
fullFileName = os.path.join(directory, f)
if os.path.isfile(fullFileName) and f.endswith('.conf'):
appName = f.rstrip('.conf')
if appName in self._applications and not override:
print("loadApplications: " + appName + " already exists, do nothing for configuration file: " + fullFileName)
else:
self._applications[appName] = {"tree": BoostInfoParser(), "dataPrefix": [], "version": int(time.time())}
self._applications[appName]["tree"].read(fullFileName)
data = Data(Name(self.prefix).append(appName).append("_schema").appendVersion(self._applications[appName]["version"]))
data.setContent(str(self._applications[appName]["tree"].getRoot()))
self.signData(data)
self._memoryContentCache.add(data)
try:
validatorTree = self._applications[appName]["tree"]["validator"][0]
for rule in validatorTree["rule"]:
self._applications[appName]["dataPrefix"].append(rule["id"][0].value)
# TODO: don't swallow any general exceptions, we want to catch only KeyError (make sure) here
except Exception as e:
print("loadApplications parse configuration file " + fullFileName + " : " + str(e))
return
示例15: _onConfigurationReceived
def _onConfigurationReceived(self, prefix, interest, transport, prefixId):
# the interest we get here is signed by HMAC, let's verify it
self.tempPrefixId = prefixId # didn't get it from register because of the event loop
dataName = Name(interest.getName())
replyData = Data(dataName)
if (self._hmacHandler.verifyInterest(interest)):
# we have a match! decode the controller's name
configComponent = interest.getName().get(prefix.size())
replyData.setContent('200')
self._hmacHandler.signData(replyData, keyName=self.prefix)
transport.send(replyData.wireEncode().buf())
environmentConfig = DeviceConfigurationMessage()
ProtobufTlv.decode(environmentConfig, configComponent.getValue())
networkPrefix = self._extractNameFromField(environmentConfig.configuration.networkPrefix)
controllerName = self._extractNameFromField(environmentConfig.configuration.controllerName)
controllerName = Name(networkPrefix).append(controllerName)
self._policyManager.setEnvironmentPrefix(networkPrefix)
self._policyManager.setTrustRootIdentity(controllerName)
self.deviceSuffix = self._extractNameFromField(environmentConfig.configuration.deviceSuffix)
self._configureIdentity = Name(networkPrefix).append(self.deviceSuffix)
self._sendCertificateRequest(self._configureIdentity)