Telethon/network/tcp_client.py
Lonami 6b8a347426 Several updates, fixes and additions (TcpClient, MtProto...)
README.md was updated to reflect more useful information
More errors from the official Telegrm website have been added
MtProtoSender now handles updates (and doesn't crash!)
Fixes on TcpClient to be able to receive whole large packets
Updated scheme.tl to the layer 55
Session is now saved more often (to prevent damages from crashes)
Fixes to the code generator (generated invalid code for reading "bytes")
2016-09-06 18:54:49 +02:00

34 lines
1.0 KiB
Python
Executable File

# 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):
"""Connects to the specified IP and port number"""
self.socket.connect((ip, port))
self.connected = True
def close(self):
"""Closes the connection"""
self.socket.close()
self.connected = False
def write(self, data):
"""Writes (sends) the specified bytes to the connected peer"""
self.socket.sendall(data)
def read(self, buffer_size):
"""Reads (receives) the specified bytes from the connected peer"""
# TODO improve (don't cast to list, use a mutable byte list instead or similar, see recv_into)
result = []
while len(result) < buffer_size:
left_data = buffer_size - len(result)
partial = self.socket.recv(left_data)
result.extend(list(partial))
return bytes(result)