(filename: str = "mytext.txt", testing: bool = False)
| 1 | def send_file(filename: str = "mytext.txt", testing: bool = False) -> None: |
| 2 | import socket |
| 3 | |
| 4 | port = 12312 # Reserve a port for your service. |
| 5 | sock = socket.socket() # Create a socket object |
| 6 | host = socket.gethostname() # Get local machine name |
| 7 | sock.bind((host, port)) # Bind to the port |
| 8 | sock.listen(5) # Now wait for client connection. |
| 9 | |
| 10 | print("Server listening....") |
| 11 | |
| 12 | while True: |
| 13 | conn, addr = sock.accept() # Establish connection with client. |
| 14 | print(f"Got connection from {addr}") |
| 15 | data = conn.recv(1024) |
| 16 | print(f"Server received: {data = }") |
| 17 | |
| 18 | with open(filename, "rb") as in_file: |
| 19 | data = in_file.read(1024) |
| 20 | while data: |
| 21 | conn.send(data) |
| 22 | print(f"Sent {data!r}") |
| 23 | data = in_file.read(1024) |
| 24 | |
| 25 | print("Done sending") |
| 26 | conn.close() |
| 27 | if testing: # Allow the test to complete |
| 28 | break |
| 29 | |
| 30 | sock.shutdown(1) |
| 31 | sock.close() |
| 32 | |
| 33 | |
| 34 | if __name__ == "__main__": |
no outgoing calls