33 import SocketServer, threading
52 _server_running =
False
55 allow_reuse_address =
True
62 def __init__(self, server_address, RequestHandlerClass, thq=None, message=''):
64 SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass)
71 self.
logger = logging.getLogger(
'SP.TCPServer')
78 self.logger.info(
'info %s',self.
message)
88 self.socket.listen(self.request_queue_size)
89 MyThreaded_TCPServer_Parser._server_running=
True
121 self.
logger = logging.getLogger(
'SP.TCPServerHandler')
122 self.logger.debug(
"MyHandler_Parser in settup")
138 self.
logger = logging.getLogger(
'SP.TCPServerHandler')
141 self.logger.debug(
"Entry Handler_Parser thq: %s", self.server._thq)
144 self.
data = self.request.recv(1024).strip()
150 tab_data = self.data.rsplit(
' ')
151 self.logger.debug(
"tab_data: %s",tab_data)
166 list_stock = tab_data[2:]
168 self.logger.debug(
"method %s", method)
169 self.logger.debug(
"action %s", action)
170 self.logger.debug(
"list_stock %s", list_stock)
180 str_line = self.update_stock.get_string_data( list_stock, opt_parse =
False )
181 str_line=str_line[:-1]
182 self.request.send(str_line)
185 elif method ==
"FORCE":
189 str_line=self.update_stock.get_string_data( list_stock, opt_parse =
True )
190 str_line=str_line[:-1]
191 self.request.send(str_line)
208 for stock
in list_stock:
212 status = self.server._thq.InsertToQueue( stock,
"InstValue")
214 self.logger.info(
"Inserted to queue %s" % stock)
216 self.logger.error(
"Cannot insert to queue %s" % stock)
243 elif self.
data ==
'RELOAD':
245 print 'reload dictstocks'
252 list_new=self.dict_stocks.ReadStocks(
'dictstocks.txt')
254 print 'list new :', list_new
255 self.dict_stocks.make_print_stocks(
None,
True )
259 for stock
in list_new:
260 if self.dict_stocks.CheckOpen(stock):
261 self.server.thq.list_active_stock.append(stock)
263 self.server.thq.list_inactive_stock.append(stock)
267 for stock
in list_new:
269 self.request.send(
"Reloaded")
271 self.request.send(
"Nothing changed in dict_stocks")
273 elif tab_data[0] ==
'LAST_CSV_UPDATE':
276 print " len tab_data ", len(tab_data)
277 print " tab_data ", tab_data
280 if ( len(tab_data) >= 2 ):
283 print "Argument is missing"
284 self.request.send(
"Argument is missing")
295 stock_to_update = YahooAPI.Parser_YahooCSV ( self.dict_stocks, stock )
296 time_t = stock_to_update.LastCSV()
298 self.request.send(str(time_t))
302 elif tab_data[0] ==
'UPDATE_CSV':
304 print 'UPDATE_CSV tab_data ',tab_data
306 if ( len(tab_data) >= 2 ):
309 print "Argument is missing"
310 self.request.send(
"Argument is missing")
315 stock_to_update = YahooAPI.Parser_YahooCSV ( self.dict_stocks, stock )
318 time_t=stock_to_update.LastCSV()
320 retour = stock_to_update.UpdateCSV( time_t )
322 self.request.send(
"Updated ret:"+str(retour) )
329 elif method ==
'BYE':
331 print "Stop thread_handle_queue first"
333 MyThreaded_TCPServer_Parser._server_running =
False
334 print "threading enumerate"
336 threading.enumerate()
337 self.request.send(
"Stopping Server")
341 self.request.send(
"RUNNING")
346 for stock
in list_stock:
348 if self.dict_stocks.dictstocks[stock][
'lock'] ==
True :
364 address_server = ( host, port )
368 print "address_server ", address_server
370 thq_for_server =
None
374 print "thq_for_server"
376 print "thq not given"
378 print "run TCPServer"
385 , thq_for_server,
'Welcome to Server_Portfolio.' )
388 while MyThreaded_TCPServer_Parser._server_running:
389 server.handle_request()
392 if __name__ ==
"__main__":
394 print "Version main of SocketServer, autoparser not running"
396 logging.basicConfig(level=logging.DEBUG)
397 logger = logging.getLogger(
'SP')
398 main_logger = logging.getLogger(
"SP.main")
404 print "host, port ", host,port
405 address = ( host, port )
424 server_thread = threading.Thread(target=run_MyServer, name=
"t-server",
428 server_thread.setDaemon(
False)
429 print "start server_thread "
430 server_thread.start()
432 print "Elapsed Time: %s" % (time.time() - start)
435 while (time.time()-start) < 2:
438 print " enumerate ", threading.enumerate()
439 print " time passed..."
440 print " server running ", MyThreaded_TCPServer_Parser._server_running
442 print "\nServer loop running in thread:", server_thread.name
Define class UpdateStocks and ParserFactory.
def server_activate
Called by constructor to activate the server.
def run_MyServer
Function to initialize and run the server.
Container of all Stocks objects, it also reads the static stocks configuration file "dictstocks...
Global variables for configuration: paths, TCP ports and generic definitions.
_thq
Save a link to the queue for interacting (ex:force update) available from the handler as self...
Update data of a list of Stock's (only one action by call is possible).
def AllStocksLock
check if at least one stock of the list is still locked
Make a proper handler class to respond to the message.
Define singleton class DictionaryStocks, act as the main container of Stocks objects.