Skip to content

Instantly share code, notes, and snippets.

@AstroEngineeer
Last active July 30, 2024 12:31
Show Gist options
  • Save AstroEngineeer/27f1f93cbf21686770ad3bcdc9f69060 to your computer and use it in GitHub Desktop.
Save AstroEngineeer/27f1f93cbf21686770ad3bcdc9f69060 to your computer and use it in GitHub Desktop.
Transfer files using TCP sockets.
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"))
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