-k: Keep inbound sockets open for multiple connects#

By default, pync’s TCP server will accept one client before closing the server’s socket.

By using the -k option, you can keep the server open to serve multiple clients one after another.

Creating a Date/Time Server#

  1. Combining -k with the -l and -e options, we can create a simple date/time server that stays open between connections:

pync -kle date localhost 8000
py -m pync -kle "time /t && date /t" localhost 8000
# datetime_server.py
import platform
import pync

command = 'date'
if platform.system() == 'Windows':
    command = 'time /t && date /t'

pync.run('-kle {} localhost 8000'.format(command))
  1. To test this, connect to the server on a separate console:

pync localhost 8000
py -m pync localhost 8000
import pync
pync.run('localhost 8000')

By setting the -k option on the server, you should be able to keep connecting to it to get the current time and date.

When you’re finished, hit Ctrl+C on the server console to close the server.



SEE ALSO: