chat.py 5.1 KB

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