R
R
R.2017-10-05 16:44:38
Django
R., 2017-10-05 16:44:38

How to restrict access to a Python module?

The essence of the task: restrict access to modules if the domain is not in the list that these modules are available to.
How to implement?
so far the ideas are:
- (not suitable) a decorator that restricts access to each module function
- import the module with a check if the check fails - block further module execution (I don’t know how to do it and is it even possible)
- something based on hooks( link ), but have not yet figured out how it works
How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Menshov, 2017-10-05
@SWEBB

The dictionary list of connected modules in python is always stored in sys.modules
At runtime (at startup) it will always show you which modules are connected...
to limit them you can just do this

sys.modules['mymodule'] = none

    try:
        __import__('mymodule')
    except ImportError :
        sys.exit("Module disabled")

Accordingly, based on your IP restriction example, you can create a module in which you describe the connection conditions and reset them .. In the same module, the connection check that I described above will also be found

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question