0
  • I've created socket server in one .py file
  • I've created socket client into an another .py file
  • I've created a demo database into an another .py file

I want that client verify a social number from database thru server and now the party begins.. 1st issue: When I introduce another social number to be verified if exists, the previous one appears and when I introduce another social number, the social number that I've asked for to be verified before appears double times :-) 2nd issue: Now I have the same database twice in two different folders. It's driving me crazy :-) Please give me a hint what I'm doing wrong

Here are my sheets:

database

from sqlalchemy import create_engine, Column, Integer, String, select
from sqlalchemy.orm import sessionmaker, declarative_base
class Personas():
    personas = create_engine('sqlite:///personas.db')
    Session = sessionmaker(bind=personas)
    sessionDB = Session()
    Base = declarative_base()

class TablePersona(Personas.Base):
    __tablename__ = 'Personas'
    id = Column('id', Integer, primary_key=True)
    name = Column('name', String)
    first_name = Column('firstName', String)
    dni = Column('DNI', Integer)

    def __init__(self, first_name, name, dni):
        self.name = name
        self.first_name = first_name
        self.dni = dni`

    `def __repr__(self):
        return f'{self.apellido} {self.nombre} tiene DNI={self.dni}'


def vizualizar_por_dni(param):
    dni = Personas.sessionDB.query(TablePersona).filter(TablePersona.dni == param).all()
    res = dni
    if len(res) > 0:
        for i in res:
            return i
    else:
        return f'Persona con DNI: {param} no existe!'


def insertar(nombre, apellido, dni):
    persona = TablePersona(nombre, apellido, dni)
    Personas.sessionDB.add(persona)
    Personas.sessionDB.commit()


def main():
    Personas.Base.metadata.create_all(Personas.personas)

socket.server .py

import socket

from personas import vizualizar_por_dni


def server():
    # he creado server con "with" loop para olvider de ese .close()
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        server_address = (socket.gethostbyname(socket.gethostname()), 1234)
        s.bind(server_address)
        s.listen(10)
        print("Server address is: ip: {} port: {}".format(*server_address))
        connection, address = s.accept()
        with connection:
            print(f"Cliente {address} connected")
            while True:
                data = connection.recv(10000000)
                if not data:
                    print("No he recibido datas!")
                    break
                datas_recibido = data.decode()
                message_to_send = f'{vizualizar_por_dni(datas_recibido)}'.encode()
                if len(message_to_send) > 0:
                    connection.sendall(message_to_send)
                else:
                    message_reply = b'Ningun persona con ese DNI!'
                    connection.sendall(message_reply)

socket.client .py

import socket


def client():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect((socket.gethostname(), 1234))
        while True:
            usuario = input("Introduce el DNI: \n")
            message = usuario.encode()
            s.sendall(message)
            if s.sendall(message):
                print("the client had sent the message")
            data = s.recv(10000000)
            received_message = data.decode()
            if len(received_message) > 0:
                print(received_message)
            else:
                print("Nothing received")
            continuar = input('Quieres que continuar? S or N')
            if continuar.capitalize() == 'S':
                continue
            elif continuar.capitalize() == 'N':
                break
        print("Connection closed")

Sorry for the spanglish mix over there, i'm still learning python

alender
  • 13
  • 4

0 Answers0