Messaging in Lan - python

I need advice on some problem I've been having.
The situation is like this. I have two devices in Local Network that are not directly connected with each other. Basically they don't know of each other existence. The problem is that I want one device to send message to the other device.
So the plan i have is this.
Make script that scans every ip address in local network and save output in txt file. Then send message to every ip address that is present in that file. On the other device make script that listens for upcoming messages(problem there is that i don't know where to listen, to whitch ip address to listen). Then that python script would send that to websocket so that js app can easily pick every message that has been sent.
This is my first app that includes some networking problems and message queues, so if any of you have some advice for me, or would do something different i would be very greatful if you can answer :)
Thank you.

In order to find communication peers on the network you might want to have a look at udp broadcasts.

Related

What would be the best way to transmit data from one python script to another, without them being on the same computer?

I am attempting to create a basic chatroom in python, and I would like to know how I could transmit data from one script to another, preferably without using google drive. If needed to, I could create a webserver on Replit, but I don't do well with HTML or PHP.
Side note: I can't port forward, as my google wifi doesn't accept any level of port forwarding.
I would send messages of about 50 characters every couple seconds
Since you mention port forwarding, I assume you want two chat clients that run on different local networks to talk to each other, for example your own and the chat client of a friend in a remote location, over the internet.
If you (or your counterpart) cannot set up port forwarding, then direct communication between the script on your computer and theirs is hard, if not impossible. The solution is to set up a third computer or service on the internet that can be reached by both clients and use it for relaying messages between them.
A network is typically protected by a firewall of sorts and will typically be behind a router that performs network address translation (NAT) to help multiple devices on a network to simultaneously access services on the internet, whilst all using the same IP address on the internet. Port forwarding fits into that by connecting a specific port from the outside directly to a port on a machine on the inside - without that, an outside computer might be able to reach your IP address, but they could never connect to a computer or program on the inside of the network, as the router wouldn't know what computer to contact, also the firewall might disallow the connection to begin with.
But if your computer on the inside establishes a connection with an accessible server on the internet, expecting a response, that creates a temporary conduit through the router and firewall that can be used by the server to send messages (look up 'hole punching' for more information). And if both computers do this, the server can relay message between both clients. Only the server then needs to run in an environment that doesn't have firewall restrictions or NAT that prevent this.
You could write a simple Python server, that accepts incoming connections and can send several responses and a simple client that connects to it, identifying itself and joining a chatroom, or having a direct conversation with another connected client. There are many techniques that would allow you to do this, but I think web sockets might be a good starting point, as long as you don't plan to do advanced fast or high volume stuff that would require something like a UDP connection.
A library like websockets could be a good starting point, but you may want to start out by figuring out where you would have this service hosted first, since there may be limitations on what you're able and allowed to do.
Also, if all you're looking to do is send simple messages, you may want to stay away from writing your own server an protocols at all - have a look around for open source message servers written in a language you are comfortable with, or that just work out of the box without any development, in which case the language doesn't even really matter, as long as you can connect to it and exchange messages from Python.

Get caller's IP in Pyro4 application

To cut the story short: is there any way to get IP of untrusted client calling remote proxy object, using Pyro4?
So person A is calling my object from IP 123.123.123.123 and person B from IP 111.111.111.111 is there any way to distinguish them in Pyro4, assuming that they cannot be trusted enough to submit their own IP.
I ran across your question when looking for the same thing. If I understand, on the server side you want to know the IP address the client's socket is connected from, right? If so, this'll do it:
Pyro4.current_context.client.sock.getpeername()[0]
Found that in this section of the Pyro4 user's guide
Here is my solution to my problem: since I didn't really need to get specific addresses of clients using pyro, just to distinguish clients in specific subnet (in my classrom) from other ips (students working from home). I just started two pyro clients on two ports and then filtered traffic using a firewall.

Python socket chat problems

I've found a code for a chat app in Python, but I can't find anything about the author or anyone on the site to help me with it..
this is a link to the whole code:
http://files.myopera.com/manojsheokand666/blog/chat.py
I'm getting a feeling something is missing.. and I need this, I want to modify it and try to learn something more
I did some reading and this is my third time editing this post..
NOW, I'm able to stay connected without getting any error, but when I try to send(type in) something it's not sending nor receiving. But whenever I try to run a second app as another "person", I am getting a message on the first running app that "person" has connected, and the first app crashes with this error:
KeyError: ('127.0.0.1',62833) - note, the port is always diferent
While, the second app stays but it's not receiving anything or crashes if I run the app again.
What I did:
host = gethostbyname(gethostname()) #this actually gets 192.168.0.101 (my local IP to the router)
s.setsockopt(SOL_IP,IP_ADD_MEMBERSHIP,\
inet_aton(addr)+inet_aton(host)) #i write 225.0.0.1 as 'addr'
Is there other way to get this working? I can run a simple server/chat using telnet but this GUI(tkinter) think makes it complicated for me, and I want to learn how this works..
Thanks!
From the definition of the IP_ADD_MEMBERSHIP option, the first address is a multicast group address and the second is an interface address.
You are using 127.0.0.1 as the first address. This is not a multicast address.
Multicast addresses are in the range 224.0.0.0/4 (i.e. 224.0.0.1 to 239.255.255.254, not including network and broadcast addresses).
For example, using the first (all hosts on same network segment) multicast address works just fine:
>>> s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
>>> s.setsockopt(socket.SOL_IP,socket.IP_ADD_MEMBERSHIP,
socket.inet_aton('224.0.0.1')+socket.inet_aton('0.0.0.0'))
Check this reference for more information about multicast addresses.
So you need to choose an unassigned multicast address in 224/4 for your application and use that (e.g. anything in the ad-hoc range, like 244.0.2.0). Note the multicast address has nothing to do with the interface address (using '0.0.0.0', you associate all local interfaces with the multicast address, meaning all interfaces can be used to receive/send multicast packets for that group).

Decentralized networking in Python - How?

I want to write a Python script that will check the users local network for other instances of the script currently running.
For the purposes of this question, let's say that I'm writing an application that runs solely via the command line, and will just update the screen when another instance of the application is "found" on the local network. Sample output below:
$ python question.py
Thanks for running ThisApp! You are 192.168.1.101.
Found 192.168.1.102 running this application.
Found 192.168.1.104 running this application.
What libraries/projects exist to help facilitate something like this?
One of the ways to do this would be the Application under question is broadcasting UDP packets and your application is receiving that from different nodes and then displaying it. Twisted Networking Framework provides facilities for doing such a job. The documentation provides some simple examples too.
Well, you could write something using the socket module. You would have to have two programs though, a server on the users local computer, and then a client program that would interface with the server. The server would also use the select module to listen for multiple connections. You would then have a client program that sends something to the server when it is run, or whenever you want it to. The server could then print out which connections it is maintaining, including the details such as IP address.
This is documented extremely well at this link, more so than you need but it will explain it to you as it did to me. http://ilab.cs.byu.edu/python/
You can try broadcast UDP, I found some example here: http://vizible.wordpress.com/2009/01/31/python-broadcast-udp/
You can have a server-based solution: a central server where clients register themselves, and query for other clients being registered. A server framework like Twisted can help here.
In a peer-to-peer setting, push technologies like UDP broadcasts can be used, where each client is putting out a heartbeat packet ever so often on the network, for others to receive. Basic modules like socket would help with that.
Alternatively, you could go for a pull approach, where the interesting peer would need to discover the others actively. This is probably the least straight-forward. For one, you need to scan the network, i.e. find out which IPs belong to the local network and go through them. Then you would need to contact each IP in turn. If your program opens a TCP port, you could try to connect to this and find out your program is running there. If you want your program to be completely ignorant of these queries, you might need to open an ssh connection to the remote IP and scan the process list for your program. All this might involve various modules and libraries. One you might want to look at is execnet.

Network Communication program in python

Basically what I'm trying to achieve is a program which allow users to connect to a each other over a network in, essentially, a chat room. What I'm currently struggling with is writing the code so that the users can connect to each other without knowing the IP-address of the computer that the other users are using or knowing the IP-address of a server.
Does anyone know of a way in which I could simply have all of the users scan the IP range of my network in order to find any active 'room' and then give the user a chance to connect to it?
Also, the hope is that there will be no need for a central server to run this from, rather every user will simply be connected to all other user, essentially being the server and client at the same time.
I can give you two suggestions. First of all, UDP packets to the broadcast address of your network will be received by everybody. Secondly, there is a protocol for programs offering certain services to find each other on a local network. That protocol is called mDNS, ZeroConf, or Bonjour.
Using broadcast UDP is likely going to be the faster route. But if I were you, I'd learn how to use ZeroConf instead. It's supported well under IPv6 and already used by several interesting programs such as SubEthaEdit and Gobby.
Here is a link to a nice tutorial for implementing something that speaks ZeroConf in Python.
Another recommendation... If you want to hand roll your own broadcast/multicast UDP code and you can be sure that all of the systems you're on are running a Linux that's newer than 2003 or so, and all the Windows systems are XP or better, you can probably get away with using IPv6. The IPv6 link-local (think same LAN) all hosts multicast address is ff02::1. That's really simple and easy, and it will reach all the other systems on the same LAN. It's much better than having to figure out what your network's broadcast address is with IPv4.

Categories

Resources