Ensure server welcome message is read on client connect
This commit is contained in:
parent
0696008af8
commit
ba597eaf7f
1 changed files with 25 additions and 14 deletions
|
@ -6,7 +6,27 @@ from nntp.tiny.socket import Connection
|
||||||
from nntp.tiny.host import Host
|
from nntp.tiny.host import Host
|
||||||
from nntp.tiny.response import Response, ResponseCode
|
from nntp.tiny.response import Response, ResponseCode
|
||||||
|
|
||||||
|
class ClientException(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
class Client(Connection):
|
class Client(Connection):
|
||||||
|
RE_SPLIT = re.compile(r'\s+')
|
||||||
|
|
||||||
|
def _read_response(self):
|
||||||
|
line = self.readline()
|
||||||
|
|
||||||
|
if line == '':
|
||||||
|
return
|
||||||
|
|
||||||
|
parts = self.RE_SPLIT.split(line.rstrip(), 1)
|
||||||
|
|
||||||
|
if len(parts) == 0:
|
||||||
|
return
|
||||||
|
elif len(parts) == 1:
|
||||||
|
return Response(ResponseCode(int(parts[0])))
|
||||||
|
else:
|
||||||
|
return Response(ResponseCode(int(parts[0])), parts[1])
|
||||||
|
|
||||||
def __init__(self, host: str, port: int, tls: bool=False):
|
def __init__(self, host: str, port: int, tls: bool=False):
|
||||||
sock = socket.create_connection((host, port))
|
sock = socket.create_connection((host, port))
|
||||||
|
|
||||||
|
@ -26,21 +46,12 @@ class Client(Connection):
|
||||||
|
|
||||||
super().__init__(sock)
|
super().__init__(sock)
|
||||||
|
|
||||||
RE_SPLIT = re.compile(r'\s+')
|
response = self._read_response()
|
||||||
|
|
||||||
|
if response is None:
|
||||||
|
raise ClientException('Server not ready')
|
||||||
|
|
||||||
def request(self, *args):
|
def request(self, *args):
|
||||||
self.print(' '.join(args))
|
self.print(' '.join(args))
|
||||||
|
|
||||||
line = self.readline()
|
return self._read_response()
|
||||||
|
|
||||||
if line == '':
|
|
||||||
return
|
|
||||||
|
|
||||||
parts = self.RE_SPLIT.split(line.rstrip(), 1)
|
|
||||||
|
|
||||||
if len(parts) == 0:
|
|
||||||
return
|
|
||||||
elif len(parts) == 1:
|
|
||||||
return Response(ResponseCode(int(parts[0])))
|
|
||||||
else:
|
|
||||||
return Response(ResponseCode(int(parts[0])), parts[1])
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue