You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 satır
2.2KB

  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. parser.add_argument('--raspivid', defalt='-t 0 -fps 20 -w 1280 -h 720 -b 2000000 -o', help='Raspivid arguments.')
  10. args = parser.parse_args()
  11. if args.udp and args.tcp:
  12. print("Only one protocol is allowed.")
  13. exit(1)
  14. elif not args.udp and not args.tcp:
  15. print("Specify protocol")
  16. exit(1)
  17. BUFFER_SIZE = 1400
  18. # IP und Port des Servers
  19. IP = args.s
  20. PORT = args.p
  21. # Unterstützte Addresstypen (IPv4, IPv6, lokale Addressen)
  22. address_families = (socket.AF_INET, socket.AF_INET6, socket.AF_UNIX)
  23. # Unterstützte Sockettypen (TCP, UDP, Raw (ohne Typ))
  24. socket_types = (socket.SOCK_STREAM, socket.SOCK_DGRAM, socket.SOCK_RAW)
  25. # Passenden Address- und Sockettyp wählen
  26. address_family = address_families[0]
  27. if args.tcp:
  28. socket_type = socket_types[0]
  29. else:
  30. socket_type = socket_types[1]
  31. # Erstellen eines Sockets (TCP und UDP)
  32. sock = socket.socket(address_family, socket_type)
  33. try:
  34. if args.tcp:
  35. # Verbinden zu einem Server-Socket (Nur TCP)
  36. sock.connect((IP,PORT))
  37. # raspivid starten
  38. cmd_raspivid = 'raspivid ' + args.raspivid + ' -'
  39. rasprocess = subprocess.Popen(cmd_raspivid,shell=True,stdout=subprocess.PIPE)
  40. while True:
  41. # Daten der Größe BUFFER_SIZE aus der Ausgabe von raspivid auslesen
  42. message = rasprocess.stdout.read(BUFFER_SIZE)
  43. if args.tcp:
  44. # TCP
  45. sock.send(message)
  46. else:
  47. # UDP
  48. sock.sendto(message, (IP, PORT))
  49. except KeyboardInterrupt:
  50. print("Close socket.")
  51. sock.close()
  52. print("Exiting through keyboard event (CTRL + C)")