@@ -101,11 +101,11 @@ def start(self):
101101class Tube (object ):
102102 def __init__ (self , _sock ):
103103 self ._sock = _sock
104- self .buf = ''
104+ self .buf = b ''
105105
106106 def readline (self ):
107107 while True :
108- i = self .buf .find ('\n ' )
108+ i = self .buf .find (b '\n ' )
109109 if i >= 0 :
110110 result , self .buf = self .buf [:i + 1 ], self .buf [i + 1 :]
111111 return result
@@ -194,7 +194,7 @@ def handle_client(self, sock,addr,idx):
194194 wrapped_sock .shutdown (socket .SHUT_WR )
195195 wrapped_sock .close ()
196196 except Exception as e :
197- f .write (traceback .format_exc ())
197+ f .write (traceback .format_exc (). encode ( 'utf-8' ) )
198198 f .flush ()
199199 f .close ()
200200
@@ -210,7 +210,7 @@ def recvline(self):
210210 return l
211211 self .f .flush ()
212212 self .f .write (l )
213- sys .stdout .write (self .host + ': ' + l )
213+ sys .stdout .write (self .host + ': ' + l . decode ( 'utf-8' ) )
214214 return l
215215
216216 @staticmethod
@@ -240,7 +240,7 @@ def handle(self, **kwargs):
240240 self .f .write (request )
241241 self .f .flush ()
242242 request = self .sock .read ()
243- self .sock .write ("HTTP/1.1 200 OK\r \n Content-Length: 0\r \n \r \n " )
243+ self .sock .write (b "HTTP/1.1 200 OK\r \n Content-Length: 0\r \n \r \n " )
244244
245245class AnonFileHandler (ConnectionHandler ):
246246 def handle (self , logfile = '' ):
@@ -252,7 +252,7 @@ def handle(self, logfile=''):
252252 file_url = ('https' if LOG_VIEWER_HTTPS else 'http' ) + '://' + FQDN + ':' + str (LOG_VIEWER_PORT ) + os .sep + logfile [len (LOG_DIR )+ 1 :]
253253 print ('Finished receiving file %s' % (logfile ,))
254254 print ('Available at %s' % (file_url ,))
255- self .sock .write (file_url + '\n ' )
255+ self .sock .write (( file_url + '\n ' ). encode ( 'utf-8' ) )
256256
257257 @staticmethod
258258 def get_file_ext ():
@@ -263,32 +263,38 @@ class SmtpHandler(ConnectionHandler):
263263 def handle (self , ** kwargs ):
264264 print (self .host + ': RX begin >>>>' )
265265
266- self .sock .send ('220 ' + FQDN + ' ESMTP Postfix\r \n ' )
266+ self .sock .send (( b '220 ' + FQDN . encode ( 'ascii' ) + b ' ESMTP Postfix\r \n ') )
267267 self .recvline ()
268- self .sock .send ('250 ' + FQDN + ', I am glad to meet you\r \n ' )
268+ self .sock .send (b '250 ' + FQDN . encode ( 'ascii' ) + b ', I am glad to meet you\r \n ' )
269269
270270 while True :
271271 l = self .recvline ()
272272 if l == None :
273273 print (self .host + ': Socket closed <<<<' )
274274 return
275- if l .startswith ('DATA' ):
276- self .sock .send ('354 End data with <CR><LF>.<CR><LF>\r \n ' )
275+ if l .startswith (b 'DATA' ):
276+ self .sock .send (b '354 End data with <CR><LF>.<CR><LF>\r \n ' )
277277 break
278- if l .startswith ('QUIT' ):
278+ if l .startswith (b 'QUIT' ):
279279 print (self .host + ': Successfully RX <<<<' )
280280 return
281- self .sock .send ('250 Ok\r \n ' )
281+ self .sock .send (b '250 Ok\r \n ' )
282282
283283 while True :
284284 l = self .recvline ()
285- if l == '.\r \n ' :
285+ if l == None :
286+ print (self .host + ': Socket closed <<<<' )
287+ return
288+ if l == b'.\r \n ' :
286289 break
287- self .sock .send ('250 Ok: queued as 12345\r \n ' )
290+ self .sock .send (b '250 Ok: queued as 12345\r \n ' )
288291
289292 while True :
290293 l = self .recvline ()
291- if l .startswith ('QUIT' ):
294+ if l == None :
295+ print (self .host + ': Socket closed <<<<' )
296+ return
297+ if l .startswith (b'QUIT' ):
292298 break
293299 print (self .host + ': Successfully RX <<<<' )
294300
0 commit comments