Advertisement
xosski

Server/client audio/video transfer

Dec 13th, 2024
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. import socket
  2. import os
  3. import threading
  4.  
  5. def send_file(client_socket, file_path):
  6. """
  7. Sends a file to the client with a header indicating file size and type.
  8. """
  9. try:
  10. if not os.path.exists(file_path):
  11. print(f"File {file_path} does not exist.")
  12. return
  13.  
  14. file_size = os.path.getsize(file_path)
  15. filename = os.path.basename(file_path)
  16.  
  17. # Send file header (filename and size)
  18. client_socket.send(f"{filename},{file_size}".encode())
  19.  
  20. # Wait for acknowledgment from the client that it's ready
  21. ack = client_socket.recv(1024).decode()
  22. if ack != "READY":
  23. print("Client is not ready to receive file.")
  24. return
  25.  
  26. # Send the file in chunks
  27. with open(file_path, 'rb') as file:
  28. while (chunk := file.read(4096)):
  29. client_socket.send(chunk)
  30.  
  31. print(f"File {filename} sent successfully.")
  32. except IOError as e:
  33. print(f"Error during file sending: {e}")
  34. finally:
  35. client_socket.close()
  36.  
  37. def handle_client(client_socket, client_address):
  38. """
  39. Handles communication with the client, sends both audio and video files.
  40. """
  41. print(f"Connection established with {client_address}")
  42.  
  43. # Send audio file
  44. send_file(client_socket, "/home/mungowz/PEPPER/microphones/pepper_audio.wav")
  45.  
  46. # Send video file
  47. send_file(client_socket, "/home/mungowz/PEPPER/cameras/pepper_video.avi")
  48.  
  49. def start_server(host, port):
  50. """
  51. Starts the server, listens for client connections and handles them in separate threads.
  52. """
  53. server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  54. server_socket.bind((host, port))
  55. server_socket.listen(5)
  56.  
  57. print(f"Server listening on {host}:{port}")
  58.  
  59. while True:
  60. client_socket, client_address = server_socket.accept()
  61. threading.Thread(target=handle_client, args=(client_socket, client_address)).start()
  62.  
  63. if __name__ == '__main__':
  64. start_server('0.0.0.0', 5000)
  65.  
  66. —————————————-
  67. import socket
  68. import os
  69.  
  70. def receive_file(client_socket, save_path):
  71. """
  72. Receives a file from the server, checks the header for file name and size.
  73. """
  74. try:
  75. # Receive file header (filename and size)
  76. file_header = client_socket.recv(1024).decode()
  77. filename, file_size = file_header.split(',')
  78. file_size = int(file_size)
  79.  
  80. # Send acknowledgment to server that we are ready to receive the file
  81. client_socket.send("READY".encode())
  82.  
  83. # Receive the file in chunks and write to disk
  84. with open(save_path, 'wb') as file:
  85. bytes_received = 0
  86. while bytes_received < file_size:
  87. data = client_socket.recv(4096)
  88. file.write(data)
  89. bytes_received += len(data)
  90.  
  91. print(f"File received: {filename}")
  92. except IOError as e:
  93. print(f"Error during file reception: {e}")
  94. finally:
  95. client_socket.close()
  96.  
  97. if __name__ == '__main__':
  98. host = '127.0.0.1'
  99. port = 5000
  100.  
  101. client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  102. client_socket.connect((host, port))
  103.  
  104. print(f"Client connected to {host}:{port}")
  105.  
  106. # Receive audio file
  107. receive_file(client_socket, "/home/mungowz/pepper_audio_received.wav")
  108.  
  109. # Receive video file
  110. receive_file(client_socket, "/home/mungowz/pepper_video_received.avi")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement