simple-socket-client 1.4__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.4
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.4
38
+ simple-socket-client~=1.5
39
39
  ```
40
40
 
41
41
  ## Usage
@@ -44,7 +44,7 @@ simple-socket-client~=1.4
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.4
19
+ simple-socket-client~=1.5
20
20
  ```
21
21
 
22
22
  ## Usage
@@ -25,7 +25,7 @@ simple-socket-client~=1.4
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.4',
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',
@@ -25,52 +25,64 @@ class SimpleSocketClient(EventBus):
25
25
  self.__port = port
26
26
  self.__socket = None
27
27
  self.__thread = None
28
+ self.__thread_err = None
28
29
  self.__outgoing_messages = None
29
30
  self.__connected = False
30
31
  self.__keep_answer = False
31
32
 
32
- def connect(self) -> None:
33
- 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))
34
36
  self.__thread.start()
35
37
  while not self.__connected:
36
- pass
38
+ if self.__thread_err:
39
+ raise self.__thread_err
37
40
 
38
41
  def disconnect(self) -> None:
39
42
  self.__connected = False
40
43
 
41
44
  @staticmethod
42
- def __threading(client):
43
- client.__incoming_messages = queue.Queue()
44
- client.__outgoing_messages = queue.Queue()
45
- client.__socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
46
- if client.__socket.fileno() < 0:
47
- raise SimpleSocketClientException('Error with creating sockets')
48
- client.__socket.connect((client.__host, client.__port))
49
- client.__connected = True
50
- client.emit('connect', (client.__host, client.__port))
51
- 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)
52
53
  try:
53
- if client.__socket.fileno() > 0:
54
- message = client.__outgoing_messages.get_nowait()
55
- else:
56
- 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:
57
68
  continue
58
- except queue.Empty:
59
- continue
60
69
 
61
- try:
62
- if message:
63
- client.__socket.sendall(message)
64
- if client.__keep_answer:
65
- client.__receive_message()
66
- except ConnectionResetError:
67
- 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
68
79
 
69
80
  client.emit('disconnect', (client.__host, client.__port))
70
81
  client.__incoming_messages = None
71
82
  client.__outgoing_messages = None
72
- client.__socket.close()
73
- client.__socket = None
83
+ if client.__socket:
84
+ client.__socket.close()
85
+ client.__socket = None
74
86
 
75
87
  def send(self, message: bytes) -> None:
76
88
  if not self.__connected:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: simple-socket-client
3
- Version: 1.4
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.4
38
+ simple-socket-client~=1.5
39
39
  ```
40
40
 
41
41
  ## Usage
@@ -44,7 +44,7 @@ simple-socket-client~=1.4
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())