Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

61 lines
2.1KB

  1. #!/usr/bin/env python
  2. import socket, argparse, subprocess
  3. if __name__ == "__main__":
  4. parser = argparse.ArgumentParser(description='Simple Client')
  5. parser.add_argument('-p', default=1337, type=int, help='Serverport the server is listen on.')
  6. parser.add_argument('-s', type=str, required=True, help='IPv4 address of the servers')
  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. BUFFER_SIZE = 1400
  17. # IP und Port des Servers
  18. IP = args.s
  19. PORT = args.p
  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. # Erstellen eines Sockets (TCP und UDP)
  31. sock = socket.socket(address_family, socket_type)
  32. try:
  33. if args.tcp:
  34. # Verbinden zu einem Server-Socket (Nur TCP)
  35. sock.connect((IP,PORT))
  36. # raspivid starten
  37. cmd_raspivid = 'raspivid -t 0 -fps 20 -w 1280 -h 720 -b 2000000 -o -'
  38. rasprocess = subprocess.Popen(cmd_raspivid,shell=True,stdout=subprocess.PIPE)
  39. while True:
  40. # Daten der Größe BUFFER_SIZE aus der Ausgabe von raspivid auslesen
  41. message = rasprocess.stdout.read(BUFFER_SIZE)
  42. if args.tcp:
  43. # TCP
  44. sock.send(message)
  45. else:
  46. # UDP
  47. sock.sendto(message, (IP, PORT))
  48. except KeyboardInterrupt:
  49. print("Close socket.")
  50. sock.close()
  51. print("Exiting through keyboard event (CTRL + C)")