title: Python - counting times a server receives request from client?
body: <p>I'm playing around with python lately and trying to learn how to build a python server, using TCP connections.</p>
  
  <p>I have this code that runs a server...</p>
  
  <pre><code>import socket
  from threading import *
  import datetime
  import time
  
  
  serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  host = "localhost"
  port = 8000
  print (host)
  print (port)
  
  serversocket.bind((host, port))
  
  class client(Thread):
  
      def __init__(self, socket, address):
          Thread.__init__(self)
          self.sock = socket
          self.addr = address
          self.start()
  
  
      def run(self):
  
          while 1:
  
              st = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S')
              #cName  =
              print(self.sock.recv(1024).decode()+' sent @ '+ st + ':' , self.sock.recv(1024).decode())
  
              self.sock.send(b'Processing!')
  
  
  
  
  
  serversocket.listen(5)
  print ('server started and listening')
  while 1:
      clientsocket, address = serversocket.accept()
      client(clientsocket, address)
  </code></pre>
  
  <p>And two of these client.py</p>
  
  <pre><code>import socket
  
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  host ="localhost"
  port =8000
  cName = 'client 2' # or client 1
  s.connect((host,port))
  
  def ts(str):
     s.sendall(cName.encode())
     s.send('e'.encode())
     data = ''
     data = s.recv(1024).decode()
     print (data)
  
  while 2:
     r = input('enter')
     ts(s)
  
  s.close ()
  </code></pre>
  
  <p>I want to know how to allow the server to count and keep track of how many times it recieves a message from both client 1 and client 2.</p>
  
  <p>For example, if server starts at count of 0 (e.g. <code>count = 0)</code>. And each time client 1 or client 2 sends back a message or in this case, hits enter, the count will go up (<code>count += 1</code>). If I call for a <code>print(count)</code>, the output should be <code>1</code>.</p>
  
  <p>Thanks?</p>