simple-socket-client 1.3__tar.gz → 1.5__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: simple-socket-client
3
- Version: 1.3
3
+ Version: 1.5
4
4
  Summary: Simple TCP socket client
5
5
  Home-page: https://github.com/webtoucher/simple-socket-client
6
6
  Author: Alexey Kuznetsov
@@ -35,7 +35,7 @@ $ pip install simple-socket-client
35
35
  Or you can add it as dependency in requirements.txt file of your python application:
36
36
 
37
37
  ```
38
- simple-socket-client~=1.3
38
+ simple-socket-client~=1.5
39
39
  ```
40
40
 
41
41
  ## Usage
@@ -44,7 +44,7 @@ simple-socket-client~=1.3
44
44
  from simple_socket_client import SimpleSocketClient
45
45
 
46
46
  client = SimpleSocketClient('192.168.0.2', 6666)
47
- client.connect()
47
+ client.connect(timeout=10)
48
48
 
49
49
  client.send('Test'.encode()) # if you don't need an answer
50
50
  answer = client.ask('Hi!'.encode())
@@ -16,7 +16,7 @@ $ pip install simple-socket-client
16
16
  Or you can add it as dependency in requirements.txt file of your python application:
17
17
 
18
18
  ```
19
- simple-socket-client~=1.3
19
+ simple-socket-client~=1.5
20
20
  ```
21
21
 
22
22
  ## Usage
@@ -25,7 +25,7 @@ simple-socket-client~=1.3
25
25
  from simple_socket_client import SimpleSocketClient
26
26
 
27
27
  client = SimpleSocketClient('192.168.0.2', 6666)
28
- client.connect()
28
+ client.connect(timeout=10)
29
29
 
30
30
  client.send('Test'.encode()) # if you don't need an answer
31
31
  answer = client.ask('Hi!'.encode())
@@ -1,7 +1,7 @@
1
1
  from setuptools import setup, find_packages
2
2
 
3
3
  setup(name='simple-socket-client',
4
- version='1.3',
4
+ version='1.5',
5
5
  url='https://github.com/webtoucher/simple-socket-client',
6
6
  license='BSD-3-Clause',
7
7
  author='Alexey Kuznetsov',
@@ -20,56 +20,69 @@ class SimpleSocketClientException(socket.error):
20
20
 
21
21
  class SimpleSocketClient(EventBus):
22
22
  def __init__(self, host: str, port: int):
23
+ super().__init__()
23
24
  self.__host = host
24
25
  self.__port = port
25
26
  self.__socket = None
26
27
  self.__thread = None
28
+ self.__thread_err = None
27
29
  self.__outgoing_messages = None
28
30
  self.__connected = False
29
31
  self.__keep_answer = False
30
32
 
31
- def connect(self) -> None:
32
- self.__thread = Thread(target=self.__threading, args=(self,))
33
+ def connect(self, timeout=None) -> None:
34
+ self.__thread_err = None
35
+ self.__thread = Thread(target=self.__threading, args=(self, timeout))
33
36
  self.__thread.start()
34
37
  while not self.__connected:
35
- pass
38
+ if self.__thread_err:
39
+ raise self.__thread_err
36
40
 
37
41
  def disconnect(self) -> None:
38
42
  self.__connected = False
39
43
 
40
44
  @staticmethod
41
- def __threading(client):
42
- client.__incoming_messages = queue.Queue()
43
- client.__outgoing_messages = queue.Queue()
44
- client.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
45
- if client.__socket.fileno() < 0:
46
- raise SimpleSocketClientException('Error with creating sockets')
47
- client.__socket.connect((client.__host, client.__port))
48
- client.__connected = True
49
- client.emit('connect', (client.__host, client.__port))
50
- while client.__connected:
45
+ def __threading(client, timeout):
46
+ try:
47
+ client.__incoming_messages = queue.Queue()
48
+ client.__outgoing_messages = queue.Queue()
49
+ client.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
50
+ if client.__socket.fileno() < 0:
51
+ raise SimpleSocketClientException('Problem with creating sockets')
52
+ client.__socket.settimeout(timeout)
51
53
  try:
52
- if client.__socket.fileno() > 0:
53
- message = client.__outgoing_messages.get_nowait()
54
- else:
55
- client.disconnect()
54
+ client.__socket.connect((client.__host, client.__port))
55
+ except TimeoutError as err:
56
+ raise SimpleSocketClientException('Connection timed out') from err
57
+ client.__socket.settimeout(None)
58
+ client.__connected = True
59
+ client.emit('connect', (client.__host, client.__port))
60
+ while client.__connected:
61
+ try:
62
+ if client.__socket.fileno() > 0:
63
+ message = client.__outgoing_messages.get_nowait()
64
+ else:
65
+ client.disconnect()
66
+ continue
67
+ except queue.Empty:
56
68
  continue
57
- except queue.Empty:
58
- continue
59
69
 
60
- try:
61
- if message:
62
- client.__socket.sendall(message)
63
- if client.__keep_answer:
64
- client.__receive_message()
65
- except ConnectionResetError:
66
- client.disconnect()
70
+ try:
71
+ if message:
72
+ client.__socket.sendall(message)
73
+ if client.__keep_answer:
74
+ client.__receive_message()
75
+ except ConnectionResetError:
76
+ client.disconnect()
77
+ except SimpleSocketClientException as err:
78
+ client.__thread_err = err
67
79
 
68
80
  client.emit('disconnect', (client.__host, client.__port))
69
81
  client.__incoming_messages = None
70
82
  client.__outgoing_messages = None
71
- client.__socket.close()
72
- client.__socket = None
83
+ if client.__socket:
84
+ client.__socket.close()
85
+ client.__socket = None
73
86
 
74
87
  def send(self, message: bytes) -> None:
75
88
  if not self.__connected:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: simple-socket-client
3
- Version: 1.3
3
+ Version: 1.5
4
4
  Summary: Simple TCP socket client
5
5
  Home-page: https://github.com/webtoucher/simple-socket-client
6
6
  Author: Alexey Kuznetsov
@@ -35,7 +35,7 @@ $ pip install simple-socket-client
35
35
  Or you can add it as dependency in requirements.txt file of your python application:
36
36
 
37
37
  ```
38
- simple-socket-client~=1.3
38
+ simple-socket-client~=1.5
39
39
  ```
40
40
 
41
41
  ## Usage
@@ -44,7 +44,7 @@ simple-socket-client~=1.3
44
44
  from simple_socket_client import SimpleSocketClient
45
45
 
46
46
  client = SimpleSocketClient('192.168.0.2', 6666)
47
- client.connect()
47
+ client.connect(timeout=10)
48
48
 
49
49
  client.send('Test'.encode()) # if you don't need an answer
50
50
  answer = client.ask('Hi!'.encode())