mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 01:09:40 +08:00
Rename file_transfer and linear_algebra (#1118)
* Rename file_transfer and linear_algebra * Rename file_transfer and linear_algebra
This commit is contained in:
36
file_transfer/ftp_send_receive.py
Normal file
36
file_transfer/ftp_send_receive.py
Normal file
@ -0,0 +1,36 @@
|
||||
"""
|
||||
File transfer protocol used to send and receive files using FTP server.
|
||||
Use credentials to provide access to the FTP client
|
||||
|
||||
Note: Do not use root username & password for security reasons
|
||||
Create a seperate user and provide access to a home directory of the user
|
||||
Use login id and password of the user created
|
||||
cwd here stands for current working directory
|
||||
"""
|
||||
|
||||
from ftplib import FTP
|
||||
ftp = FTP('xxx.xxx.x.x') # Enter the ip address or the domain name here
|
||||
ftp.login(user='username', passwd='password')
|
||||
ftp.cwd('/Enter the directory here/')
|
||||
|
||||
"""
|
||||
The file which will be received via the FTP server
|
||||
Enter the location of the file where the file is received
|
||||
"""
|
||||
|
||||
def ReceiveFile():
|
||||
FileName = 'example.txt' """ Enter the location of the file """
|
||||
with open(FileName, 'wb') as LocalFile:
|
||||
ftp.retrbinary('RETR ' + FileName, LocalFile.write, 1024)
|
||||
ftp.quit()
|
||||
|
||||
"""
|
||||
The file which will be sent via the FTP server
|
||||
The file send will be send to the current working directory
|
||||
"""
|
||||
|
||||
def SendFile():
|
||||
FileName = 'example.txt' """ Enter the name of the file """
|
||||
with open(FileName, 'rb') as LocalFile:
|
||||
ftp.storbinary('STOR ' + FileName, LocalFile)
|
||||
ftp.quit()
|
6
file_transfer/mytext.txt
Normal file
6
file_transfer/mytext.txt
Normal file
@ -0,0 +1,6 @@
|
||||
Hello
|
||||
This is sample data
|
||||
«küßî»
|
||||
“ЌύБЇ”
|
||||
😀😉
|
||||
😋
|
23
file_transfer/recieve_file.py
Normal file
23
file_transfer/recieve_file.py
Normal file
@ -0,0 +1,23 @@
|
||||
if __name__ == '__main__':
|
||||
import socket # Import socket module
|
||||
|
||||
sock = socket.socket() # Create a socket object
|
||||
host = socket.gethostname() # Get local machine name
|
||||
port = 12312
|
||||
|
||||
sock.connect((host, port))
|
||||
sock.send(b'Hello server!')
|
||||
|
||||
with open('Received_file', 'wb') as out_file:
|
||||
print('File opened')
|
||||
print('Receiving data...')
|
||||
while True:
|
||||
data = sock.recv(1024)
|
||||
print(f"data={data}")
|
||||
if not data:
|
||||
break
|
||||
out_file.write(data) # Write data to a file
|
||||
|
||||
print('Successfully got the file')
|
||||
sock.close()
|
||||
print('Connection closed')
|
34
file_transfer/send_file.py
Normal file
34
file_transfer/send_file.py
Normal file
@ -0,0 +1,34 @@
|
||||
if __name__ == '__main__':
|
||||
import socket # Import socket module
|
||||
|
||||
ONE_CONNECTION_ONLY = True # Set this to False if you wish to continuously accept connections
|
||||
|
||||
filename='mytext.txt'
|
||||
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....')
|
||||
|
||||
while True:
|
||||
conn, addr = sock.accept() # Establish connection with client.
|
||||
print(f"Got connection from {addr}")
|
||||
data = conn.recv(1024)
|
||||
print(f"Server received {data}")
|
||||
|
||||
with open(filename,'rb') as in_file:
|
||||
data = in_file.read(1024)
|
||||
while (data):
|
||||
conn.send(data)
|
||||
print(f"Sent {data!r}")
|
||||
data = in_file.read(1024)
|
||||
|
||||
print('Done sending')
|
||||
conn.close()
|
||||
if ONE_CONNECTION_ONLY: # This is to make sure that the program doesn't hang while testing
|
||||
break
|
||||
|
||||
sock.shutdown(1)
|
||||
sock.close()
|
Reference in New Issue
Block a user