Example #1
0
 def test_connect_fail_host(self):
     s = tcp.create()
     success = True
     try:
         tcp.connect(s, "gaagle.france", 80)
     except:
         success = False
     finally:
         s.close()
     self.assertEqual(success, False)
Example #2
0
 def test_connect(self):
     s = tcp.create()
     success = True
     try:
         tcp.connect(s, "google.fr", 80)
     except:
         success = False
     finally:
         s.close()
     self.assertEqual(success, True)
Example #3
0
def inter(remote_ip, port, data):
    rep = 0
    if data != "":
        s = tcp.connect(remote_ip, port)
        
        if s != False:
            #Send data
            if not tcp.send(data, s):
                return False
            
            #Receive data
            rep = tcp.receive(s)
            
            if rep == False:
                print("error disconnected")
                return False
            else:
                s.close()
                return rep
                
        else:
            print("An error has occured")
            return False
    else:
        return False
Example #4
0
    def __init__(self, host, port):
        self.name = "rpcc(%s:%d)" % (host, port)
        self.sock = tcp.connect(host, port, self.on_client_data,
                                self.on_client_close)

        self.nextreqid = 1
        self.maxreqid = 0xffffffff

        self.requests = {}

        self.inst[host, port] = self
Example #5
0
 def connect(self, host, port):
     self.__sock = tcp.create()
     tcp.connect(self.__sock, host, int(port))
     self.connected = True
Example #6
0
def main():

    conn = sql.connect_database()

    connections = []

    new_connections = sql.select_table_n(conn, sql_connections)

    for i in range(len(new_connections)):
        if new_connections[i][2] == 'tcpip':
            connections.append(
                (tcp.connect(new_connections[i][3],
                             int(new_connections[i][4])), 'tcpip'))
            active_connection(conn, new_connections[i][0])
        elif new_connections[i][2] == 'rtu':
            connections.append((rtu.connect(new_connections[i][4]), 'rtu'))
            active_connection(conn, new_connections[i][0])
        else:
            print('Bus from ', new_connections[i][1],
                  ' uses a protocol is not supported.')  # CORRIGIR O INGLES
            print(
                'You need to fix the connections table to continue the process.'
            )
            print('The process will be ended.')
            return

    arguments = sql.select_table_n(conn, sql_arguments)
    conn.close()

    interrupt = False
    try:
        while not interrupt:
            conn = sql.connect_database()

            write_request = sql.select_table_n(conn, sql_write_requests)
            if write_request != None:
                for wr in write_request:
                    if protocol_for_id(conn, wr[0]) == 'tcpip':
                        print('Writing not yet implemented for TCP/IP')
                    else:
                        rtu.fc6(connections[wr[0] - 1][0], wr[2], wr[3], wr[1])

                sql.delete_table_n(conn, sql_clear_write_request)

            for i in range(len(connections)):
                arguments = sql.select_table_n(conn, sql_arguments)
                size = sql.select_table(conn, sql_read_size, str(i + 1))[0][0]
                if connections[i][1] == 'tcpip':
                    for j in range(i, size + i):
                        t = strftime("%d/%m/%Y %H:%M:%S", gmtime())
                        data = tcp.fc4(connections[i][0], arguments[j][2],
                                       arguments[j][3], arguments[j][1])
                        if data == None:
                            print('There is no slave ' + str(arguments[j][1]) +
                                  ' or above.')
                            print('Deleting these slaves from the table.')
                            print('Please fix the error on connections table.')
                            sql.delete_table(
                                conn, sql_delete_read_arguments,
                                (arguments[j][0], arguments[j][1]))
                            break
                        insert_data(conn, arguments[j][3], arguments[j][0],
                                    arguments[j][1], arguments[j][2] + j, data,
                                    t)
                        insert_history(conn, arguments[j][3], arguments[j][0],
                                       arguments[j][1], arguments[j][2] + j,
                                       data, t)
                else:
                    for j in range(i, size + i):
                        t = strftime("%d/%m/%Y %H:%M:%S", gmtime())
                        data = rtu.fc4(connections[i][0], arguments[j][2],
                                       arguments[j][3], arguments[j][1])
                        if data == None:
                            print('There is no slave ' + str(arguments[j][1]) +
                                  ' or above.')
                            print('Deleting these slaves from the table.')
                            print('Please fix the error on connections table.')
                            sql.delete_table(
                                conn, sql_delete_read_arguments,
                                (arguments[j][0], arguments[j][1]))
                            break
                        insert_data(conn, arguments[j][3], arguments[j][0],
                                    arguments[j][1], arguments[j][2] + j, data,
                                    t)
                        insert_history(conn, arguments[j][3], arguments[j][0],
                                       arguments[j][1], arguments[j][2] + j,
                                       data, t)

            conn.close()

            print('Complete cicle'
                  )  # Read available devices, and write if request
            sleep(1)

        print('Finishing the main process.')

    except KeyboardInterrupt:
        interrupt = True