Accessing device on local network through server hosted webhook - python

I have a python script that acts as a webhook. A part of it is as follows:
import json
import os
import urllib
import socket
import _thread
from flask import Flask
from flask import request
from flask import make_response
app=Flask(__name__)
ip = ('192.168.1.75', 9050)
#app.route('/webhook',methods=['GET','POST'])
def webhook():
_thread.start_new_thread(sendDataToDevice,(ip))
req = request.get_json(silent=True,force=True)
print("Request:")
print(json.dumps(req,indent=4))
res=makeWebHookResult(req)
res=json.dumps(res,indent=4)
r=make_response(res)
r.headers['Content-Type']='application/json'
return r
if __name__ == '__main__':
app.run(port=8080,host='localhost')
The function of the script is to send some data to a device connected to the local network.
It works flawlessly when I open my web browser and type the following on the url bar:
http://localhost:8080/webhook
I want to host the script on a server, eg. Heroku. How can I access the local device in that case?
Note: I know I can run the script on my local machine and make it visible to the internet using ngrok, but I want to keep it accessible even when my computer is switched off. Also, want a fixed link, and the links given by ngrok change on every run.

I've faced a similar issue before with IoT. Unfortunately there is no simple way to make a device be visible online. Here's a simple solution I've used. It might not be the best, but it works.
DDNS + Port Forwarding + Static IP
If you have access to your local WiFi router, then you can setup something called as DDNS (Dynamic Domain Name System). Your router will then connect to a DDNS service provider like no-ip (www.noip.com) and it will be visible on the internet. You can give a custom URL like susmit-home.noip.com.
However susmit-home.noip.com will now point only to your WiFi router and not your WiFi network. So if you want to access the local device_ip and device_port such as "192.168.1.75", 9050. Then you can setup Port Forwarding on your router for that local IP-Port combination. Usually the setup looks like this:
Local IP: device_ip (e.g. 192.168.1.75)
Local Port: device_port (e.g. 9050)
Outbound Port: any_port (e.g. 9050)
Make sure that your device_ip is a static IP on your WiFi router so that it doesn't change.
Finally in your code you can just replace the line ip = ('192.168.1.75', 9050) with ip = ('susmit-home.noip.com', 9050).
Other solutions:
A slightly more complicated solution is setting up a VPN, such that your local network and your remote server (e.g. Heroku) will all be available to each other as if they were within the same local network.
If your device is a computer or a Raspberry Pi, then you can use SSH Remote Port Forwarding to have access to your local device from the remote server.

Related

How to make Python HTTP server have custom url instead of localhost?

I'm using the HTTP server module in Python 3 and I want to make my local HTTP server have a custom url instead of "localhost:8080". Is there any way for me to do this in Python alone without changing default OS settings like the settings in the host file?
When you can setup your router, you must make a port-forwarding. (You usually can setup it on http://192.168.0.1/)
Then you can use that url: "http:// Your Global IP:8080/" for example, when my global IP address is 4.4.0.0, than "http://4.4.0.0:8080/".
When you use the port 80 instead of 8080 (you can setup in port forwarding), it is the professional port of http server, your url will be (with the last example ip): "http://4.4.0.0/"
You can see your global IP address for example on this page: https://whatismyipaddress.com/
or from python code:
gloval_IP = get('https://api.ipify.org').text

Access Flask application from local network

I want to be able to run Flask on a LAN connection without connection to the internet. When running the flask application it starts and runs on host = 0.0.0.0:5000 but the site can't be reached from browsers going to 0.0.0.0:5000. Running with an internet connection to the network allows the site to show up. When communicating from another device on the same network sending a post to the Flask's ip and correct endpoint allows for a successful http post request with response when the network does not have internet.
The main function is shown here.
if __name__ == "__main__": port = 5000 app.run(port = port,host = HOST, debug = True)
Bottomline: How do I run flask with a wifi connection that does not have internet to communicate with other devices on the network and show the site at 0.0.0.0:5000? thanks
its 127.0.0.1:5000 from your same pc or the local ip like 192.168.1.x from other pcs on the same lan, check your ip with ipconfig
Change the code to this:
if __name__ == "__main__":
app.run(host='127.0.0.1', debug=True)
127.0.0.1 is the localhost of your computer and does not require internet connections, besides, the address is used to establish an IP connection to the same machine or computer being used by the end-user. So it still can run even without the internet.

I can't connect to my Bottle server on external devices

I'm trying to get a Bottle server in Python to work. Here is my code:
from bottle import route, run, template
from socket import gethostname, gethostbyname
from time import sleep
ip = str(gethostbyname(gethostname()))
#route('/')
def index():
return 'Hello World!'
run(host=ip, port=1234)
I run this, and on my computer where I'm running it, I navigate to http://127.0.1.1:1234/, and my website shows up, with Hello World!.
However, if I try to connect to it on my phone or my sister's Chromebook, it says that the website refused to connect.
I have tried replacing str(gethostbyname(gethostname())) with '0.0.0.0' and 'localhost', but none have worked.
Get rid of this line; it's not necessary:
ip = str(gethostbyname(gethostname()))
Make your run line look like this:
run(host='0.0.0.0', port=1234)
The address 0.0.0.0 means "listen on all addresses".
Lastly, figure out the network address of the host on which your app is running. Then other devices on the same network should be able to connect to <that ip address>:1234. Devices not on the same network would only be able to connect to the service if you had a publicly routeable address (or if you arranged to forward the appropriate port from a router that has a public address).
You'll want to make sure the system on which your app is running doesn't have firewall rules that would prevent an otherwise successful connection.

Flask server not visible from my public ip address

I'm trying to run a flask server on my desktop PC that is publicly available on the internet. I've done the following:
Set up a static IP address: 192.168.1.11 (http://i.imgur.com/Z9GEBYV.png)
Forwarded port 33 on my router to my static ip address (http://i.imgur.com/KGNQ2Qk.png)
Setup flask to use my static ip and port: 33
I'm using the following code as a test webserver
from flask import Flask, request, redirect
app = Flask(__name__)
#app.route("/")
def hello_world():
return "Test 123 "
if __name__ == "__main__":
app.run(host="0.0.0.0", port="33")
When I open my browser to: http://192.168.1.11:33/ the page displays properly, I see "Test 123"
My problem comes when trying to connect to my webserver from my public ip address When I open my browser to http://xx.xxx.xxx.xx:30 (my ip address) all I see is "this site can't be reached, xx.xxx.xxx.xx refused to connect"
I've looked up all the stack overflow answers, I've done the following:
Turned off windows firewall
Changed host from "192.168.1.11" to "0.0.0.0"
Tried a different port
screenshot of code running and error shown: http://i.imgur.com/a05GvEs.png
My question is: What do I need to do to make my flask server visible from my public ip address?
Do you have DHCP activated on your router?
If yes do you see your host as 192.168.1.11 in there?
You have to use '0.0.0.0' on host, that tells Flask to listen on all addresses.
Try specifying the port with quotes as app.run(host="0.0.0.0", port="33")
change it to app.run(host= '0.0.0.0', port="33") to run on your machines IP address.
Documented on the Flask site under "Externally Visible Server" on the Quickstart page:
http://flask.pocoo.org/docs/0.10/quickstart/#a-minimal-application
Add port forwarding to port 33 in your router
Port forwarding explained here
http://www.howtogeek.com/66214/how-to-forward-ports-on-your-router/
You must give the public ip address/LAN ip address as an argument to app.run method.
When you don't provide host argument, it works fine with http://localhost:8888/ and http://127.0.0.1:888/, but not to access outside the system where you are running the REST services
Following is the example.
app.run(host="192.168.0.29",debug=True, port=8888)
You must try and use the same ip of the development server. Thus, for instance, if the dev server is running on a PC with the address 192.168.1.11
and port 33, other clients must point at the same address: 192.168.1.11:33.
As far as my small experience, it works with the debugger disabled, but I did not check if this is an essential prerequisite.
good luck
Every webservice should be run from different port address.Single service is running from a single port.

Flask - How to make an app externally visible through a router?

The question in short
How do you run a simple Flask-based website visible to the internet, from a host PC which is accessing the internet from a wireless router?
Question details
I would like to make a flask application visible to the internet, as per Quickstart Guide.
If I launch the simple Flask app below, it becomes accessible from a computer on the same network as the host pc, but not from a device connected through the internet through another network.
The problem is similar to the one discussed here and here, with the added element that running from a home pc seems to suggest that external connections point to the xx port of the router, not to the xx port of the host pc, as is suggested in the comments in this post.
What I did
Referencing the code below, here's what I did:
Checked my IP address in Control Panel
disabled all network protection in the antivirus
run `ipconfig /all', being on a windows machine
finally opened a browser in a device connected to another network and pointed it to the appropriate IP:port address
The result is that "The webpage is not available".
Has anybody encountered the same problem? Is this a router issue?
Reference Flask app
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run(host= '0.0.0.0', port=9000, debug=False)
The basic setup will be to create a rule which will forward request to port 80 and/or port 443 to a destined host in your local network.
Example create NAT(address translation) and port forwarding rule to forward inbound HTTP/S requests to your local network host running your python application.
For example:
app.run(host= '192.168.0.58', port=9000, debug=False)
Your NAT rule should target 192.168.0.58 on port 9000.

Categories