How can I host a flask api on local web server? [duplicate] - python

This question already has answers here:
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 3 years ago.
I have made an flask API which would take input directory as input parameter by get http request. It is working fine on port 5000 on the local machine.
Now I want that working api to run on a web server so that I can use it over internet. I tried apache 2 server to do the work but it wasn't successful.
# API
app = Flask(__name__)
#app.route('/')
def home():
return "HOME PAGE LOADED"
#app.route('/runDocumentManager', methods=["GET"])
def runDocumentManager():
# pdf path
file = request.args.get('input_path')
.
.
.
if __name__ == '__main__':
app.run()

Once you start your website it will be hosted on localhost:5000
So you want to make it host on your network first. Do so with:
app.run(host="0.0.0.0")
This will make your app run on your local network.
Then you will need to portforward it on your router. This is so that if someone types in your ip then it will send them to your router.
There you want your router to route it to your machine's server.
So if someone connects to (Your IP):5000 then it will place them on (192.168.0.X):5000 (aka. The server python webserver you are running). I highly recomend googling on how to portforward as it differs slightly on different routers
Usualy you can find it at either:
https://192.168.0.1
https://192.168.1.1
https://192.168.2.1
You might be required to type in the password and username which usualy is "admin" for both but may be different depending on your ISP. You can check that online as well.

Related

Flask Rest-Api App running fine in localhost but not with public ip

Have my proper greetings at the very beginning.
I have recently built a simple rest-api app using python flask and flask_restful that takes an image as an input through POST request(using "curl" command), applies ocr on the image, saves the output of the ocr operation in a text file and then sends back that text file as the response (of the POST request).
The app works quite well in localhost.
I then set my ipv4 address manually to a particular ip address on my ubuntu pc. After that i forwarded the port my app is listening to(port: 5000) in my company's mikrotik router corresponding to my ip address. But the app does not work when the same POST request is made with the public ip address.By the way, the public ip is a real-ip.
I then changed my "app.run(port = 5000, debug = True)" statement to 'app.run(host = "0.0.0.0", port = 5000, debug = True)'. Still the app does not work for the POST request made with the public ip address
How to solve the problem? Please feel free to ask me any question if further information is required.
Thanks in advance.

Recently used an old script using Ngrok but now it doesnt work

Basically I just booted up an older script that I made 1-2 months ago and which worked perfectly at the time, but now when I try to connect to the ngrok tunnel i get a 404 and a "Tunnel xxxx not found"
I have a non-paying user, which didn't use to be a problem and I cant see anywhere that they changed their terms of service and I can find the tunnel on the dashboard but not connect, any idea what is wrong and if they changed something?
I just booted an old super simple test script which also doesn't work, I can see the tunnel but it returns "Funnel xxxx not found" and server 404:
from flask import Flask
from flask_ngrok import run_with_ngrok
app = Flask(__name__)
run_with_ngrok(app) #starts ngrok when the app is run
#app.route("/")
def home():
return "<h1>Running Flask on Google Colab!</h1>"
app.run()

Get the URL of a server

I am using flask and flask email for sending an email. When I work on I used the localhost as a base url. I deployed it on the server and email sent is still showing with localhost address.
For example
base_url = "http://localhost:0.0.0.6465"
url=base_url+'/login'
I sent an email(i am using flask-mail) with the url and I can login with the url.
With the same script when I deployed on the server I am getting with same localhost address.I need the server URL should be the base url.
To debug this I tried
url=request.base_url+'/login'
I am getting 404 error in the browser if I use this. I dont want to change the initializtion of base_url because I have to use both in the local as well as in the server.
How can I do that?
You can get the URL to the currently running app through request.host_url. But what you really want to to do to get an external URL to a specific part of your application, is to use url_for as you'd do when referencing your regular endpoints, but with the parameter _external=True:
Given that you have:
#app.route('/login')
def login():
....
You can generate an external URL by using:
from flask import (
url_for,
)
...
url = url_for('login', _external=True)
This will also take into account any proxies in front of your application if you need that, as long as you've used the ProxyFix middleware when setting up your app object.
Since this uses the same mechanism as Flask uses when generating URLs between different pages, it should behave just as you want - i.e. it'll work both on localhost and on the remote host.

flask-CORS extension doesn't work. What am I doing wrong? [duplicate]

This question already has an answer here:
CORS Error: “requests are only supported for protocol schemes: http…” etc
(1 answer)
Closed 3 years ago.
I'm developing a website using Flask, Vuejs and Webpack. The server and the client are separated into two folders and in development I'm running the server using flask run and the client (Vuejs) using yarn serve.
So the frontend is coming together nicely, and I now want to start doing some calls to the server. Since the client is served on port 8080, and the server on port 5000 I'm getting CORS errors. To avoid these I'm trying to use the flask-cors extension.
I've copied exactly what they do in the example:
from flask import Flask
from app.config import Config
from flask_cors import CORS, cross_origin
app = Flask(__name__)
app.config.from_object(Config)
CORS(app)
And instead of the last line I also tried the resource specific example:
cors = CORS(app, resources={r"/api/*": {"origins": "*"}})
Unfortunately I'm still getting CORS errors in the frontend.
As far as I understand the server side should send along headers which allow cors, but I'm not actually seeing a request in the browser network tab, I just get an error in the console.
Also, it says only the protocol schemes http [...] https are allowed. I'm doing an ordinary http request, so I'm not sure why that wouldn't work.
And from here I'm kinda lost. Does anybody know how I can debug this?
Ok, nevermind. I now realise I didn't add http:// to my request..
Thanks for listening anyway.. :-)

Torrent tracker proxy

I'm trying to implement a scipt on OpenShift, which works to bypass a very basic firewall in my college.
The aim is that I add the OpenShift address to the tracker list in any torrent I am running.
The client requests the script for peers.
The script accepts the peer list request and then asks for the list itself from a valid tracker. For testing purposes I have hardcoded this into the script as the tracker works for the test torrent without the firewall.
The response is passed back to the torrent client on my computer.
MyPC <==> Openshift <==> Tracker
This code is not working for some reason. I followed the flask quick start guide and the OpenShift getting started guide .
I am new to networking so please help me out.
This is the routes.py file:
#!/usr/bin/python
import os,urllib2
from flask import Flask
from flask import request
app=Flask(__name__)
app.config['PROPAGATE_EXCEPTIONS']=True
#app.route('/announce/')
def tormirror_test():
q=request.query_string
u=urllib2.urlopen("http://exodus.desync.com:6969/announce?"+str(q))
return u
#app.route("/<name>")
def insulter(name):
return "this is a test code====="+name
if __name__ == "__main__":
app.run()
I think part of it is that your university may be blocking the connection back to your computer from OpenShift. My guess is your university blocks incoming connections on port 6969
Just putting it here so you can mark it answered

Categories