0

I'm trying to dynamically import python files from a directory and then have those functions available to me somehow, such as a list. Currently I'm importing all modules from a parent folder where those modules live that contains an __init__.py file with the following

__init.py__

import os
import glob
modules = glob.glob(os.path.dirname(__file__)+"/*.py")
__all__ = [ os.path.basename(f)[:-3] for f in modules]

This works to get all the modules imported. But I'm not sure how to know what functions in the local scope were from the import and how I should access them? These modules can change and I basically want to send data to the imported mystery functions.

Thoughts on how to go about this (with Python 2.7.6)?

To expand on what I'm doing, I'm using a AFL fuzzer to fuzz an imported function. This is only going to be one file with a single function definition for this project. Here's some horrible pseudo code. Then module.func is where I need to figure out how to access whatever function just got imported.

import sys
import os
sys.path.append('..')
from binary import *

def main():
    s = sys.stdin.read()
    try:
        module.func(s)
    except:
        os._exit(0)

if __name__ == '__main__':
    import afl
    afl.start()
    main()
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Michael
  • 1,577
  • 1
  • 18
  • 33
  • You can try `dir(module)` to get a list of it's objects and then use `callable(object)` to check if it's a callable function or class. – Renae Lider Jul 24 '15 at 02:22
  • I guess the problem is the way im doing all my imports. Also, I don't know what the module name is going to be. This module and it's function could change. All I know is that it's one module and in a certain location. – Michael Jul 24 '15 at 02:24
  • @Michael l don't think it is a good idea to do what you are asking for. You should only import what you need and have control over your imported modules. In my opinion, it is preferable to have 100 lines like `from module import myFunction` than a single line importing functions from several files. – tomasyany Jul 24 '15 at 02:24
  • I agree, it's ok that it's a hack, it's for demonstration purposes and not to be used in any serious environment. – Michael Jul 24 '15 at 02:25
  • I can just not do this if it's super horrible. But was an interesting problem that I was curious if it was possible. Or if I could do my imports different to have more control around knowing what im importing. – Michael Jul 24 '15 at 02:30
  • @Michael: is there any specific directory you'd be looking for Python modules in? – d33tah Oct 15 '15 at 19:40

0 Answers0