0% found this document useful (0 votes)
11 views2 pages

New Text Document

This document contains a Python script that implements a simple TCP server using sockets. The server listens for incoming client connections, sends a welcome message, simulates file transfer progress with messages, and sends a final confirmation message upon completion. It handles each client connection in a separate thread to allow multiple simultaneous connections.

Uploaded by

labibboulwithnin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

New Text Document

This document contains a Python script that implements a simple TCP server using sockets. The server listens for incoming client connections, sends a welcome message, simulates file transfer progress with messages, and sends a final confirmation message upon completion. It handles each client connection in a separate thread to allow multiple simultaneous connections.

Uploaded by

labibboulwithnin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import socket

import threading
import time

def handle_client(client_socket, client_address):


print(f"Connection from {client_address}")
try:
# Send initial message
client_socket.sendall(b"Welcome! fou ta7chelik m3a zbi hhhhhhhhh\n")

# Simulate file transfer with progress messages


progress_messages = [
"Transferring files... [█---------] 10% complete\n",
"Transferring files... [███-------] 40% complete\n",
"Transferring files... [█████-----] 60% complete\n",
"Transferring files... [███████---] 85% complete\n",
"Transferring files... [██████████] 100% complete. Transfer
successful!\n"
]

for msg in progress_messages:


client_socket.sendall(msg.encode("utf-8"))
time.sleep(1) # Add a 1-second delay to simulate progress

# Final message
client_socket.sendall(b"All your files have been successfully copied.\n")
client_socket.sendall(b"ya3tik 3asba w taw ta5rali fih\n")

except Exception as e:
print(f"Error handling client {client_address}: {e}")
finally:
client_socket.close()
print(f"Connection with {client_address} closed.")

def main():
HOST = '' # Listen on all available interfaces
PORT = 1338 # Port to listen on

# Create a socket object


with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as server_socket:
# Allow reusing the address
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

# Bind to the specified host and port


server_socket.bind((HOST, PORT))

# Listen for incoming connections


server_socket.listen(5) # Allow up to 5 queued connections
print(f"Server listening on port {PORT}...")

while True:
# Accept a connection
client_socket, client_address = server_socket.accept()

# Handle the client in a new thread


client_thread = threading.Thread(
target=handle_client, args=(client_socket, client_address)
)
client_thread.start()
if __name__ == "__main__":
main()

You might also like