diff --git a/.travis.yml b/.travis.yml index 9abbb0365..2536e72fa 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/file_transfer_protocol/client.py b/file_transfer_protocol/client.py new file mode 100644 index 000000000..f404546d7 --- /dev/null +++ b/file_transfer_protocol/client.py @@ -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') diff --git a/file_transfer_protocol/ftp_client_server.py b/file_transfer_protocol/ftp_client_server.py deleted file mode 100644 index 414c336de..000000000 --- a/file_transfer_protocol/ftp_client_server.py +++ /dev/null @@ -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') diff --git a/file_transfer_protocol/mytext.txt b/file_transfer_protocol/mytext.txt new file mode 100644 index 000000000..54cfa7f76 --- /dev/null +++ b/file_transfer_protocol/mytext.txt @@ -0,0 +1,6 @@ +Hello +This is sample data +«küßî» +“ЌύБЇ” +😀😉 +😋 diff --git a/file_transfer_protocol/server.py b/file_transfer_protocol/server.py new file mode 100644 index 000000000..92fab206c --- /dev/null +++ b/file_transfer_protocol/server.py @@ -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()