Felix Werner Burgos
Felix Werner Burgos

Reputation: 11

Send a message to multiple hosts with python socket.

first of all i want to clarify. I don't have any deep knowledge on python, but im trying to learn while doing a project for college so lets start and thanks for your time.

I'm working with Python 3.4 on Windows 10 for the hosts and Ubuntu in an Odroid as a server.

The problem on my code is that i need to send a code like 0001 to a set of hosts of which i have their ips but when i try to close the socket it won't do it and i can't open a new one to send the code to the next host.

Client code :

[yes, it's a socket server code but when i try to connect multiple clients to one server, the code stopped on listening new connections and i couldn't "fix" it in another way]

while 1:
TCP_PORT = 5000
BUFFER_SIZE = 4  # Normally 1024, but we want fast response
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('', TCP_PORT))
s.listen(1)
s, addr = s.accept()



data = s.recv(BUFFER_SIZE)

if not data:
    break

if data.decode('ascii') == '0001':
    print (data.decode('ascii'))
    s.shutdown(socket.SHUT_RDWR)
    s.close()

Server side code:

    import serial
    import time
    import mysql.connector
    import shutil
    import socket
    import sys
    import _thread

    joined_seq = '0001'
    Alerta = True
    TotalClientes = True


    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)  

    seq = []
    UltimaIPextraida = ''
    port = 5000


while Alerta == True:

    if TotalClientes == True:
        cnx2 = mysql.connector.connect(connection parameters removed)
        cursor2 = cnx2.cursor()
        cursor2.execute("SELECT MAX(Entrada) FROM Host")

        for (Entrada) in cursor2:
           NoTupla = ''.join(str(v) for v in Entrada)
           ValorMAX = int(NoTupla)

        cursor2.close()
        cnx2.close()
        TotalClientes = False


    if ValorMAX > 0:
        print('Host Numero', ValorMAX)
        cnx3 = mysql.connector.connect(connection parameters removed)
        cursor3 = cnx3.cursor()
        query = ("SELECT IP FROM Host WHERE entrada = '%s' " % (ValorMAX))
        cursor3.execute(query)

        for (IP) in cursor3:
            IPextraida = ''.join(str(v) for v in IP)

        cursor3.close()
        cnx3.close()

        #if ValorMAX == 1:  #DESCOMENTAR
        #   Alerta = False   #DESCOMENTAR

        ValorMAX = ValorMAX -1
        print('IP Extraida = ' + IPextraida)
        print('Ultima IP Extraida = '+ UltimaIPextraida)


        if UltimaIPextraida == IPextraida:
            print('Ultima IP extraida es identica a la nueva ip extraida, pasando a la siguiente')



        elif UltimaIPextraida != IPextraida:

            try:
                s.connect((IPextraida, port))
                s.send(joined_seq.encode('ascii'))
                s.shutdown(s.SHUT_RDWR)
                s.close()
                print('Mensaje enviado correctamente = ' + joined_seq)


            except:
                 print('No se pudo conectar con host')



         UltimaIPextraida = IPextraida

** some code was ommited because it has no relevance **

With the mysql query i get the total of ip entries on the table and then with that i get all ip one by one to make the socket connection.

I really hope someone can help me to solve this problem... i've been 2 days trying and i'm running out of time to finish the code and it isn't the main part of the project, i only need a functional code to show some capabilities of the arduino functions graphically.

thank you all for your time and sorry for the gramatical errors and the code in spanish :(

Upvotes: 0

Views: 616

Answers (1)

Felix Werner Burgos
Felix Werner Burgos

Reputation: 11

Solved with this:

def enviaMensaje(ipdelwn):
    enviado=False
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)  
    try:
        s.connect((ipdelwn, port))
        s.send(joined_seq.encode('ascii'))
        s.close()
        print('Mensaje enviado correctamente = ' + joined_seq)
        enviado=True
   except:
        print('No se pudo conectar con host')
   return enviado

Upvotes: 1

Related Questions