Connect a Flask webservice from a device which is not on the same network

2024/11/18 5:29:17

I am not an expert in web programming and know very little about it. I am trying to run a webservice on an EC2 instance (Windows Server 2012R2) and the webservice is written in Python using Flask package. I am able to connect to my webservice from localhost. But what I want to do is to connect it from a remote system which is NOT on the same network.

I learned from this link, that changing the line to app.run(host='0.0.0.0') will let all devices on SAME network to connect to my webservice. But like I said, I want to connect from a system which is NOT on the same network. How can I do that in Flask? Also I have not installed Apache. Is that necessary to run a webservice?

Any suggestions would be really helpful. Thanks a lot.

Answer

Let A be the server you connecting to. Let B be the server you are connecting from, which, from what I gather, is sometimes localhost.

Why can't B connect to A?

1. Security Settings

The following is EC2-specific: You have to open up connections to specific IPs. Check your instance's security settings. If you added a security group to your EC2 instance, add your IP or the IP of the server to it. Otherwise, make sure to whitelist the IP for B.

2. Port

I'm not entirely sure this is what you want, but if everyone should be able to access A, you should run the Flask app on port 8000. If only B (and possibly your localhost) should be able to access A, then ignore this paragraph...

0.0.0.0 allows devices on the SAME network to connect

Automagically, yes. Incidentally, this is the IP you should be pointing to, for other servers to connect too. So in your case, point to 0.0.0.0 too.

Is Apache necessary?

No, Apache is not necessary. You can persist a Flask development server if you want. (a.k.a. You can use python run.py and just leave that process running for a "web service") In this case specifically, you don't need Apache to make this work. In the long-term, you will want Apache or Nginx to run your webservice however.

Here's a tutorial you can use to get Apache2 setup with Python. Just ignore the MySQL parts: https://www.digitalocean.com/community/tutorials/how-to-set-up-an-apache-mysql-and-python-lamp-server-without-frameworks-on-ubuntu-14-04

https://en.xdnf.cn/q/120111.html

Related Q&A

why int object is not iterable while str is into python [duplicate]

This question already has answers here:Why is int" not iterable in Python, but str are?(4 answers)Closed 2 years ago.As i know we can not iterate int value while we can iterate strings in python.…

an irregular anomaly in python tuple

i create two identical tuples and use is operator on them the answer that should come is false but when i use it in vscode/atom/notepadd++ it comes true but when i use the same code in pthon run throug…

AttributeError: type object Employee has no attribute Worker

Taking a class on Python coding and trying to use inheritance to code an answer to this problem: Write an Employee class that keeps data attributes for the following piece of information: Employee name…

How to add tag for numbers which in brackets using python regex?

The default strings is:strings123[abc123def456]strings456Add tag for number:strings[abc<span>123</span>def<span>456</span>]strings

How to write program run matrix as below in python?

Thanks for everyones reply. I will explain here. Suppose there is a given matrixx y B = [5,-4,5,-6] [[0,0,0,0], [[0,1,0,1],[0,0,0,0], [0,0,0,0],[0,0,0,0],…

Scraping Dynamic Information

I recently started with coding, I use Python and Pycharm. I Installed and imported the needed "Add-ons" like Selenium. For my first project I tried to get the "address" information …

Combining three RGB images into a single RGB image

I have three RGB images, but each one has only 1 non-zero channel (ie. one has a red channel with 0s in the blue and green channels) and I want to combine them into a single RGB image with the correct …

Parse XML to Table in Python

Im trying to parse XML to table-like structure in Python. Imagine XML like this:<?xml version="1.0" encoding="UTF-8"?> <base><element1>element 1</element1>…

Use Artificial Intelligence to predict next number (n+1) in a sequence

The AI must predict the next number in a given sequence of incremental integers using Python, but so far I havent gotten the intended result. I tried changing the learning rate and iterations but so fa…

Calculating size folder python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 8…