chat.py 4.2 KB

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