How to run a Python program over the internet?
What am I trying to solve?
I want to run a python program whenever I need and get some information out of it. The problem is, I want to call that through internet. And I want to show the content on a web page or some UI container. That's it. Let put this as a requirement.
Problem: Display some information on my web page calculated by a python program.
- How do we do it on our local computer? We run our program as : python my_program.py .
- So may be I can do something like: www.mywebsite.com/my_program.py . Can we? No idea.
I expect to collect the response in some variable (no idea how can I create a variable in the first place on html) and then display the contents of this variable . Displaying a simple text on html is known , so that should be easy.
You must remember that we want some information from our program . It's a basic GET call where you just ask for some information from a server. You may or may not pass some inputs while doing this.
So what's going on here? This is not the simple hello_world program as promised. Well it almost is.
- We create an app from class Flask and an api object from Api class for the app created.
- Since we are want to write a GET api, we define a "get" function inside our Hello class that inherits from Resource.
- And at last, we register our function with a url.
Save the above code in a file and run this. This is what you will see.
Now visit the link for e.g : http://127.0.0.1:5000/hello/YourName
DeploymentNow we need to place our running SERVER someplace which is accessible 24/7. We obviously cannot use our server running on local PC. I am going to use pythonanywhere for this . As they advertise,
Host, run, and code Python in the cloud!
So we are going to use it as a host. We are going to open a console, and check if we have everything that we need. Generally , the environment already has all our favorite modules and tools installed.
This is the Console tab. Click on "Bash" to launch a new bash console. Then launch python . Try to import the modules that we have used above.
If you can see this, you are ready to go. Else:
pip install flask
pip install flask_restful
Now go back by clicking on "Dashboard". Click on the tab "Files". It's probably empty right now. Create a new directory by providing a name and clicking on 'New Directory'. Use the " Upload a file " to add the above file created. (Or you can checkout from git)
- Select Web domain name (example "xyz")
- "Flask" as python web framework
- Python 2.7
- Put the path as /home/xyz/mysite/flask_app.py
After above wizard is finished, you will see the config param of your website. Change it like shown in the pic below.
The section under "Code: " should be like above. Now click on the WSGI python file and edit it like shown below:
Save it . Now go back the "Web" tab from the Dashboard and run you website. "Run until 3 months" button.
- Now launch browser.
- Visit the page , "www.xyz.pythonanywhere.com/hello/YourText"
|This is the same code on my website.|
Now you can use any module to call this API from your scripts and you will get this response . Now you are ready to create more complicated response.
Problem 2: We want to know, what people used in the above hello url while trying it out.. So design a way to store this info and display the same .
Lets prepare a list to steps to do that.
- Setup Database in pythonanywhere.com (Setup usr, pwd for the DB).
- Install 'flask_sqlalchemy' using pip.
- Create database and tables inside it.
- In our api_script.py file, add config for connecting to database. Refer this.
After you setup your database, you will be seeing some details under your 'Database' tab.
The api_script.py file should be modified like shown below.
Now that our code is ready , let's do some finishing touches. pythonanywhere works with the help of a WSGI file. Refer the picture below to find the WSGI file.
|WSGI link on WEB Tag in Pythonanywhere.com|
Click on the file to edit it. Edit it as following.
We are done. Reload the web site from the WEB tab and visit the url