python 模块 子模块_尝试在python中打印模块内的每个子模块

sorry if I got some facts wrong during this but I have only been programming for about a year, since I started a computing course at my school, and sorry if this question has been answered before I have looked everywhere but I don't know what to search for. Recently I found a game where you can use an injector to inject python code into it, but the only way of finding the codes is by using:

import modulename

print dir(modulename)

and then

print dir(modulename.submodulename)

Which would print all the submodules inside that module to the log file of the game. Using this technique I have developed a script which when executed will find all of the sub modules in that directory and write them to a file.

import modulename

myfile=open("name.txt","w")

myfile.write("modulename\n")

for a in dir(modulename):

a="modulename"+"."+a

for b in dir(a):

b=a+"."+b

for c in dir(b):

c=b+"."+c

if ".__" in c:

pass

elif "._" in c:

pass

else:

myfile.write(c+"\n")

if ".__" in b:

pass

if "._" in b:

pass

else:

myfile.write(b+"\n")

if ".__" in a:

pass

if "._" in a:

pass

else:

myfile.write(a+"\n")

print "Done"

myfile.close()

Unfortunately when run past "something.something" the modules are non existent. Here is a sample for the module "Random" where "random.weibullvariate" is a real module but anything past the second "." is not in the random submodules.

random.weibullvariate.title.translate

random.weibullvariate.title.upper

random.weibullvariate.title.zfill

random.weibullvariate.title

random.weibullvariate.translate.capitalize

random.weibullvariate.translate.center

random.weibullvariate.translate.count

random.weibullvariate.translate.decode

random.weibullvariate.translate.encode

random.weibullvariate.translate.endswith

random.weibullvariate.translate.expandtabs

random.weibullvariate.translate.find

random.weibullvariate.translate.format

random.weibullvariate.translate.index

random.weibullvariate.translate.isalnum

random.weibullvariate.translate.isalpha

random.weibullvariate.translate.isdigit

random.weibullvariate.translate.islower

random.weibullvariate.translate.isspace

random.weibullvariate.translate.istitle

random.weibullvariate.translate.isupper

random.weibullvariate.translate.join

random.weibullvariate.translate.ljust

random.weibullvariate.translate.lower

random.weibullvariate.translate.lstrip

random.weibullvariate.translate.partition

random.weibullvariate.translate.replace

random.weibullvariate.translate.rfind

random.weibullvariate.translate.rindex

random.weibullvariate.translate.rjust

random.weibullvariate.translate.rpartition

random.weibullvariate.translate.rsplit

random.weibullvariate.translate.rstrip

random.weibullvariate.translate.split

random.weibullvariate.translate.splitlines

random.weibullvariate.translate.startswith

random.weibullvariate.translate

As you can see there are submodules being put in that don't exist in "random".

I ended up working out what the problem is but I am not experienced enough to solve the problem.

The problem is that using the first 2 lines as an example

for a in dir(modulename):

a="module name"+"."+a

if I did

a

"modulename.submodule"

As you can see "a" is a string, and if I then put "a" into a "dir()" then the same thing would be returned no matter what the submodules name was.

I need to find a way to add the submodule onto the previous modules name with a "." in between without turning it into a string.

Sorry for the long post, anyone have any ideas?

解决方案import types

def list_modules(module_name):

try:

module = __import__(module_name, globals(), locals(), [module_name.split('.')[-1]])

except ImportError:

return

print(module_name)

for name in dir(module):

if type(getattr(module, name)) == types.ModuleType:

list_modules('.'.join([module_name, name]))

Can't claim this will work for all cases, but worth a try?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值