I'm trying to use this library : http://pastebin.com/xgPXpGtw (an example of use: http://pastebin.com/fNFAW3Fh) I have some issues since I dont want to split in an array all the byte as he does.
My test script looks like this:
import random
from random import *
def onerand(packet):
pack = packet[:]
byte = str(chr(choice(range(256))))
pack[choice(range(len(packet)))]= byte
print "fuzzing rand byte:%s\n" % (byte.encode("hex"))
return pack
test = "\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63\x63"
while True:
print onerand(test)
And actually returns :
Traceba开发者_C百科ck (most recent call last):
File "test.py", line 14, in <module>
print onerand(test)
File "test.py", line 7, in onerand
pack[choice(range(len(packet)))]= byte
TypeError: 'str' object does not support item assignment
So what should i do to be able to use that function on the test parameters ?
Thanks !
In Python, strings are immutable. You pass to function onerand
a string, argument name packet
, copy it giving a local name pack
(still a string, still therefore immutable), then you try to do
pack[whatever] = byte
the index doesn't matter: you're trying to modify the immutable string. That's what the error message is telling you, as clearly as possible it seems to me: you can't do that.
I dont want to split in an array all the byte
Well you surely can't use a string, if you need to assign some of them. What do you have against arrays, anyway? import array
, use pack = array.array('c', packet)
instead of pack = packet[:]
, and live happily ever after -- an array.array
is very compact and speedy, and mutable too!
Edit: you could do it with a list, as in the accepted answer, but that's only at a truly steep relative cost in performance. Consider, for example:
$ py26 -mtimeit -s's="".join([str(x)[0] for x in range(99)]); import array
> ' 'a=array.array("c",s); a[23]="b"; b=a.tostring()'
1000000 loops, best of 3: 1.09 usec per loop
$ py26 -mtimeit -s's="".join([str(x)[0] for x in range(99)]); import array
> ' 'a=list(s); a[23]="b"; b="".join(a)'
100000 loops, best of 3: 7.68 usec per loop
A list
is a much more general structure than the array.array
you really need here, whence the more-than-seven-times slowdown in choosing the wrong data structure. (It's less terrible in Python 2.7, "only" a 4-times-plus slowdown -- but, think how much it would cost you to buy a machine four times faster than your current one, and maybe you'll agree that even speeding things up by "just" 4+ times, instead of 7+ times, is still a well-worthwhile byproduct;-).
instead of pack = packet[:]
, use pack = list(packet)
, and then return ''.join(pack)
at the end.
you can't replace a single byte of a string, but you can convert it to a list of characters, replace one item, and then convert back.
精彩评论