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

Dani AI

Generated

A simple and robust discovery pattern is to flip your flow: have clients broadcast a short probe and have servers reply unicast. Include a magic token and version (e.g., DISCOVER_SERVER v1) so you can ignore unrelated traffic and evolve the protocol. Clients should collect replies for a short window (e.g., 1–2 seconds), deduplicate by IP, and then write the full set to disk atomically (write to a temp file and rename) instead of deleting/rewriting on each packet. Run this periodically (or on startup plus a refresh timer) so new servers are picked up and stale ones age out. On the server side, listen on a fixed UDP port and answer only valid probes with your IP and any metadata (service port, cluster ID, load metric if desired).

Example client probe that gathers all replies within a timeout:

import socket, time

BCAST = ("255.255.255.255", 10100)
PROBE = b"DISCOVER_SERVER v1"
timeout = 1.5

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.settimeout(timeout)
sock.sendto(PROBE, BCAST)

servers = set()
t_end = time.time() + timeout
while True:
    try:
        data, addr = sock.recvfrom(1024)
        if data.startswith(b"SERVER v1"):
            servers.add(addr[0])
        if time.time() > t_end:
            break
    except socket.timeout:
        break

For larger or cross-subnet networks, consider multicast or DNS-SD/mDNS (Bonjour) via the Python Zeroconf library; see RFC 6762 and RFC 6763. Socket details: socket, and nonblocking waits via select. Limit broadcasting to trusted LANs and throttle probe frequency.

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.