NickSimkin
3/21/2018 - 5:50 PM

send message over socket in python

send message over socket in python

import socket
import time

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port

s.listen(5)                 # Now wait for client connection.
while True:
   c, addr = s.accept()     # Establish connection with client.
   print 'Got connection from', addr

   counter = 0

   while True:
      c.send('Message #{counter}'.format(counter=counter))
      counter += 1
      time.sleep(0.1)
      print(counter)

   c.close()