Tech With Tim Logo
Go back

Sending/Receiving Information

Creating a Network Class

In this video I show you how to create a basic class capable of connecting to our server and sending/receiving information. We will use this class later to send information about the state of our player.

Network Class Code

import socket


class Network:
    def __init__(self):
        self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server = "SERVER_IP"
        self.port = 5555
        self.addr = (self.server, self.port)
        self.id = self.connect()
        print(self.id)

    def connect(self):
        try:
            self.client.connect(self.addr)
            return self.client.recv(2048).decode()
        except:
            pass

    def send(self, data):
        try:
            self.client.send(str.encode(data))
            return self.client.recv(2048).decode()
        except socket.error as e:
            print(e)
Design & Development by Ibezio Logo