Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

64 lines
2.4KB

  1. #!/usr/bin/env python
  2. import socket, argparse, subprocess
  3. if __name__ == "__main__":
  4. parser = argparse.ArgumentParser(description='Simple Server')
  5. parser.add_argument('-p', default=1337, type=int, help='Serverport the server is listen on.')
  6. parser.add_argument('-a', type=str, required=True, help='IPv4 address of the servers interface for bind.')
  7. parser.add_argument('--tcp', action='store_true', help='Use TCP.')
  8. parser.add_argument('--udp', action='store_true', help='Use UDP.')
  9. args = parser.parse_args()
  10. if args.udp and args.tcp:
  11. print("Only one protocol is allowed.")
  12. exit(1)
  13. elif not args.udp and not args.tcp:
  14. print("Specify protocol")
  15. exit(1)
  16. # Port des Servers
  17. PORT = args.p
  18. # Lesepuffergröße
  19. BUFFER_SIZE = 1400
  20. # Unterstützte Addresstypen (IPv4, IPv6, lokale Addressen)
  21. address_families = (socket.AF_INET, socket.AF_INET6, socket.AF_UNIX)
  22. # Unterstützte Sockettypen (TCP, UDP, Raw (ohne Typ))
  23. socket_types = (socket.SOCK_STREAM, socket.SOCK_DGRAM, socket.SOCK_RAW)
  24. # Passenden Address- und Sockettyp wählen
  25. address_family = address_families[0]
  26. if args.tcp:
  27. socket_type = socket_types[0]
  28. else:
  29. socket_type = socket_types[1]
  30. # Maximale Anzahl der Verbindungen in der Warteschlange
  31. backlog = 1
  32. try:
  33. # Erstellen eines Socket (TCP und UDP)
  34. sock = socket.socket(address_family, socket_type)
  35. sock.bind((args.a, PORT))
  36. if args.tcp:
  37. # Lausche am Socket auf eingehende Verbindungen (Nur TCP)
  38. sock.listen(backlog)
  39. clientsocket, address = sock.accept()
  40. # mplayer starten
  41. cmd_mplayer = 'mplayer -fps 25 -cache 512 -'
  42. mprocess = subprocess.Popen(cmd_mplayer, shell=True, stdin=subprocess.PIPE)
  43. while True:
  44. # Daten (der Größe BUFFER_SIZE) aus dem Socket holen und ausgeben:
  45. if args.tcp:
  46. # TCP:
  47. data = clientsocket.recv(BUFFER_SIZE)
  48. else:
  49. # UDP:
  50. data, address = sock.recvfrom(BUFFER_SIZE)
  51. # Die ausgelesenen Daten an mplayer weiterleiten
  52. mprocess.stdin.write(data)
  53. except KeyboardInterrupt:
  54. print("Close socket.")
  55. sock.close()
  56. print("Exiting through keyboard event (CTRL + C)")