Last active
July 30, 2024 12:31
-
-
Save AstroEngineeer/27f1f93cbf21686770ad3bcdc9f69060 to your computer and use it in GitHub Desktop.
Transfer files using TCP sockets.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) | |
s.connect((socket.gethostname(),1234)) | |
msg = s.recv(1024) | |
print("File received.") | |
filename = input("Save file as:") | |
filetoWrite = open(file=filename,mode="wb") | |
filetoWrite.write(msg) | |
filetoWrite.close() | |
print("File saved.") | |
print("---------------------Contents of File----------------------------------") | |
print(msg.decode("utf-8")) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) | |
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) | |
s.bind((socket.gethostname(),1234)) | |
s.listen() | |
filename = input("Enter the name of the file to send: ") | |
fileToSend = open(file=filename,mode="rb") | |
print("Waiting for Receiver to connect.") | |
conn,add = s.accept() | |
print("Sending File to {}....".format(add)) | |
conn.sendfile(fileToSend) | |
fileToSend.close() | |
print("File sent.") | |
conn.close() | |
s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment