mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2026-09-11 03:05:59 +00:00
Added ability to upload large files
This commit is contained in:
+6
-13
@@ -21,7 +21,6 @@ class TcpClient:
|
||||
"""Connects to the specified IP and port number"""
|
||||
self.socket.connect((ip, port))
|
||||
self.connected = True
|
||||
self.socket.setblocking(False)
|
||||
|
||||
def close(self):
|
||||
"""Closes the connection"""
|
||||
@@ -34,18 +33,9 @@ class TcpClient:
|
||||
|
||||
# Ensure that only one thread can send data at once
|
||||
with self.lock:
|
||||
# Do not use .sendall:
|
||||
# "on error, an exception is raised, and there is no way to
|
||||
# determine how much data, if any, was successfully sent."
|
||||
while data:
|
||||
try:
|
||||
sent = self.socket.send(data)
|
||||
data = data[sent:]
|
||||
except BlockingIOError as e:
|
||||
if 'Errno 11' in str(e): # Error #11: Resource temporary unavailable
|
||||
time.sleep(0.1) # Sleep a bit waiting for the resource to be available
|
||||
else:
|
||||
raise e
|
||||
# Set blocking so it doesn't error
|
||||
self.socket.setblocking(True)
|
||||
self.socket.sendall(data)
|
||||
|
||||
def read(self, buffer_size):
|
||||
"""Reads (receives) the specified bytes from the connected peer"""
|
||||
@@ -55,6 +45,9 @@ class TcpClient:
|
||||
# Ensure it is not cancelled at first, so we can enter the loop
|
||||
self.cancelled = False
|
||||
|
||||
# Set non-blocking so it can be cancelled
|
||||
self.socket.setblocking(False)
|
||||
|
||||
with BinaryWriter() as writer:
|
||||
while writer.written_count < buffer_size:
|
||||
# Only do cancel if no data was read yet
|
||||
|
||||
Reference in New Issue
Block a user