|
- #!/usr/bin/env python
-
- import socket, argparse, subprocess
-
- if __name__ == "__main__":
- parser = argparse.ArgumentParser(description='Simple Client')
- parser.add_argument('-p', default=1337, type=int, help='Serverport the server is listen on.')
- parser.add_argument('-s', type=str, required=True, help='IPv4 address of the servers')
- parser.add_argument('--tcp', action='store_true', help='Use TCP.')
- parser.add_argument('--udp', action='store_true', help='Use UDP.')
- parser.add_argument('--raspivid', defalt='-t 0 -fps 20 -w 1280 -h 720 -b 2000000 -o', help='Raspivid arguments.')
- args = parser.parse_args()
-
- if args.udp and args.tcp:
- print("Only one protocol is allowed.")
- exit(1)
- elif not args.udp and not args.tcp:
- print("Specify protocol")
- exit(1)
-
- BUFFER_SIZE = 1400
-
- # IP und Port des Servers
- IP = args.s
- PORT = args.p
- # Unterstützte Addresstypen (IPv4, IPv6, lokale Addressen)
- address_families = (socket.AF_INET, socket.AF_INET6, socket.AF_UNIX)
- # Unterstützte Sockettypen (TCP, UDP, Raw (ohne Typ))
- socket_types = (socket.SOCK_STREAM, socket.SOCK_DGRAM, socket.SOCK_RAW)
- # Passenden Address- und Sockettyp wählen
- address_family = address_families[0]
- if args.tcp:
- socket_type = socket_types[0]
- else:
- socket_type = socket_types[1]
- # Erstellen eines Sockets (TCP und UDP)
- sock = socket.socket(address_family, socket_type)
- try:
- if args.tcp:
- # Verbinden zu einem Server-Socket (Nur TCP)
- sock.connect((IP,PORT))
-
- # raspivid starten
- cmd_raspivid = 'raspivid ' + args.raspivid + ' -'
- rasprocess = subprocess.Popen(cmd_raspivid,shell=True,stdout=subprocess.PIPE)
-
- while True:
- # Daten der Größe BUFFER_SIZE aus der Ausgabe von raspivid auslesen
- message = rasprocess.stdout.read(BUFFER_SIZE)
-
- if args.tcp:
- # TCP
- sock.send(message)
- else:
- # UDP
- sock.sendto(message, (IP, PORT))
-
- except KeyboardInterrupt:
- print("Close socket.")
- sock.close()
- print("Exiting through keyboard event (CTRL + C)")
|