Run flask file on wlan1 instead of wlan0 - python

I have a raspberry pi, a flask server, a flask client, and two different networks.
when I connect a wifi adapter to the raspberry pi I can see that I have a new interface called "wlan1" is there a way to run a the server for example on "wlan0" and the client on "wlan1".
what I'm trying to do is run the server on a different network than the client (while both of them are on the pi).

Server:
For the server part, you need to "bind" the listening socket to the IP address of wlan0.
Find the IP address of wlan0 using ifconfig wlan0 or ip addr show dev wlan0 (e.g. 192.168.0.2)
Bind the Flask server to that IP address using app.run(host='192.168.0.2', port=80)
If you bind to 0.0.0.0, it will be reachable from all network devices.
Client:
A little bit more involved, take a look at how "routing tables" work for the theory.
Find out the IP address of the server that your client will connect to (e.g. 93.184.216.34)
Find out the IP address of the default gateway on the interface wlan1, for example with ip route (look for "default via dev wlan1"), e.g. "default via 192.168.1.1 dev wlan1"
Add a route to that IP address via the gateway and interface, using route add 93.184.216.34 gw 192.168.1.1 dev wlan1
Note that the routing table will affect all programs on the raspberry pi, not just your client application.

Related

How can I make my client and server IPv6 compatible? (Flutter, Python, Nginx)

I have a mobile app that sends location information to database. The server (Tavu.io) is Ubuntu based and uses Nginx. The problem is that when I try to connect to the server using IPv6 address, the data is not transferred. The application should work in IPv6 network only. UDP protocol should be used.
In the nginx.conf, there are path to files included that contain:
listen 80 default_server;
listen [::]:80 default_server;
netstat -pnltu shows that "tcp" and "tcp6" have LISTEN. But, there is only "udp", and no "udp6" which I assume it should be. There is no LISTEN on "udp".
My Python socket is like this:
self.connection = db.connect_to_database()
self.status = True
self.udp = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP)
self.udp.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
self.udp.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
self.udp.bind(('', 50943))
And the mobile client (Dart/Flutter):
static String serverIP = '[ipv4 address hardcoded]';
static Future<RawDatagramSocket> rDgS =
RawDatagramSocket.bind(InternetAddress.anyIPv4, 50943);
rDgS.then(
(RawDatagramSocket udpSocket) {
udpSocket.writeEventsEnabled = true;
List<int> data = utf8.encode(message);
udpSocket.send(data, InternetAddress(serverIP), 50943);
},
);
That above code works at that state as intended, but when is use hardcoded IPv6 address and change to .anyIPv6, the sent data is not reaching the server. No error is shown on the VSCode console though. The reason the IP addresses are hard coded because I don't have the hostname for the server and giving server name to nginx.conf was not working. Is there a workaround to this issue to have client to connect to server while in IPv6-only network?
I have changed the settings on nginx and attempted multiple times this application on IPv6-only network but the data (location and username) are not sent to database.

Porting domain to raspberry pi server

I'm running a Flask app installed on my Rasperry pi and I am able to access it via the local network at the internal IP and port 8080 (192.168.0.21:8080). I am now trying to access it externally from the network.
I have a netgear router that I've setup to forward port 8080 to 192.168.0.21 (my raspberry pi's internal IP). When I visit aaa.bb.cc.dd:8080 (my external IP) I don't see anything, what am I doing wrong here?
This is the line I am using to start the app from my raspberry pi:
app.run(host='192.168.0.21', port=8080, debug=True)
Ideally I would like to have a subdomain on a domain that I own point at this app, would I basically just point the A record for that subdomain at my external IP?
Update
I've since tried several different ports (ie. 8910, 87, 22) and forwarded these to my raspberry pi's internal IP still to no avail :/ I've even taken a step back and made a super simple hello world app and I am still not having any luck
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'hello world'
if '__name__' == '__main__':
app.run(host='0.0.0.0', port=8910, debug=True)
My router forwarding settings are below:
And then I am basically going to aa.bb.cc.dd:8910 (where aa.bb.cc.dd equals my external ip).
I've also tried porting port 8000 to 80 and then forwarding it to the pi and still nothing.
Solution
I ended up porting the external port 80 to internal port 8000 at the static IP address for my raspberry pi and then running the raspberry pi server on port 8000. I then forwarded my sub-domain to my external IP address and voila!

DNS requests over ICS (Internet Connection Sharing)

Recently i implemented i small captive portal in python. i redirect users to the login page from dns requests. All worked fine until i realised when dns server i manually change on client system to a public dns, it totally bypass the captive portal. My problem is, how to redirect users even with dns servers changed or how to block all outgoing dns requests which is not using the default dns.
I was thinking listening on port 53 would capture all request using twisted.
This is a very simple example of how i am doing it:
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor`
class UDP(DatagramProtocol):
def datagramReceived(self, datagram, addr):
print datagram, addr
port = 53
max_byte = 512
reactor.listenUDP(port, UDP(), '', max_byte)
reactor.run()
Am i doing it wrong?
I also tried to block remote port 53 from the firewall on the main machine providing Internet connectivity but it also doesnt work.
If users are bypassing your captive portal by changing DNS, the issue is that they can route DNS requests around the portal, and therefore there's nothing you can do in the portal. You need to create routing rules which redirect all port 53 traffic on your network to your DNS server, regardless of where they're trying to send it.
The bad news is, you can't do this with Twisted. You need to do this in your router's operating system, using something like iptables.

A Server Behind A Router [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm trying to set up a server behind a router, and I've been able to reduce it to the following problem:
I use:
Siemens SL2-141 Router.
Windows 7 64-bits with Python 2.7.
I run:
server.py:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((IP, 8080))
sock.listen(1)
sock.accept()
print 'success'
sock.close()
client.py:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((IP, 8080))
sock.close()
When IP = '127.0.0.1' it works.
I set up a static IP:
> Network And Sharing Center > Change Adaper Settings > Local Area Connection
> Properties > Internet Protocol Version 4 (TCP/IPv4) > Properties
> Use The Following IP Address:
IP Address: 10.0.0.200
Subnet Mask: 255.0.0.0
Default Gateway: 10.0.0.138
> Use The Following DNS Server Addresses:
Preferred DNS Server: 10.0.0.138
Alternate DNS Server: - - - -
And when IP = '10.0.0.200' it works.
I set up port forwarding on my router:
> http://10.0.0.138/
> Username: Admin
> Password: Admin
> Advanced > Virtual Server > Port Forwarding > Add:
User Defined: Test
From Internal Host IP Address: ALL
Forward to Internal Host IP Address: 10.0.0.200
Protocol: TCP
External Packet Port Start: 8080
External Packet Port End: 8080
Forward To Internal Host Port Start: 8080
Forward To Internal Host Port End: 8080
> Apply (and reboot router, just in case).
To my understanding, if I leave the server IP '10.0.0.200' and set the client IP to my public IP it should work, but it doesn't work ("no connection could be made because the target machine actively refused it").
I tried doing the same thing with an Apache server and the result was similar: browsing localhost worked, browsing the static IP worked, but browsing the public IP didn't work (port 80 gave me the router setup page, and port 8080 just couldn't connect).
Final notes:
I turned off my firewall.
I checked that the port is open (http://www.canyouseeme.org/) and it is.
I checked that the server is listening (netstat -na | find /i "8080") and it does.
Any ideas what's the problem?
To my understanding, if I leave the server IP '10.0.0.200' and set the client IP to my public IP it should work, but it doesn't work.
That's right, if the client is on an external network. On the local network if you use your public address it may or may not work depending on if your router implements NAT reflection (if not it will drop the packets). You should use the your local (private) IP address on your local network. Many routers allow you to configure DNS records for local resources (that override records from the DNS server, implementing a type of "Split DNS"). That way you can use one DNS name to get the correct address.
If you're problem is with a connection from a client on the external network, it sounds like somehow your NAT router is not port forwarding. I don't have your model of router, but I see this line:
From Internal Host IP Address: ALL
And wonder if you have to allow From External.
Also are you sure this rule is enabled? (I only ask because the last time I had a problem like this I had created the forwarding rule correctly but it wasn't enabled.)
If you're still stuck, try removing the rule and re-test if the port looks open to http://www.canyouseeme.org/. I would also re-test when NOT running your server program (to test if the external port scan is misleading, which can happen).
Finally, when you write:
I turned off my firewall.
Do you mean Windows Firewall? You'll want to double check that too because Windows Firewall can allow local connections while blocking remote connections.

In need of a python script to upload a file with IP address every few minutes

I'm using windows server 2008, and one of the things I need to do in order to pair to a domain name is send a file with the computers current IP address (it's not static) to a server via sftp every few minutes. The problem is that I'm not sure how to do this.
I would send it via XMPP. You can set up a listener service for the server.
Send an xmpp message using a python library
Here are some ideas on XMPP servers to run on your IIS server (listening to recieve the incoming messages from clients http://metajack.im/2008/08/26/choosing-an-xmpp-server/
Pretzel looks nice
this python code can be run client side to get the public IP address.
host, aliaslist, lan_ip = socket.gethostbyname_ex(socket.gethostname())
print host
print aliaslist
print lan_ip[0]
Than you would send via XMPP message containing the IP to the server you have set up on your IIS server. Depending on what you want to do with the IP address once it gets to the server, you will handle the message serverside

Categories

Resources