Skip to main content

why is creating multiple databases when I interact with socket.server?

  • 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



source https://stackoverflow.com/questions/76124674/why-is-creating-multiple-databases-when-i-interact-with-socket-server

Comments

Popular posts from this blog

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console...

How to split a rinex file if I need 24 hours data

Trying to divide rinex file using the command gfzrnx but getting this error. While doing that getting this error msg 'gfzrnx' is not recognized as an internal or external command Trying to split rinex file using the command gfzrnx. also install'gfzrnx'. my doubt is I need to run this program in 'gfzrnx' or in 'cmdprompt'. I am expecting a rinex file with 24 hrs or 1 day data.I Have 48 hrs data in RINEX format. Please help me to solve this issue. source https://stackoverflow.com/questions/75385367/how-to-split-a-rinex-file-if-i-need-24-hours-data