import sys
import md5
from TOSSIM import *
from RadioCountMsg import *
t = Tossim([]) #The Tossim object is defined here
m = t.mac()#The mac layer is defined here , in which the communication takes place
r = t.radio()#The radio communication link object is defined here , as the communication needs Rf frequency to transfer
t.addChannel("RadioCountToLedsC", sys.stdout)# The various channels through which communication will take place
t.addChannel("LedsC", sys.stdout)
#The no of nodes that would be required in the simulation has to be entered here
print("enter the no of nodes you want ")
n=input()
for i in range(0, n):
m = t.getNode(i)
m.bootAtTime((31 + t.ticksPerSecond() / 10) * i + 1)
#The booting time is defined so that the time at which the node would be booted is given
f = open("topo.txt", "r") #The topography is defined in topo.txt so that the RF frequencies of the transmission between nodes are are set
lines = f.readlines()
for line in lines:
s = line.split()
if (len(s) > 0):
if (s[0] == "gain"):
r.add(int(s[1]), int(s[2]), float(s[3])) #The topogrography is added to the radio object
noise = open("meyer-heavy.txt", "r") #The noise model is defined for the nodes
lines = noise.readlines()
for line in lines:
str = line.strip()
if (str != ""):
val = int(str)
for i in range(0, 4):
t.getNode(i).addNoiseTraceReading(val)
for i in range (0, n):
t.getNode(i).createNoiseModel() #The noise model is created for each node
for i in range(0,n):
t.runNextEvent()
fk=open("key.txt","w")
for i in range(0,n):
if i ==0 :
key=raw_input()
fk.write(key)
ak=key
key=md5.new()
key.update(str(ak))
ak=key.digest()
fk.write(ak)
fk.close()
fk=open("key.txt","w")
plaint=open("pt.txt")
for i in range(0,n):
msg = RadioCountMsg()
msg.set_counter(7)
pkt = t.newPacket()#A packet is defined according to a certain format
print("enter message to be transported")
ms=raw_input()#The message to be transported is taken as input
#The RC5 encryption has to be done here
plaint.write(ms)
pkt.setData(msg.data)
pkt.setType(msg.get_amType())
pkt.setDestination(i+1)#The destination to which the packet will be sent is set
print "Delivering " + " to" ,i+1
pkt.deliver(i+1, t.time() + 3)
fk.close()
print "the key to be displayed"
ki=raw_input()
fk=open("key.txt")
for i in range(0,n):
if i==ki:
ms=fk.readline()
for i in range(0,n):
msg=RadioCountMsg()
msg.set_counter(7)
pkt=t.newPacket()
msg.data=ms
pkt.setData(msg.data)
pkt.setType(msg.get_amType())
pkt.setDestination(i+1)
pkt.deliver(i+1,t.time()+3)
#The key has to be broadcasted here so that the decryption can take place
for i in range(0, n):
t.runNextEvent();
这段代码在这里给我错误key.update(str(ak))。当我在python终端上运行类似的代码没有这样的错误,但这个代码弹出一个错误。为什么呢?
答案 0 :(得分:3)
在第35行,你从内置它重新分配'str'它最初引用另一个对象。然后,在第53行,您尝试再次使用它作为原始内置。如果你想在第53行使用'str'作为'str()',你需要在第35行(以及36和37)上使用不同的变量名称
不要在这里使用'str':
for line in lines:
str = line.strip()
if (str != ""):
for i in range(0, 4):
t.getNode(i).addNoiseTraceReading(val)
答案 1 :(得分:2)
不要将名称str
用于变量。它是Python中类型的名称。