ServerPortfolio  2.0
Python parsers and server
 All Classes Namespaces Files Functions Variables Properties Pages
ModClientTCP.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 ## @package serverportfolio.ModClientTCP
4 # @brief Make a class for sending new data to the server of ROOT_application.
5 #
6 # A simple TCP client using module socket
7 #
8 # Last Changed $Id: ModClientTCP.py 13 2015-04-12 19:45:14Z michael $
9 
10 import socket
11 import logging, sys
12 
13 from GlobalDicts import HOST,PORT_GUI
14 
15 ## @class TCPClient
16 # @brief Make a connection to the root application server to send directly new data.
17 # do not accept answer from the server.\n
18 # Test is listening (correct connect function) to do
19 class TCPClient:
20 
21  def __init__(self):
22  self.logger = logging.getLogger( "SP.TCPClient")
23  self.logger.debug("Constructor TCPClient PORT to send %d" % PORT_GUI)
24 
25  # can set only once ? seems to need every time it is closed ?
26  # Address Family : AF_INET (this is IP version 4 or IPv4)
27  # Type : SOCK_STREAM (this means connection oriented TCP protocol)
28  #self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
29 
30  ## default true at creation, SendData will send back false if the connection is impossible.
31  # now tested by Connect every time
32  self.active = False #True #False
33 
34  def GetActivity(self):
35  return self.active
36 
37  ## Try to connect to the server
38  #
39  # connection is opened at the end ??
40  #
41  # @param only_check: if True try to connect and close the socket and return the status
42  def Connect(self, only_check=False):
43 
44  # Create a socket (SOCK_STREAM means a TCP socket)
45  # seems to reassign after each call to close() ??
46  try :
47  self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
48  # all function in socket throw socket.error
49  except socket.error, msg:
50  #print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
51  self.logger.info('Failed to create socket. Error code: %d Error message : %d ' % (msg[0], msg[1]))
52 
53  # Connect to server before sending data
54  try:
55  self.sock.connect((HOST, PORT_GUI))
56  self.logger.debug("OK for connection to %d" % PORT_GUI)
57  # here root server crash, if necessary to close, should use finally
58  if only_check:
59  self.logger.debug("ONLY_CHECK, close the socket")
60  self.sock.close()
61 
62  return True
63 
64  # got exception if connection impossible (not running)
65  except socket.error, ex:
66  self.sock.close()
67  #print "Get exception socket.error in connect(), no connection possible"
68  self.logger.info('Failed in socket.connect. Error code: %d, Error message : %s' % (ex[0], ex[1]) )
69  # could raise exception if caught by caller
70  return False
71 
72  # should not be necessary, keep for testing
73  except ex:
74  self.sock.close()
75  self.logger.error("Get a base exception in connect(), SHOULD NOT HAPPEN. no connection possible")
76  return False
77 
78  ## Connect to the server
79  # send the data
80  # no answer at this point
81  # Close the socket
82  def SendData ( self,data ):
83  self.logger.debug("Entry SendData")
84  self.logger.debug("Data to send %s" % data)
85 
86  status = self.Connect()
87  self.logger.debug("status %d" % status)
88  if status == False :
89  self.logger.info("GUI Server NOT RUNNING")
90  return False;
91 
92  # alternative sendall() return None
93  # append " END" for marking the end of the message, expected by TCPServer
94  try :
95  nb_bytes_send = self.sock.send(data + " END")
96  self.logger.debug("nb_bytes_send %d" % nb_bytes_send)
97  except socket.error, ex:
98  self.logger.error("Get exception socket.error in connect(), no connection possible")
99  self.logger.error('Failed in socket.coonect. Error code: ' + str(ex[0]) + ' , Error message : ' + ex[1])
100  return False
101 
102  # not used for the moment
103  # Receive data from the server and shut down
104  #received = sock.recv(1024)
105  self.sock.close()
106  return True
107  # something send back from server gui ?
108  #return received
109 
110 #################### main function ##########
111 if __name__ == "__main__":
112 
113  # should make some test ??
114  #data = " ".join(sys.argv[1:])
115 
116  # END added in SendData
117  data="DJ,2014-09-22 22:08:09,17172.680000,74298798.000000"
118  #data="1 END" #crash but send the message, certainly in creation DataStock
119  #print "data ", data
120  tcp = TCPClient()
121 
122  # test connection manually, could be in __init__
123  # need to close
124  #tcp.active = tcp.Connect( only_check=True )
125  #tcp.sock.close()
126 
127  #print "after tcp.Connect, only_check=True"
128  print "GetActivity ", tcp.GetActivity()
129 
130  #tcp.SendData( data )
131 
132  #tcp.SendData( data )
133  ## Receive data from the server and shut down
134  #received = sock.recv(1024)
135  #sock.close()
136  received = tcp.SendData( data )
137 
138  print "Sent: %s" % data
139  print "Received: %s" % received
140  # version for web application
141  #print "%s" % received
Make a connection to the root application server to send directly new data.
Definition: ModClientTCP.py:19
active
default true at creation, SendData will send back false if the connection is impossible.
Definition: ModClientTCP.py:32
def SendData
Connect to the server send the data no answer at this point Close the socket.
Definition: ModClientTCP.py:82
def Connect
Try to connect to the server.
Definition: ModClientTCP.py:42