Module CoffeeChat.coffeechat.Server.ServerSocket

Configuração do socket do servidor

Expand source code
"""Configuração do socket do servidor"""
import os
import threading


class ServerSocket(threading.Thread):
        """
        Suporta comunicações com um cliente conectado.

        Attributes:
                sc (socket.socket): Socket conectado.
                sockname (tuple): Endereço do socket do client.
                server (Server): Thread pai.
        """
        def __init__(self, sc, sockname, server):
                super().__init__()
                self.sc = sc
                self.sockname = sockname
                self.server = server

        def run(self):
                """
                Recebe dados do cliente conectado e transmite a mensagem para todos os outros clientes.
                Se o cliente saiu da conexão, fecha o socket conectado e remove a si mesmo da lista
                de threads ServerSocket no thread de servidor pai.
                """
                while True:
                        message = self.sc.recv(1024).decode('ascii')
                        if message:
                                print(f'{self.sockname} diz {message}')
                                self.server.broadcast(message, self.sockname)
                        else:
                                print(f'{self.sockname} fechou a conexao.')
                                self.sc.close()
                                self.server.remove_connection(self)
                                return

        def send(self, message):
                """
                Envia uma mensagem ao servidor conectado

                Args:
                        message (str): Mensagem a ser enviada.
                """
                self.sc.sendall(message.encode('ascii'))

Classes

class ServerSocket (sc, sockname, server)

Suporta comunicações com um cliente conectado.

Attributes

sc : socket.socket
Socket conectado.
sockname : tuple
Endereço do socket do client.
server : Server
Thread pai.

This constructor should always be called with keyword arguments. Arguments are:

group should be None; reserved for future extension when a ThreadGroup class is implemented.

target is the callable object to be invoked by the run() method. Defaults to None, meaning nothing is called.

name is the thread name. By default, a unique name is constructed of the form "Thread-N" where N is a small decimal number.

args is the argument tuple for the target invocation. Defaults to ().

kwargs is a dictionary of keyword arguments for the target invocation. Defaults to {}.

If a subclass overrides the constructor, it must make sure to invoke the base class constructor (Thread.init()) before doing anything else to the thread.

Expand source code
class ServerSocket(threading.Thread):
        """
        Suporta comunicações com um cliente conectado.

        Attributes:
                sc (socket.socket): Socket conectado.
                sockname (tuple): Endereço do socket do client.
                server (Server): Thread pai.
        """
        def __init__(self, sc, sockname, server):
                super().__init__()
                self.sc = sc
                self.sockname = sockname
                self.server = server

        def run(self):
                """
                Recebe dados do cliente conectado e transmite a mensagem para todos os outros clientes.
                Se o cliente saiu da conexão, fecha o socket conectado e remove a si mesmo da lista
                de threads ServerSocket no thread de servidor pai.
                """
                while True:
                        message = self.sc.recv(1024).decode('ascii')
                        if message:
                                print(f'{self.sockname} diz {message}')
                                self.server.broadcast(message, self.sockname)
                        else:
                                print(f'{self.sockname} fechou a conexao.')
                                self.sc.close()
                                self.server.remove_connection(self)
                                return

        def send(self, message):
                """
                Envia uma mensagem ao servidor conectado

                Args:
                        message (str): Mensagem a ser enviada.
                """
                self.sc.sendall(message.encode('ascii'))

Ancestors

  • threading.Thread

Methods

def run(self)

Recebe dados do cliente conectado e transmite a mensagem para todos os outros clientes. Se o cliente saiu da conexão, fecha o socket conectado e remove a si mesmo da lista de threads ServerSocket no thread de servidor pai.

Expand source code
def run(self):
        """
        Recebe dados do cliente conectado e transmite a mensagem para todos os outros clientes.
        Se o cliente saiu da conexão, fecha o socket conectado e remove a si mesmo da lista
        de threads ServerSocket no thread de servidor pai.
        """
        while True:
                message = self.sc.recv(1024).decode('ascii')
                if message:
                        print(f'{self.sockname} diz {message}')
                        self.server.broadcast(message, self.sockname)
                else:
                        print(f'{self.sockname} fechou a conexao.')
                        self.sc.close()
                        self.server.remove_connection(self)
                        return
def send(self, message)

Envia uma mensagem ao servidor conectado

Args

message : str
Mensagem a ser enviada.
Expand source code
def send(self, message):
        """
        Envia uma mensagem ao servidor conectado

        Args:
                message (str): Mensagem a ser enviada.
        """
        self.sc.sendall(message.encode('ascii'))