2016-08-28 14:43:00 +03:00
|
|
|
# Python rough implementation of a C# TCP client
|
2016-08-26 13:58:53 +03:00
|
|
|
import socket
|
2016-09-09 12:47:37 +03:00
|
|
|
import time
|
2016-10-03 10:53:41 +03:00
|
|
|
from datetime import datetime, timedelta
|
2017-06-09 12:42:39 +03:00
|
|
|
from io import BytesIO, BufferedWriter
|
2017-05-09 20:01:44 +03:00
|
|
|
from threading import Event, Lock
|
2017-08-23 00:12:32 +03:00
|
|
|
import errno
|
2016-09-09 12:47:37 +03:00
|
|
|
|
2017-05-21 14:02:54 +03:00
|
|
|
from ..errors import ReadCancelledError
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
|
|
|
|
class TcpClient:
|
2017-03-20 19:16:34 +03:00
|
|
|
def __init__(self, proxy=None):
|
2017-05-30 11:40:33 +03:00
|
|
|
self._proxy = proxy
|
2017-08-29 14:35:56 +03:00
|
|
|
self._socket = None
|
2017-05-09 20:05:14 +03:00
|
|
|
|
2017-08-29 14:49:41 +03:00
|
|
|
def _recreate_socket(self, mode):
|
2017-05-30 12:42:14 +03:00
|
|
|
if self._proxy is None:
|
2017-08-29 14:49:41 +03:00
|
|
|
self._socket = socket.socket(mode, socket.SOCK_STREAM)
|
2017-05-30 12:42:14 +03:00
|
|
|
else:
|
2017-05-30 11:40:33 +03:00
|
|
|
import socks
|
2017-08-29 14:49:41 +03:00
|
|
|
self._socket = socks.socksocket(mode, socket.SOCK_STREAM)
|
2017-06-05 07:04:01 +03:00
|
|
|
if type(self._proxy) is dict:
|
|
|
|
self._socket.set_proxy(**self._proxy)
|
|
|
|
else: # tuple, list, etc.
|
|
|
|
self._socket.set_proxy(*self._proxy)
|
2017-05-30 11:40:33 +03:00
|
|
|
|
2017-06-22 20:21:33 +03:00
|
|
|
def connect(self, ip, port, timeout):
|
|
|
|
"""Connects to the specified IP and port number.
|
|
|
|
'timeout' must be given in seconds
|
|
|
|
"""
|
2017-04-29 21:57:08 +03:00
|
|
|
if not self.connected:
|
2017-08-29 14:49:41 +03:00
|
|
|
if ':' in ip: # IPv6
|
2017-09-02 20:14:11 +03:00
|
|
|
mode, address = socket.AF_INET6, (ip, port, 0, 0)
|
2017-08-29 14:49:41 +03:00
|
|
|
else:
|
2017-09-02 20:14:11 +03:00
|
|
|
mode, address = socket.AF_INET, (ip, port)
|
|
|
|
|
|
|
|
self._recreate_socket(mode)
|
|
|
|
self._socket.settimeout(timeout)
|
|
|
|
self._socket.connect(address)
|
2017-08-29 14:35:56 +03:00
|
|
|
|
|
|
|
def _get_connected(self):
|
|
|
|
return self._socket is not None
|
|
|
|
|
|
|
|
connected = property(fget=_get_connected)
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
def close(self):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Closes the connection"""
|
2017-08-29 14:35:56 +03:00
|
|
|
try:
|
|
|
|
if self.connected:
|
2017-08-23 00:12:32 +03:00
|
|
|
self._socket.shutdown(socket.SHUT_RDWR)
|
|
|
|
self._socket.close()
|
2017-08-29 14:35:56 +03:00
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.ENOTCONN:
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
self._socket = None
|
2016-08-26 13:58:53 +03:00
|
|
|
|
|
|
|
def write(self, data):
|
2016-08-28 14:43:00 +03:00
|
|
|
"""Writes (sends) the specified bytes to the connected peer"""
|
2016-09-10 11:17:15 +03:00
|
|
|
|
2017-09-02 19:49:29 +03:00
|
|
|
# TODO Check whether the code using this has multiple threads calling
|
|
|
|
# .write() on the very same socket. If so, have two locks, one for
|
|
|
|
# .write() and another for .read().
|
2017-09-02 20:14:11 +03:00
|
|
|
#
|
|
|
|
# TODO Timeout may be an issue when sending the data, Changed in v3.5:
|
|
|
|
# The socket timeout is now the maximum total duration to send all data.
|
2017-09-02 19:49:29 +03:00
|
|
|
try:
|
2017-09-02 20:14:11 +03:00
|
|
|
self._socket.sendall(data)
|
2017-09-02 19:49:29 +03:00
|
|
|
except BrokenPipeError:
|
|
|
|
self.close()
|
|
|
|
raise
|
2016-08-26 13:58:53 +03:00
|
|
|
|
2017-06-09 12:42:39 +03:00
|
|
|
def read(self, size, timeout=timedelta(seconds=5)):
|
|
|
|
"""Reads (receives) a whole block of 'size bytes
|
|
|
|
from the connected peer.
|
|
|
|
|
|
|
|
A timeout can be specified, which will cancel the operation if
|
|
|
|
no data has been read in the specified time. If data was read
|
|
|
|
and it's waiting for more, the timeout will NOT cancel the
|
|
|
|
operation. Set to None for no timeout
|
|
|
|
"""
|
2017-09-02 20:14:11 +03:00
|
|
|
# TODO Remove the timeout from this method, always use previous one
|
2017-09-02 19:49:29 +03:00
|
|
|
with BufferedWriter(BytesIO(), buffer_size=size) as buffer:
|
|
|
|
bytes_left = size
|
|
|
|
while bytes_left != 0:
|
2017-09-02 20:14:11 +03:00
|
|
|
partial = self._socket.recv(bytes_left)
|
|
|
|
if len(partial) == 0:
|
|
|
|
self.close()
|
|
|
|
raise ConnectionResetError(
|
|
|
|
'The server has closed the connection.')
|
2017-09-02 19:49:29 +03:00
|
|
|
|
2017-09-02 20:14:11 +03:00
|
|
|
buffer.write(partial)
|
|
|
|
bytes_left -= len(partial)
|
2017-09-02 19:49:29 +03:00
|
|
|
|
|
|
|
# If everything went fine, return the read bytes
|
|
|
|
buffer.flush()
|
|
|
|
return buffer.raw.getvalue()
|