chat.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #! /usr/bin/python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. This file is part of librelight.
  5. librelight is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, version 2 of the License.
  8. librelight is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with librelight. If not, see <http://www.gnu.org/licenses/>.
  14. (c) 2012 micha@uxsrv.de
  15. """
  16. import socket
  17. import sys
  18. import time
  19. class tcp_sender(object):
  20. def __init__(self,port=50000):
  21. self.port = port
  22. self.connect()
  23. def connect(self,client_name="unkown"):
  24. self.xip = "127.0.0.1" #raw_input("IP-Adresse: ")
  25. self.xs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  26. try:
  27. self.xs.connect((self.xip, self.port)) #50000))
  28. except ConnectionRefusedError as e:
  29. print("ConnectionRefusedError: ", "ERR: {0} ".format(e.args) ,end="")
  30. print("Server nicht ereichbar/unterbrochen")
  31. time.sleep(1)
  32. self.connect()
  33. print("connected !")
  34. def send(self,nachricht):
  35. try:
  36. self.xs.send(bytes(nachricht+";","utf-8") )
  37. except socket.error as e:
  38. self.connect()
  39. def close(self):
  40. self.xs.close()
  41. def dummyCB(msg):
  42. print("dummy_CB",msg)
  43. def cmd(cb=dummyCB,port=50000):
  44. import socket
  45. import select
  46. server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  47. server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  48. #self.xs.getsockopt(socket.AF_INET, socket.SO_REUSEADDR )
  49. while 1:
  50. try:
  51. server.bind(("", port))
  52. break
  53. except Exception as e:
  54. print("except",e)
  55. print( "bind error")
  56. time.sleep(1)
  57. server.listen(1)
  58. clients = []
  59. clients2 = [""]*300
  60. try:
  61. while True:
  62. lesen, schreiben, oob = select.select([server] + clients,
  63. [], [])
  64. for sock in lesen:
  65. if sock is server:
  66. client, addr = server.accept()
  67. client.setblocking(0)
  68. clients.append(client)
  69. print("+++ Client %s verbunden" % addr[0])
  70. #sock.send("hi du")
  71. else:
  72. msg=b''
  73. try:
  74. xmsg = sock.recv(1024)#5120)
  75. except BlockingIOError as e:
  76. pass#print( "exception",e)
  77. try:
  78. while xmsg:
  79. msg += xmsg
  80. xmsg = sock.recv(1024)#5120)
  81. xmsg = xmsg.replace(b";",b"")
  82. #print(msg)
  83. except BlockingIOError as e:
  84. pass#print( "exception",e)
  85. if not msg:
  86. continue
  87. nachricht = msg
  88. #print(msg)
  89. nachricht = str(nachricht,"utf-8")
  90. nachricht = nachricht.replace(";","")
  91. nachrichten = nachricht.strip().replace("EOB","")
  92. if "client_name:" in nachrichten:
  93. if sock in clients:
  94. client_nr = clients.index(sock)
  95. clients2[client_nr] = nachrichten
  96. if sock in clients:
  97. client_nr = clients.index(sock)
  98. #print(clients2[client_nr])
  99. ip = sock.getpeername()[0]
  100. #print(">>>", ip, nachrichten.split(";"))
  101. if nachrichten:
  102. tstamp = time.strftime("%H:%M:%S")
  103. #print("from:",client_nr,">>>", tstamp , ip, nachrichten.split(";"))
  104. for xx,nachricht in enumerate(nachrichten.split(";")):
  105. cmd = nachricht #.split(" ")
  106. #print(xx,cmd)
  107. cb({"c":client_nr,"cmd":cmd})
  108. else:
  109. print("+++ Verbindung zu %s beendet" % ip)
  110. sock.close()
  111. if sock in clients:
  112. client_nr = clients.index(sock)
  113. clients2[client_nr] = ""
  114. clients.remove(sock)
  115. except KeyboardInterrupt:
  116. print(" strg+c")
  117. finally:
  118. for c in clients:
  119. print(c,"close")
  120. c.close()
  121. server.close()
  122. print("server close")
  123. if __name__ == "__main__":
  124. print( sys.argv )
  125. if sys.argv[1] == "server":
  126. cmd()
  127. elif sys.argv[1] == "client":
  128. c = tcp_sender()
  129. while 1:
  130. x = input(":: ")
  131. c.send(x)