chat.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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):
  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, 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):
  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. server.bind(("", 50000))
  50. server.listen(1)
  51. clients = []
  52. clients2 = [""]*300
  53. try:
  54. while True:
  55. lesen, schreiben, oob = select.select([server] + clients,
  56. [], [])
  57. for sock in lesen:
  58. if sock is server:
  59. client, addr = server.accept()
  60. clients.append(client)
  61. print("+++ Client %s verbunden" % addr[0])
  62. #sock.send("hi du")
  63. else:
  64. nachricht = sock.recv(1024)
  65. nachricht = str(nachricht,"utf-8")
  66. nachrichten = nachricht.strip().replace("EOB","")
  67. if "client_name:" in nachrichten:
  68. if sock in clients:
  69. client_nr = clients.index(sock)
  70. clients2[client_nr] = nachrichten
  71. if sock in clients:
  72. client_nr = clients.index(sock)
  73. print(clients2[client_nr])
  74. ip = sock.getpeername()[0]
  75. #print(">>>", ip, nachrichten.split(";"))
  76. if nachrichten:
  77. tstamp = time.strftime("%H:%M:%S")
  78. print("from:",client_nr,">>>", tstamp , ip, nachrichten.split(";"))
  79. for xx,nachricht in enumerate(nachrichten.split(";")):
  80. cmd = nachricht #.split(" ")
  81. print(xx,cmd)
  82. cb({"c":client_nr,"cmd":cmd})
  83. else:
  84. print("+++ Verbindung zu %s beendet" % ip)
  85. sock.close()
  86. if sock in clients:
  87. client_nr = clients.index(sock)
  88. clients2[client_nr] = ""
  89. clients.remove(sock)
  90. except KeyboardInterrupt:
  91. print(" strg+c")
  92. finally:
  93. for c in clients:
  94. print(c,"close")
  95. c.close()
  96. server.close()
  97. print("server close")
  98. if __name__ == "__main__":
  99. print( sys.argv )
  100. if sys.argv[1] == "server":
  101. cmd()
  102. elif sys.argv[1] == "client":
  103. c = tcp_sender()
  104. while 1:
  105. x = input(":: ")
  106. c.send(x)