Python:通过名称在字符串中访问结构字段(Python: access structure field through its name in a string)
在Scapy中,我想比较任意两个数据包a和b之间的多个头字段。 这个字段列表是预定义的,例如:
fieldsToCompare = ['tos', 'id', 'len', 'proto'] #IP header通常我会单独做:
if a[IP].tos == b[IP].tos: ... do stuff...有没有办法从字符串列表中访问这些数据包字段,包括每个字符串被称为什么? 喜欢:
for field in fieldsToCompare: if a[IP].field == b[IP].field: ... do stuff...In Scapy, I want to compare a number of header fields between any two packets a and b. This list of fields is predefined, say:
fieldsToCompare = ['tos', 'id', 'len', 'proto'] #IP headerNormally I would do it individually:
if a[IP].tos == b[IP].tos: ... do stuff...Is there any way to access those packet fields from a list of strings including what each one of them is called? Like:
for field in fieldsToCompare: if a[IP].field == b[IP].field: ... do stuff...最满意答案
你可以使用getattr() 。 这些线是相同的:
getattr(x, 'foobar') x.foobarsetattr()是它的对手。
You can use getattr(). These lines are equivalent:
getattr(x, 'foobar') x.foobarsetattr() is its counterpart.
更多推荐
发布评论