2
0
mirror of https://github.com/LonamiWebs/Telethon synced 2025-08-22 01:49:45 +00:00
telethon/network/tcp_client.py

27 lines
776 B
Python
Raw Normal View History

2016-08-28 13:43:00 +02:00
# Python rough implementation of a C# TCP client
import socket
class TcpClient:
def __init__(self):
self.connected = False
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def connect(self, ip, port):
2016-08-28 13:43:00 +02:00
"""Connects to the specified IP and port number"""
self.socket.connect((ip, port))
self.connected = True
def close(self):
2016-08-28 13:43:00 +02:00
"""Closes the connection"""
self.socket.close()
self.connected = False
def write(self, data):
2016-08-28 13:43:00 +02:00
"""Writes (sends) the specified bytes to the connected peer"""
self.socket.sendall(data)
def read(self, buffer_size):
2016-08-28 13:43:00 +02:00
"""Reads (receives) the specified bytes from the connected peer"""
return self.socket.recv(buffer_size)