Search code examples
pythonsocketsserverclientserversocket

How to send continous data from server to client in Python?


I am building a server to send data to the client in Python. I would like to continuously send the time until the client closes the connection. So far, I have done :

For the server:

import socket
from datetime import datetime

# take the server name and port name
host = 'local host'
port = 5001

# create a socket at server side
# using TCP / IP protocol
s = socket.socket(socket.AF_INET,
                  socket.SOCK_STREAM)

# bind the socket with server
# and port number
s.bind(('', port))

# allow maximum 1 connection to
# the socket
s.listen(1)

# wait till a client accept
# connection
c, addr = s.accept()

# display client address
print("CONNECTION FROM:", str(addr))

dateTimeObj = str(datetime.now())
print(dateTimeObj)

c.send(dateTimeObj.encode())

# disconnect the server
c.close()

For the client:

import socket

# take the server name and port name

host = 'local host'
port = 5001

# create a socket at client side
# using TCP / IP protocol
s = socket.socket(socket.AF_INET,
                  socket.SOCK_STREAM)

# connect it to server and port
# number on local computer.
s.connect(('127.0.0.1', port))

# receive message string from
# server, at a time 1024 B
msg = s.recv(1024)

# repeat as long as message
# string are not empty
while msg:
    print('Received date :' + msg.decode())
    msg = s.recv(1024)

# disconnect the client
s.close()

How can I modify the server to continously send the current date? At the moment, the server is just sending one date and closing the connection.


Solution

  • you need to use While True loop.

    import socket
    from datetime import datetime
    
    # take the server name and port name
    host = 'local host'
    port = 5001
    
    # create a socket at server side
    # using TCP / IP protocol
    s = socket.socket(socket.AF_INET,
                      socket.SOCK_STREAM)
    
    # bind the socket with server
    # and port number
    s.bind(('', port))
    
    # allow maximum 1 connection to
    # the socket
    s.listen(1)
    
    # wait till a client accept
    # connection
    while True:
        c, addr = s.accept()
    
        # display client address
        print("CONNECTION FROM:", str(addr))
    
        dateTimeObj = str(datetime.now())
        print(dateTimeObj)
    
        c.send(dateTimeObj.encode())
    
        # disconnect the server
        c.close()
    

    client:

    import socket
    
    # take the server name and port name
    
    host = 'local host'
    port = 5001
    
    # create a socket at client side
    # using TCP / IP protocol
    s = socket.socket(socket.AF_INET,
                      socket.SOCK_STREAM)
    
    # connect it to server and port
    # number on local computer.
    s.connect(('127.0.0.1', port))
    
    # receive message string from
    # server, at a time 1024 B
    while True:
        msg = s.recv(1024)
    
        # repeat as long as message
        # string are not empty
        while msg:
            print('Received date :' + msg.decode())
            msg = s.recv(1024)
    
    # disconnect the client
    s.close()