-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport.py
80 lines (62 loc) · 2.44 KB
/
transport.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# python imports:
from abc import ABCMeta, abstractmethod
import logging
import ssl
import sys
from typing import Optional as Opt
# email_proto imports:
from util import BYTES
logger = logging.getLogger ( __name__ )
class Transport ( metaclass = ABCMeta ):
ssl_context: Opt[ssl.SSLContext] = None
def ssl_context_or_default_client ( self ) -> ssl.SSLContext:
if self.ssl_context is None:
self.ssl_context = ssl.create_default_context ( ssl.Purpose.SERVER_AUTH )
return self.ssl_context
def ssl_context_or_default_server ( self ) -> ssl.SSLContext:
if self.ssl_context is None:
self.ssl_context = ssl.create_default_context()
self.ssl_context.verify_mode = ssl.CERT_NONE
return self.ssl_context
class SyncTransport ( Transport ):
@abstractmethod
def read ( self ) -> bytes:
cls = type ( self )
raise NotImplementedError ( f'{cls.__module__}.{cls.__name__}.read()' )
@abstractmethod
def write ( self, data: BYTES ) -> None:
cls = type ( self )
raise NotImplementedError ( f'{cls.__module__}.{cls.__name__}.write()' )
@abstractmethod
def starttls_client ( self, server_hostname: str ) -> None:
cls = type ( self )
raise NotImplementedError ( f'{cls.__module__}.{cls.__name__}.starttls_client()' )
@abstractmethod
def starttls_server ( self ) -> None:
cls = type ( self )
raise NotImplementedError ( f'{cls.__module__}.{cls.__name__}.starttls_server()' )
@abstractmethod
def close ( self ) -> None:
cls = type ( self )
raise NotImplementedError ( f'{cls.__module__}.{cls.__name__}.close()' )
class AsyncTransport ( Transport ):
@abstractmethod
async def read ( self ) -> bytes:
cls = type ( self )
raise NotImplementedError ( f'{cls.__module__}.{cls.__name__}.read()' )
@abstractmethod
async def write ( self, data: BYTES ) -> None:
cls = type ( self )
raise NotImplementedError ( f'{cls.__module__}.{cls.__name__}.write()' )
@abstractmethod
async def starttls_client ( self, server_hostname: str ) -> None:
cls = type ( self )
raise NotImplementedError ( f'{cls.__module__}.{cls.__name__}.starttls_client()' )
@abstractmethod
async def starttls_server ( self ) -> None:
cls = type ( self )
raise NotImplementedError ( f'{cls.__module__}.{cls.__name__}.starttls_server()' )
@abstractmethod
async def close ( self ) -> None:
cls = type ( self )
raise NotImplementedError ( f'{cls.__module__}.{cls.__name__}.close()' )