0

Hello guys so I wanted to be able to host an API on a raspberry and acces it from computers that are on another networks.

this is my simple code that's just for test as I need only to be able to acces it remote with the public ip

import flask
from flask import request

app = flask.Flask(__name__)



@app.route('/', methods=['GET'])
def home():
    return '123'

app.run(host='0.0.0.0', port=3138)

So I have created a port forward in my router option on the port 3138 linked with the static internal ip of the raspberry and I tried to acces it remotely like this: <public_ip>:3138/ it should show "123" but it shows nothing, it won't even load, do you have any ideeas how to be able to acces it in this way ?

Adrian
  • 178
  • 1
  • 1
  • 13

2 Answers2

1

Can you do some tests:

  • Have you tried to access it from your local network first (to make sure the port is open)?

  • Can also try run netcat on the raspberry(to exclude your program is not working) : "nc -l 3138" Then access the port from your mobile phone (should not be on connected to your network)

  • Setup your PC to use same ip and disconnect raspberry (to make sure port is open)

  • Check that you have a public IP so its not a Carrier-grade NAT (https://en.wikipedia.org/wiki/Carrier-grade_NAT). Check if your ip starts with 10.x.x.x, 172.16.x.x or 192.168.x.x (This can be indication its CGNAT)

davidism
  • 121,510
  • 29
  • 395
  • 339
olle.holm
  • 233
  • 1
  • 4
  • 11
  • from local network it works, I have tried netcat and it won't let me connect from my mobile phone but it sucesfully get the request from local network – Adrian Sep 04 '20 at 18:59
  • Then it must be something with router. Have you checked so you have a public IP. Many networks has started to use Carrier-grade NAT(https://en.wikipedia.org/wiki/Carrier-grade_NAT). Then portforwarding won´t work since you are still in the network providers local network. – olle.holm Sep 04 '20 at 19:23
  • mine starts with 82 so it’s not a cgnat – Adrian Sep 04 '20 at 20:29
  • What router do you have? Have you open other ports before? It must be something wrong with the portforwarding, since you can access it from your internal network. – olle.holm Sep 04 '20 at 20:34
  • AX20, yes, I have used port forward before. I’ll restart the router, once I got an IP that didn’t supported port forwarding – Adrian Sep 04 '20 at 20:36
0

It's not advisable to you the flask development server in production. I'll advise you use on of the WSGI suited for production(you may use waitress):

1. pip install waitress
2. create a file server.py (or whatever name suites you)
#content of server.py
from waitress import serve
import main #import flask app main file
serve(main.app, host='0.0.0.0', port=8080)
3. run server.py
4. access you app via:
http://<public_ip>:8080
Seyi Daniel
  • 2,259
  • 2
  • 8
  • 18
  • I’ll take a look. I don’t need to process a lot of data. Just to send each hour one post request – Adrian Sep 04 '20 at 18:46