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

0

I've separated using a function, but the code does not get 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 CUSTOMER 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()

There are no mistakes. The server simply waits for the client to send something forever.

    
asked by anonymous 05.10.2018 / 13:18

1 answer

0

I did it that way, but it does not 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, looks 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')

The "fet fetch ()" is called by a button.

    
05.10.2018 / 14:02