2424 # Python 2
2525 import SocketServer as socketserver
2626
27- thesocket = None
28-
2927class ThreadedTCPRequestHandler (socketserver .BaseRequestHandler ):
3028
3129 def handle (self ):
3230 print ("=== socket opened ===" )
33- global thesocket
34- thesocket = self .request
3531 while True :
3632 try :
3733 received = self .request .recv (4096 ).decode ('utf-8' )
@@ -77,19 +73,19 @@ def handle(self):
7773 cmd = '["ex","call append(\\ "$\\ ",\\ "added1\\ ")"]'
7874 cmd += '["ex","call append(\\ "$\\ ",\\ "added2\\ ")"]'
7975 print ("sending: {}" .format (cmd ))
80- thesocket .sendall (cmd .encode ('utf-8' ))
76+ self . request .sendall (cmd .encode ('utf-8' ))
8177 response = "ok"
8278 elif decoded [1 ] == 'eval-works' :
8379 # Send an eval request. We ignore the response.
8480 cmd = '["eval","\\ "foo\\ " . 123", -1]'
8581 print ("sending: {}" .format (cmd ))
86- thesocket .sendall (cmd .encode ('utf-8' ))
82+ self . request .sendall (cmd .encode ('utf-8' ))
8783 response = "ok"
8884 elif decoded [1 ] == 'eval-fails' :
8985 # Send an eval request that will fail.
9086 cmd = '["eval","xxx", -2]'
9187 print ("sending: {}" .format (cmd ))
92- thesocket .sendall (cmd .encode ('utf-8' ))
88+ self . request .sendall (cmd .encode ('utf-8' ))
9389 response = "ok"
9490 elif decoded [1 ] == 'eval-result' :
9591 # Send back the last received eval result.
@@ -105,14 +101,12 @@ def handle(self):
105101
106102 encoded = json .dumps ([decoded [0 ], response ])
107103 print ("sending: {}" .format (encoded ))
108- thesocket .sendall (encoded .encode ('utf-8' ))
104+ self . request .sendall (encoded .encode ('utf-8' ))
109105
110106 # Negative numbers are used for "eval" responses.
111107 elif decoded [0 ] < 0 :
112108 last_eval = decoded
113109
114- thesocket = None
115-
116110class ThreadedTCPServer (socketserver .ThreadingMixIn , socketserver .TCPServer ):
117111 pass
118112
0 commit comments