Setting up Python Flask server on internet via Port forwarding

In this post we will cover topics;

  • Starting a server using Python Flask.
  • Accessing server in Local Network.
  • Adding a Port Forwarding rule in NAT settings.
  • Accessing server in Internet via public IP.
  • Difference between static IP and dynamic IP.


Step 1 :-

As the first step, we need to setup a server on our system.A server exposes your code to a particular port.Some examples are Apache Tomcat, Spring boot (inbuilt), Python Django (inbuilt), Nginx.
Here we have a simple server in Python Flask.


For a GET request in root path the server returns a "Hello World!".That's it.
Going to start the server on my local IP address(192.168.1.105) at default flask port(5000) using below commands,
$export FLASK_APP=flaskTest.py
$flask run  --host=192.168.1.105



Step 2 :-

For security purpose all ports for external communication is closed by router's firewall by default. So no communication can be done to outside world. For same reason our server won't be available to other devices.
For our purpose we need to add a new rule on out router administrator page.We can get to there by URL -192.168.1.1
Default username and password is "admin".
My router's made is iBall-Baton, so will be following its configuration.
Goto advanced settings -> NAT -> Virtual server.
Add a name to application,your local IP and port.Press save.
You can see a new configuration is added there.



Step 3 :-

If the service is up and running, we should be able to do GET operation,ie
URL - 192.168.1.105:5000/ will display "Hello World!" in browser.


Now, you can access this URL from any device under the local network.For example,through a mobile connected to the same network.

But,our aim is to access from anywhere through internet.
For that, we need to do this using public IP. URL should become mypubicIP:5000/
To know your public IP use this site - here



Mostly you will have a dynamic IP.This one is allotted to you when your router connects to internet.This temporary IP changes when you reboot router or device.
For a server it is best to have a static IP.You may need to pay some bucks to your ISP to get static IP.This IP is allotted only for you.

If you are facing any issue please put it as comments.

Thank you
Video Tutorial :-


Follow Us,
Youtube - https://goo.gl/Xvmj1w
Google PLus - https://goo.gl/8S8mrp
Facebook - https://goo.gl/FX61Ye
Twitter - https://goo.gl/nDZCeN
Instagram - https://goo.gl/rwuuYi
Website - https://goo.gl/bCBmSE

#TheBeArsenal #python #pythonflask #portforwarding #dynamicip #staticip #iballbaton

Comments

Post a Comment

Popular posts from this blog

Unsupervised Learning

Automate Blog Post creation using Blogger APIs and Python

The beginning of Data Quest

Setting up Jupyter Lab integrated with Python, Julia, R on Windows Subsystem for Linux (Ubuntu)