@@ -56,6 +56,17 @@ async def echo_client_streams(reader, writer):
56
56
writer .close ()
57
57
58
58
59
+ class EchoProtocol (asyncio .Protocol ):
60
+ def connection_made (self , transport ):
61
+ self .transport = transport
62
+
63
+ def connection_lost (self , exc ):
64
+ self .transport = None
65
+
66
+ def data_received (self , data ):
67
+ self .transport .write (data )
68
+
69
+
59
70
async def print_debug (loop ):
60
71
while True :
61
72
print (chr (27 ) + "[2J" ) # clear screen
@@ -67,6 +78,7 @@ async def print_debug(loop):
67
78
parser = argparse .ArgumentParser ()
68
79
parser .add_argument ('--uvloop' , default = False , action = 'store_true' )
69
80
parser .add_argument ('--streams' , default = False , action = 'store_true' )
81
+ parser .add_argument ('--proto' , default = False , action = 'store_true' )
70
82
parser .add_argument ('--addr' , default = '127.0.0.1:25000' , type = str )
71
83
parser .add_argument ('--print' , default = False , action = 'store_true' )
72
84
args = parser .parse_args ()
@@ -100,6 +112,10 @@ async def print_debug(loop):
100
112
print ('serving on: {}' .format (addr ))
101
113
102
114
if args .streams :
115
+ if args .proto :
116
+ print ('cannot use --stream and --proto simultaneously' )
117
+ exit (1 )
118
+
103
119
print ('using asyncio/streams' )
104
120
if unix :
105
121
coro = asyncio .start_unix_server (echo_client_streams ,
@@ -108,6 +124,17 @@ async def print_debug(loop):
108
124
coro = asyncio .start_server (echo_client_streams ,
109
125
* addr , loop = loop )
110
126
srv = loop .run_until_complete (coro )
127
+ elif args .proto :
128
+ if args .streams :
129
+ print ('cannot use --stream and --proto simultaneously' )
130
+ exit (1 )
131
+
132
+ print ('using simple protocol' )
133
+ if unix :
134
+ coro = loop .create_unix_server (EchoProtocol , addr )
135
+ else :
136
+ coro = loop .create_server (EchoProtocol , * addr )
137
+ srv = loop .run_until_complete (coro )
111
138
else :
112
139
print ('using sock_recv/sock_sendall' )
113
140
loop .create_task (echo_server (loop , addr , unix ))
0 commit comments