forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_file.py
35 lines (27 loc) · 1.01 KB
/
send_file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
defsend_file(filename: str="mytext.txt", testing: bool=False) ->None:
importsocket
port=12312# Reserve a port for your service.
sock=socket.socket() # Create a socket object
host=socket.gethostname() # Get local machine name
sock.bind((host, port)) # Bind to the port
sock.listen(5) # Now wait for client connection.
print("Server listening....")
whileTrue:
conn, addr=sock.accept() # Establish connection with client.
print(f"Got connection from {addr}")
data=conn.recv(1024)
print(f"Server received: {data=}")
withopen(filename, "rb") asin_file:
data=in_file.read(1024)
whiledata:
conn.send(data)
print(f"Sent {data!r}")
data=in_file.read(1024)
print("Done sending")
conn.close()
iftesting: # Allow the test to complete
break
sock.shutdown(1)
sock.close()
if__name__=="__main__":
send_file()