Skip to content

Commit 238fe8c

Browse files
Update receive_file.py (TheAlgorithms#8541)
* Update receive_file.py Here are the changes I made: Added the main() function and called it from if __name__ == "__main__" block. This makes it easier to test the code and import it into other programs. Added socket.AF_INET as the first argument to socket.socket(). This specifies the address family to be used, which is necessary when using connect(). Changed print(f"{data = }") to print("Received:", len(data), "bytes"). This makes it clearer what's happening and how much data is being received. Changed the final print statement to "Successfully received the file". This makes it more accurate and descriptive. Moved the import statement to the top of the file. This is a common convention in Python. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent a004929 commit 238fe8c

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

Diff for: file_transfer/receive_file.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
if __name__ == "__main__":
2-
import socket # Import socket module
1+
import socket
2+
33

4-
sock = socket.socket() # Create a socket object
5-
host = socket.gethostname() # Get local machine name
4+
def main():
5+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
6+
host = socket.gethostname()
67
port = 12312
78

89
sock.connect((host, port))
@@ -13,11 +14,14 @@
1314
print("Receiving data...")
1415
while True:
1516
data = sock.recv(1024)
16-
print(f"{data = }")
1717
if not data:
1818
break
19-
out_file.write(data) # Write data to a file
19+
out_file.write(data)
2020

21-
print("Successfully got the file")
21+
print("Successfully received the file")
2222
sock.close()
2323
print("Connection closed")
24+
25+
26+
if __name__ == "__main__":
27+
main()

0 commit comments

Comments
 (0)