ServerPortfolio  2.0
Python parsers and server
 All Classes Namespaces Files Functions Variables Properties Pages
SocketServer_Client.py
Go to the documentation of this file.
1 #!/usr/bin/python
2 
3 ## @package serverportfolio.SocketServer_Client
4 # To communicate with the server.
5 #
6 # Only use to send string data request / print answer
7 # a simple TCP client using module SocketServer
8 #
9 # Last Changed $Id: SocketServer_Client.py 9 2015-04-02 23:27:25Z michael $
10 #
11 
12 import socket
13 import sys
14 
15 from serverportfolio.GlobalDicts import HOST,PORT
16 
17 # global to the module, because no classes
18 # Create a socket (SOCK_STREAM means a TCP socket)
19 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
20 
21 def Connect():
22  #print "Connect"
23 
24  # here needed, otherwise local variable
25  global sock
26 
27  # Create a socket (SOCK_STREAM means a TCP socket)
28  sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
29 
30  # Connect to server and send data
31  try:
32  sock.connect((HOST, PORT))
33  return True
34  except:
35  return False
36 
37 def SendData ( data ):
38 
39  # use global sock in namespace
40  print "Data to send ", data
41  sock.send(data + "\n")
42  # Receive data from the server and shut down
43  received = sock.recv(1024)
44  sock.close()
45 
46  return received
47 
48 ## test, not used, to delete
49 def SendDataNoReturn ( data ):
50  #print "Data to send no return", data
51  sock.send(data + "\n")
52  if DEBUG :
53  print "data send"
54 
55 #################### main function ##########
56 if __name__ == "__main__":
57 
58  # should make some test ??
59  data = " ".join(sys.argv[1:])
60  #print "data ", data
61 
62  # Create a socket (SOCK_STREAM means a TCP socket)
63  # sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
64 
65  # Connect to server and send data
66  #try:
67  # sock.connect((HOST, PORT))
68  #except:
69  # print "NOT RUNNING"
70  # sys.exit();
71 
72  status = Connect()
73  #print "status connect ", status
74  if status == False :
75  print "NOT RUNNING"
76  sys.exit();
77 
78  #sock.send(data + "\n")
79  ## Receive data from the server and shut down
80  #received = sock.recv(1024)
81  #sock.close()
82  received = SendData( data )
83 
84  #print "Sent: %s" % data
85  #print "Received: %s" % received
86  # version for web application
87  print "%s" % received
def SendDataNoReturn
test, not used, to delete
Global variables for configuration: paths, TCP ports and generic definitions.
Definition: GlobalDicts.py:1