0

Here is my code for a server that allows remote importing of python3 libraries.

from flask import Flask
app = Flask(__name__)
@app.route("/import/<lib>",methods=['GET'])
def remote_import(lib):
    try:
        exec("import %s as m" % str(lib))
        fn = m.__file__
        with open(fn,"r") as f:
            return f.read(), 200
    except Exception as e:
        print(e)
        return "False", 404
app.run(host="127.0.0.1",port=5000)

It compiles and runs, but when testing (curl http://localhost:5000/import/os) I get "undefined variable 'm'".

Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • 1
    Also [Dynamic module import in Python](//stackoverflow.com/q/301134) – Aran-Fey Aug 03 '18 at 10:00
  • You should look up "importlib" in the docs. I would say never use exec, and most definately not when you have little/no control over its input. – efr4k Aug 03 '18 at 11:35

0 Answers0