mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-19 00:37:02 +00:00
Seperate client and server of FTP (#1106)
* added sample file to transfer * split client and server into separate files * client and server now work in python2 * server works on python3 * client works on python3 * allow configurable ONE_CONNECTION_ONLY for testing server * allow testing of ftp server + client * use f-strings * removed single letter vars * fixed bad quote marks * clearer file handler names * 'with open() as' syntax * unicode and emojis in the test data * s -> sock * consistent comment spacing * remove closing formalities * swap in and out_file * f-string * if __name__ == '__main__':
This commit is contained in:
parent
561a41464f
commit
9456e81437
|
@ -38,7 +38,6 @@ script:
|
|||
- mypy --ignore-missing-imports .
|
||||
- pytest . --doctest-modules
|
||||
--ignore=file_transfer_protocol/ftp_send_receive.py
|
||||
--ignore=file_transfer_protocol/ftp_client_server.py
|
||||
--ignore=machine_learning/linear_regression.py
|
||||
--ignore=machine_learning/perceptron.py
|
||||
--ignore=machine_learning/random_forest_classification/random_forest_classification.py
|
||||
|
|
23
file_transfer_protocol/client.py
Normal file
23
file_transfer_protocol/client.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')
|
|
@ -1,57 +0,0 @@
|
|||
# server
|
||||
|
||||
import socket # Import socket module
|
||||
|
||||
port = 60000 # Reserve a port for your service.
|
||||
s = socket.socket() # Create a socket object
|
||||
host = socket.gethostname() # Get local machine name
|
||||
s.bind((host, port)) # Bind to the port
|
||||
s.listen(5) # Now wait for client connection.
|
||||
|
||||
print('Server listening....')
|
||||
|
||||
while True:
|
||||
conn, addr = s.accept() # Establish connection with client.
|
||||
print('Got connection from', addr)
|
||||
data = conn.recv(1024)
|
||||
print('Server received', repr(data))
|
||||
|
||||
filename = 'mytext.txt'
|
||||
with open(filename, 'rb') as f:
|
||||
in_data = f.read(1024)
|
||||
while in_data:
|
||||
conn.send(in_data)
|
||||
print('Sent ', repr(in_data))
|
||||
in_data = f.read(1024)
|
||||
|
||||
print('Done sending')
|
||||
conn.send('Thank you for connecting')
|
||||
conn.close()
|
||||
|
||||
|
||||
# client side server
|
||||
|
||||
import socket # Import socket module
|
||||
|
||||
s = socket.socket() # Create a socket object
|
||||
host = socket.gethostname() # Get local machine name
|
||||
port = 60000 # Reserve a port for your service.
|
||||
|
||||
s.connect((host, port))
|
||||
s.send("Hello server!")
|
||||
|
||||
with open('received_file', 'wb') as f:
|
||||
print('file opened')
|
||||
while True:
|
||||
print('receiving data...')
|
||||
data = s.recv(1024)
|
||||
print('data=%s', (data))
|
||||
if not data:
|
||||
break
|
||||
# write data to a file
|
||||
f.write(data)
|
||||
|
||||
f.close()
|
||||
print('Successfully get the file')
|
||||
s.close()
|
||||
print('connection closed')
|
6
file_transfer_protocol/mytext.txt
Normal file
6
file_transfer_protocol/mytext.txt
Normal file
|
@ -0,0 +1,6 @@
|
|||
Hello
|
||||
This is sample data
|
||||
«küßî»
|
||||
“ЌύБЇ”
|
||||
😀😉
|
||||
😋
|
34
file_transfer_protocol/server.py
Normal file
34
file_transfer_protocol/server.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()
|
Loading…
Reference in New Issue
Block a user