1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #!/usr/bin/env python3
# coding: utf-8
import socket
import sys
import os
class cSocket:
def __init__(self, port):
self.__port=port
self.__connect=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.__connect.bind(("", port))
print("socket {} prête à écouter sur le port {}".format(self.__connect, self.__port))
# __init__()
def __del__(self):
self.__connect.close()
print("socket {} sur port {} terminée".format(self.__connect, self.__port))
# __del__()
# Ecoute
def listen(self):
while True:
try:
(data, infos)=self.__connect.recvfrom(1024)
except KeyboardInterrupt:
break
# try
try: host=socket.gethostbyaddr(infos[0])[0]
except socket.herror as e: host="?"
txt=data.decode("utf-8")
print("adr=[{}]: [{}]{}".format(host, txt, " => Arrêt du client" if txt == "EOT" else ""))
# while
print("socket {} sur port {} terminé !!!".format(self.__connect, self.__port))
# listen()
# class cSocket
if __name__ == "__main__":
cSocket(int(sys.argv[1])).listen() |
Partager