简体   繁体   English

Python for循环查询

[英]Python for-loop query

I am checking a piece of Python code I found online ( http://www.exploit-db.com/exploits/18305/ ), and I'm stuck trying to understand a seemingly simple for loop. 我正在检查我在网上找到的一段Python代码( http://www.exploit-db.com/exploits/18305/ ),并且我一直试图了解一个看似简单的for循环。

To be honest I don't know Python, but I have experience in other programming languages. 老实说我不懂Python,但是我有其他编程语言的经验。

def _generatePayload(self, collisionchars, payloadlength):
    result = ""

    for item in collisionchars:
        result = result.replace(str(item), collisionchars[item])

    return result;

I have modified the code slightly to remove the parts that are irrelevant to the question, but I have retained the same method definition as found in the original source. 我已经稍微修改了代码以删除与该问题无关的部分,但是我保留了与原始源代码中相同的方法定义。 As I understand this, it is replacing a string (within a char array / string) by another string which has the same value?? 据我了解,它正在用具有相同值的另一个字符串替换一个字符串(在char数组/字符串内)?

Thanks a bunch guys! 谢谢一群人!

collisionchars seems to be a dict (note the indexer). collisionchars似乎是字典(请注意索引器)。

What the code does is loop through all keys of collisionchars , and replace each occurence of that key in result with the value of that key. 该代码的作用是循环遍历所有collisionchars字符键,并用该键的值替换该键中每次出现的result

So, if collisionchars = {'a': 'X', 'o': '_'} and result = 'FooBar' , the code would alter result to F__BXr (replacing each a with X and each o with _ ). 因此,如果collisionchars = {'a': 'X', 'o': '_'} result = 'FooBar' collisionchars = {'a': 'X', 'o': '_'}并且result = 'FooBar' ,则代码会将result更改为F__BXr (将每个a替换a X ,将每个o替换为_ )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM