2010-05-03 43 views
0
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終端上運行一個類似的代碼時,沒有這樣的錯誤,但是這個代碼彈出一個錯誤。爲什麼這樣?str在Python中不可調用錯誤

回答

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) 
2

不要將名稱str用於變量。這是Python中的一個類型的名稱。

+0

您是否認爲只更改變量str的名稱將會調試代碼? – Hick 2010-05-03 19:12:47

+0

是的,你用''str'作爲變量來打破它。使用不同的名稱。 – 2010-05-03 19:19:53

相關問題