How to host a flask web app on my own pc? - python

Im developing an app using flask framework in python, i wanted to host it on my pc for a few people to be able to visit it, similar to wamps put online feature but for flask instead, i dont want to deploy it to the cloud just yet. how can i do it.

Enable port forwarding on your router, start flask on the 0.0.0.0 address of your computer, set the forwarded port to be the one started on your laptop. This will now allow your LAN and calls to your ISP provided address to be directed to your laptop.
To clarify, LAN can do it without port forwarding in my experience.

Related

Make python Flask application accessible from the Internet with gunicorn

what I have:
I made a simple web application using Flask, which woks great on the localhost.
what I want to achieve:
I want to deploy it so it is visible in the internet.
Because Flask says that the production server should not be used for deployment I installed gunicorn.
I managed to get it working on localhost (running gunicorn server:app).
Then I tried to make it public using the -b 0.0.0.0:5000 option. it said it is running on 192.168....:5000 Now I could access the website using that ip address from my computer and also from my phone connected to the same wifi. however I was unable to connect to the website when I didn't use this wifi.
when I searched about this, I found out that the 192.168.... ip address range is reserved to the local network and cannot be accessed from anywhere else.
how do I need to run the script so it is accessible from everywhere? Do I need to modify the firewall settings? Maybe there is a better way to deploy such an app on the local machine using a different framework?
Just some additional information:
python version: 3.9
I am using a venv (and installed flask and gunicorn into it)
os: macos 11.6.1 (I could run it on an Ubuntu machine if that is easier)
This question isn't anything related with Python or Gunicorn but to networking (so maybe StackOverflow isn't the correct place to ask but other community like ServerFault)
Currently your application is already exposed on your machine in the defined port, so next step would be to forward all the traffic that comes to your router to there.
In order to do that, you will have to configure the firewall of your router to accept incoming traffic through a desired port and finally forward the traffic which comes from that port to your machine in the port 5000.
Also, in the case that you have everything already configured, it will only work if your ISP is providing you a single IPv4, what currently doesn't happen anymore but you actually are sharing that with few more people. Other option would be that you configure IPv6 incoming traffic.
As you can see, this isn't a simple task neither one that should be done without proper care, since you would be literally opening your network to possible attackers.
So, in order to simplify it at the most for you, since you already have Gunicorn, I would recommend you to use any of the resources exposed by other users as Heroku or Netlify which are free for a single application and will fulfill your expectations without requiring high amount of networking knowledge.
You would need to do the following steps:
read up and learn a lot about security for Web servers, then read some more, it is fraught with risks
find your Mac's IP address on your local network and make sure it is fixed, i.e. set as static on your Mac (under "System Preferences->Network") or reserved in your router's DHCP tables (by putting your Mac's MAC address in its allocation tables) so that it always gets the same local address on your internal network when it boots
log into your router and set up "Port Forwarding" to forward external requests (coming from the Internet) for port 5000 (or some other port) to your Mac's fixed IP address and the port 5000 where Gunicorn is serving
log into your router and find your WAN IP address, or go to http://whatsmyip.com to get the address you need to put in your browser, or your friends need to put in their browsers to see your shiny new website
as it stands, this will work until the next time your router reboots when your ISP will likely allocate it a new IP address. If you want it permanent, you need to either 1) ask your ISP for a static IP address, or 2) subscribe a DDNS service (e.g. noip.com or dyndns.com) or 3) tell your friends your new IP address every time you reboot your router
I do not know anything about gunicorn. But what I used to use when needing to deploy a flask app was pythonanywhere. They have a great and totally free hosting service. It's really fast in deploying, needs no resources from your computer and is just great. Also you would have to forward the port 5000 for your computer to enable other devices not in the same network to access your computer/flask app. That comes with security issues. However, you do not need any of that when using pythonanywhere.
There are also other great hosting services like that one. It's just the only one I know and used yet but you'll find others for sure if you don't like that one.

How to browse IIS deployed flask rest api on windows server 2012 r2 from Internet

I deployed my flask rest api on IIS 8 to windows server 2012 r2. I followed this article. (https://medium.com/#bilalbayasut/deploying-python-web-app-flask-in-windows-server-iis-using-fastcgi-6c1873ae0ad8) I can browse the rest api on the local server but I can not browse the api from other computers on public internet. I can ping the public IP of the server without problem. There is no firewall rule that blocks the server. Would you please help me?
PS. I am trying to browse by the IP, 185.201.212.219. Is it related to this?
edit1: I will try this which is from flask documentation.
Externally Visible Server
If you run the server you will notice that the server is only accessible from your own computer, not from any other in the network. This is the default because in debugging mode a user of the application can execute arbitrary Python code on your computer.
If you have the debugger disabled or trust the users on your network, you can make the server publicly available simply by adding --host=0.0.0.0 to the command line:
flask run --host=0.0.0.0
This tells your operating system to listen on all public IPs.
edit2: Adding host parameter didn't work.
Is there any ideas?
Thanks in advance.
All ports except 80 was open. When I set port=80, all my troubles seem so far away :)

How to run localhost on a port on a website with Flask

so I'm trying to host my flask app on a site only when it's accessed via a certain port for example. If a user goes to www.example.com on port 80 the site acts normal and they connect to the site. However if they visit www.example.com on port 5000 they are greeted with my flask app, how can I achieve this? Side note as people are getting confused, I don't own www.example.com I just want it to display what's running on localhost (flask) when it's accessed on a certain port.
You can run both the applications at the same time. Since Flask and the webserver run on different ports, they can co-exist without any issue.
However, you need to make sure clients have access to both port 80 (usually this is allowed from most firewalls and access restricting methods) and 5000 from client devices.

Django - Host a website on a Lan Network

I've been working on a website for the past month, and now its time for me to host the django website onto a private server. Is there a detailed method on how I'm supposed to host the website onto a specific IP address assigned to my system?
This is my first time dealing with hosting a server and i dont know much about it.
Also, once i host it, How do i change the link from being the IP address to an actual link like "12345678.com" or something like that?
(The website should be hosted in such a way that only the people who are in the same network/lan connection should have access to the website.)
I have wamp on the system and the system has a Windows Server OS (if this info helps)
you can run your codes with manage command like this
python manage.py runserver 0.0.0.0:8000
but its strongly recommended to run your code with a web server like nginx or apache read this tutorial to run your site on a linux server
https://www.digitalocean.com/community/tutorials/how-to-set-up-uwsgi-and-nginx-to-serve-python-apps-on-ubuntu-14-04
on windows you can use IIS but its not recommended. its performance is not good
to use 1234567890.com instead of using server ip address you must setup a DNS server on your local network. in linux you can use bind and windows server have its own DNS server. but in your DHCP configuration you must set this DNS server as clients DNS.

Accessing Python webserver remotely on Amazon EC2

I work on a project in which I need a python web server. This project is hosted on Amazon EC2 (ubuntu).
I have made two unsuccessful attempts so far:
run python -m SimpleHTTPServer 8080. It works if I launch a browser on the EC2 instance and head to localhost:8080 or <ec2-public-IP>:8080. However I can't access the server from a browser on a remote machine (using <ec2-public-IP>:8080).
create a python class which allows me to specify both the IP address and port to serve files. Same problem as 1.
There are several questions on SO concerning Python web server on EC2, but none seems to answer my question: what should I do in order to access the python web server remotely ?
One more point: I don't want to use a Python web framework (Django or anything else): I'll use the web server to build a kind of REST API, not for serving HTML content.
you should open the 8080 port and ip limitation in security croups, such as:
All TCP TCP 0 - 65535 0.0.0.0/0
the last item means this server will accept every request from any ip and port, you also
You passble need to IAM in AWS.
Aws set security permission that need to open up port so you only localhost links your webservice
aws link

Categories

Resources