How to make more than one type of request to the Python server?

Asked

Viewed 92 times

0

I separated using a function, but the code does not receive the 'date'.

COMPLETE SERVER CODE:

import socket
import json
import sys

dicty = {
    'the': 'o, a, os, as',
    'hello': 'ola',
    'hi': 'oi',
    'good': 'bom',
    'ugly': 'feio',
    'bad': 'ruim, mal',
    'pretty': 'lindo(a)',
    'happy': 'feliz',
    'sad': 'triste',
    'yes': 'sim',
    'no': 'nao'
}



def buscar():
    data, adress = sock.recvfrom(4000)

    if data.decode('ascii') in dicty:
        sock.sendto(dicty[data.decode('ascii')].encode('ascii'), adress)
    else:
        sock.sendto(('0'.encode('ascii')), adress)

port = 5000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = socket.gethostbyname(socket.gethostname())
sock.bind((host, port))

dado, adress = sock.recvfrom(3000)
while True:
    if dado.decode('ascii') == 'Buscar':

        buscar()

    if dado.decode('ascii') != 'Buscar':

        sys.exit(0)

COMPLETE CLIENT CODE:

import socket
from tkinter import *
import sys

port = 5000
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = socket.gethostbyname(socket.gethostname())
sock.connect((host,port))

def buscar(): #função de buscar palavras
               sock.sendto(('Buscar'.encode('ascii')), (host,port))
               sock.sendto((e1.get().encode('ascii')), (host, port))
               data, adress = sock.recvfrom(4000)
               if data.decode('ascii')=='0':
                   lb2["text"] = 'Esse nome nao existe em nosso dicionario. Ajude-nos cadastrando uma nova palavra'
               else:
                   lb2["text"] = data.decode('ascii')

janela = Tk()
janela.geometry("900x500+100+100")
janela.title("DICIONARIO COLABORATIVO")

lb1 = Label(text="DICIONARIO INGLES-PORTUGUES") #titulo
lb1.place(x=4, y =30)

btn1 = Button(text="Buscar", command=buscar) #botao de busca
btn1.place(x=4, y=100)

lb2 = Label(text="") #local do significado
lb2.place(x=200, y=90)

lb3 = Label(text="Significado")
lb3.place(x=200,y=50)

e1 = Entry(janela) #recebe palavra
e1.place(x=4, y=50)

btn3 = Button(text='SAIR', command=janela.quit)
btn3.place(x=300,y=300)

janela.mainloop()

No errors. The server simply waits for the client to send something eternally.

  • Why you have "recvfrom(3000)" and also "recvfrom(4000)" on the server ? This will crash the program...but I think the first error is that the "bind" of port 3000 is missing in the client.

1 answer

0

I did it that way, but it doesn’t work:

dado, adress = sock.recvfrom(3000)
while True:
    if dado.decode('ascii') == 'Buscar':
        data, adress = sock.recvfrom(4000)

        if data.decode('ascii') in dicty:
            sock.sendto(dicty[data.decode('ascii')].encode('ascii'), adress)
        else:
            sock.sendto(('0'.encode('ascii')), adress)

        sock.bind((host, port))

    if dado.decode('ascii') != 'Buscar':

        data2, adress = sock.recvfrom(4000)

        if data2.decode('ascii') == '0':
            sock.sendto('SAIR'.encode('ascii'), adress)
            sys.exit(0)

The program does not receive the date. The client, in this part, is like this:

def buscar(): #função de buscar palavras
               sock.sendto(('BUSCAR'.encode('ascii')), (host,port))
               sock.sendto((e1.get().encode('ascii')), (host, port))
               data, adress = sock.recvfrom(4000)
               if data.decode('ascii')=='0':
                   lb2["text"] = 'Esse nome nao existe em nosso dicionario. Ajude-nos cadastrando uma nova palavra'
               else:
                   lb2["text"] = data.decode('ascii')

"def fetch()" is called by a button.

  • First, @Diego Cândido, try not to publish new answers as an alternative to update the content of your question. Edit it, stackoverflow provides this option. And please, you could provide the whole code followed by the error that returns?

  • All right. I updated!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.