-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#
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
from pync import pync
command = 'date'
if platform.system() == 'Windows':
command = 'time /t && date /t'
pync('-kle {} localhost 8000'.format(command))
To test this, connect to the server on a separate console:
pync localhost 8000
py -m pync localhost 8000
from pync import pync
pync('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