Skip to content

Instantly share code, notes, and snippets.

@drgarcia1986
Created February 17, 2014 23:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save drgarcia1986/9061705 to your computer and use it in GitHub Desktop.
Save drgarcia1986/9061705 to your computer and use it in GitHub Desktop.
Classe feita em Python que encapsula um cliente UDP enviando e recebendo mensagens de um servidor.
__author__ = 'diego.garcia'
import socket
class UDPClient:
__IP = '255.255.255.255'
__PORT = 49152
__udp_cli = ''
def __init__(self):
self.__udp_cli = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
self.__udp_cli.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True)
self.__udp_cli.settimeout(5000)
def send_msg(self, msg):
self.__udp_cli.sendto(bytes(msg, "utf-8"), (self.__IP, self.__PORT))
return self.__udp_cli.recv(256).decode("UTF-8")
def set_port(self, port):
self.__PORT = port
def close(self):
self.__udp_cli.close()
__author__ = 'diego.garcia'
import udp_client
udpClient = udp_client.UDPClient()
print('Para sair use "fim"')
msg = input('Digite a mensagem a ser enviada\n')
while msg != 'fim':
print(udpClient.send_msg(msg))
msg = input('Digite a mensagem a ser enviada\n')
udpClient.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment