Search code examples
python-3.xurlhandlerweb-deployment

Reading url arguments to run a python script


I have a python file lets say myfile.py and I want to run it by a url .lets say www.abc.com/argument. This argument needs to be read within the python script. For eg. if the argument is www.abc.com/boy, then boy is my argument and my python code should be able to read this argument and then use it as a input parameter to run my code futher. How to achieve it?.


Solution

  • You could look at one of python's web development platforms, such as flask or django. With them you could write web server that will run a piece of code every tome a url is called.

    For example:

    from flask import Flask
    app = Flask(__name__)
    
    @app.route("/abc")
    def hello():
        print("The code here will run each time /abc is called")
        return "Hello World!"
    
    app.run()
    

    If you want to run a particular file each time the url is called, you could wrap the code in this file into a function, then use it here..