Question

I am working on inter process communication between machines. Currently I have 3 laptops connected by a hub, without Internet connection.

Is there any way this code could be adapted to send and receive messages between machines with a code like this without Internet?

import socket
import sys
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
    print("Failed to create a socket")
print("Socket created")
host = ("www.google.com")
port = 80
try:
    remote_ip = socket.gethostbyname(host)
except socket.gaierror:
    print("Hostname could not be found, exiting finding socket")
    sys.exit()
print("Ip adress of",host,"is",remote_ip)
s.connect((remote_ip, port))
print("Socket connected to",host,"on ip", remote_ip)
message = "GET / HTTP/1.1\r\n\r\n"
try:
    s.send(message.encode("utf-8"))
except socket.error:
    print("Failed")
    sys.exit

print("Message send successful")
reply = s.recv(4096)
print(reply)
s.close()
Was it helpful?

Solution

You don't need internet access, you just need a connection between machines (which you apparently have). Obviously you won't be able to connect to google without internet access though.

For connections between three machines I would suggest running a server program on one, and clients on all of them (unless by hub you mean a server of some sort, rather than a small thing with a load of ethernet cables plugged in, in which case you should run the server program on that).

I would suggest that first you work through some socket tutorials, here are some I found useful:

http://docs.python.org/3/howto/sockets.html

http://www.kellbot.com/2010/02/tutorial-writing-a-tcp-server-in-python/

And some example code for a simple chat program:

http://extr3metech.wordpress.com/2012/04/28/writing-a-simple-tcp-server-client-application-in-python/

(The last two of those are for Python 2.x, which I assume you aren't using by your use of print as a function, but they should be easy enough to adapt).

At some stage, you may find that you have trouble sending information other than strings. I know I got stuck on this for ages, and tried to do stuff like rolling my own encoding system for Python objects, I just couldn't understand how you could send a list of integers, but not an integer by itself.

I solved this problem by using json encoding of data. This is simple enough and there is a module for it in the standard library.

However, although you may find it fairly easy to communicate simply using the socket library, for larger or scalable projects you probably want to use a wrapper library.

The only one of these I know anything about really is twisted however it doesn't yet work with Python 3.x.

Oh, also, you need to make sure you can connect to the other machines, as in you know the correct IP address for them. So you either need to be on the same local area network, or do something like create a virtual network (Hamachi is one thing for this) or give the server machine at least a static IP (port forwarding etc.). Generally, you have to do the kind of stuff you need to do to get a server running for a game like minecraft.

OTHER TIPS

You can use the same code but skip the dns-lookup of the hostname and make sure the "server" is listening on the right port.

Personally I'd make a wrapper for netcat or maybe implement something similar like in this answer: Netcat implementation in Python

The fact that you're not connected to the internet is not a problem. Make sure both machines are connected physically and configured to use the same network/netmask and a reachable ip address. You'd have to set this manually static since there's no dhcp (I assume) that is assigning this for you.

If the machines are to communicate via TCP/IP (which I assume from the AF_INET in your code), you will need TCP/IP between them.

AFAIK, you can't do that with a simple hub. Maybe there are black magic tricks, but generally you'd need at least router to be able to transfer data on the IP level.

Once you've set up a LAN, in order to be able to use hostnames, you can set up your DNS but since it's just 3 machines, simply editing /etc/hosts file for each of them could be easier.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top