mirror of
https://github.com/LonamiWebs/Telethon.git
synced 2024-11-22 17:36:34 +03:00
Fix closed sockets not being recreated as they should
See http://stackoverflow.com/a/15958099
This commit is contained in:
parent
468033fa7e
commit
6550d83d42
|
@ -11,26 +11,29 @@ from telethon.utils import BinaryWriter
|
||||||
class TcpClient:
|
class TcpClient:
|
||||||
def __init__(self, proxy=None):
|
def __init__(self, proxy=None):
|
||||||
self.connected = False
|
self.connected = False
|
||||||
|
self.proxy = proxy
|
||||||
if proxy:
|
self._recreate_socket()
|
||||||
try:
|
|
||||||
import socks
|
|
||||||
self.socket = socks.socksocket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
self.socket.set_proxy(*proxy)
|
|
||||||
except (ImportError, SystemError):
|
|
||||||
print("Can't import PySocks, fallback to vanilla socket. "
|
|
||||||
"Proxy settings are ignored. "
|
|
||||||
"Try to install PySocks via pip")
|
|
||||||
proxy = None
|
|
||||||
|
|
||||||
if not proxy:
|
|
||||||
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
|
|
||||||
# Support for multi-threading advantages and safety
|
# Support for multi-threading advantages and safety
|
||||||
self.cancelled = Event() # Has the read operation been cancelled?
|
self.cancelled = Event() # Has the read operation been cancelled?
|
||||||
self.delay = 0.1 # Read delay when there was no data available
|
self.delay = 0.1 # Read delay when there was no data available
|
||||||
self.lock = Lock()
|
self.lock = Lock()
|
||||||
|
|
||||||
|
def _recreate_socket(self):
|
||||||
|
self.socket = None
|
||||||
|
if self.proxy:
|
||||||
|
try:
|
||||||
|
import socks
|
||||||
|
self.socket = socks.socksocket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
self.socket.set_proxy(*self.proxy)
|
||||||
|
except (ImportError, SystemError):
|
||||||
|
print("Can't import PySocks, fallback to vanilla socket. "
|
||||||
|
"Proxy settings are ignored. "
|
||||||
|
"Try to install PySocks via pip")
|
||||||
|
|
||||||
|
if not self.socket:
|
||||||
|
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
|
|
||||||
def connect(self, ip, port):
|
def connect(self, ip, port):
|
||||||
"""Connects to the specified IP and port number"""
|
"""Connects to the specified IP and port number"""
|
||||||
if not self.connected:
|
if not self.connected:
|
||||||
|
@ -42,6 +45,7 @@ class TcpClient:
|
||||||
if self.connected:
|
if self.connected:
|
||||||
self.socket.close()
|
self.socket.close()
|
||||||
self.connected = False
|
self.connected = False
|
||||||
|
self._recreate_socket()
|
||||||
|
|
||||||
def write(self, data):
|
def write(self, data):
|
||||||
"""Writes (sends) the specified bytes to the connected peer"""
|
"""Writes (sends) the specified bytes to the connected peer"""
|
||||||
|
|
|
@ -64,8 +64,7 @@ class TcpTransport:
|
||||||
return seq, body
|
return seq, body
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
if self.tcp_client.connected:
|
self.tcp_client.close()
|
||||||
self.tcp_client.close()
|
|
||||||
|
|
||||||
def cancel_receive(self):
|
def cancel_receive(self):
|
||||||
"""Cancels (stops) trying to receive from the
|
"""Cancels (stops) trying to receive from the
|
||||||
|
|
Loading…
Reference in New Issue
Block a user