Hello All,

Currently, I have a static setup between Server and Clients. Server IP is hardcoded into the configuration file, so that clients connect to the server using this configuration file. In our network there are many server machines that can serve these clients and balance the load.

I am new to Python network programming and Pythin newbie as well. I would like some suggestions in this regard.

My plan:

1) Broadcast message to the network.
2) Servers respond with a message 'Server'
3) Add the Servers IP to the list of "Server IP List"
4) Make the clients target the Server on round robin or list connected clients first.

If you have any alternative ideas in this regard, it would helpful. Current situation is, We have a server.config file which has server IP and port, the clients use this to connect to the root. I want to make this server.config as dynamic .

your help in this regard would be much appreciated.

Cheers
Adarsh

Recommended Answers

All 2 Replies

Have tried to do this:

1) Broadcast a message from the servers.
2) Clients receive the message from the server and store it in server.txt

Static problem:
1) server.txt is updated only once
2) when new servers join then the server.txt file is not updated

Below is my code:

#serversend.py
import socket
import sys
import traceback


#print socket.gethostname()
#print socket.gethostbyname(socket.gethostname())
#print socket.gethostbyaddr(socket.gethostbyname(socket.gethostname()))

msg = socket.gethostbyname(socket.gethostname())
dest = ('<broadcast>',10100)
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.sendto(msg, dest)
print "Looking for replies; press Ctrl-C to stop."

while 1:
    (buf,address)=s.recvfrom(10100)
    if not len(buf):
        break
    print "received from %s: %s" %(address, buf)
#client.py
import socket
import traceback
import os

host = ''
port = 10100

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
s.bind((host,port))

while 1:
    try:
        message, address = s.recvfrom(10104)
        print "Got data from", address
        if os.path.isfile("serverlist.txt"):
            os.remove("serverlist.txt")
            
        ad = ''.join(str(address));
        ad1 = ad.split()
        ad2= ad1[0]
        ad3=ad2[2:15]
        print ad3
        f = open('serverlist.txt', 'a')
        f.write(ad3+'\n')
        f.close()        
    except (KeyboardInterrupt, SystemExit):
        
        raise
    except:
        traceback.print_exc()

Could anybody help me to solve this problem.

Thank you

There are a couple of bugs that jump out at me.

First, notice that you remove the "serverlist.txt" file each time a server message is received. So naturally, only the last server that broadcasts will be in your list -- which I don't think was the desired behavior.

Second, your server is trying to receive a message. Why? The client never sends a message, so the only thing it could receive is ... broadcasts from the other servers.

So I think your algorithm needs some refinement still.

Jeff

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.