Skip to content

Seperate client and server of FTP #1106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions file_transfer_protocol/client.py
Original file line number Diff line number Diff line change
@@ -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')
57 changes: 0 additions & 57 deletions file_transfer_protocol/ftp_client_server.py

This file was deleted.

6 changes: 6 additions & 0 deletions file_transfer_protocol/mytext.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Hello
This is sample data
«küßî»
“ЌύБЇ”
😀😉
😋
34 changes: 34 additions & 0 deletions file_transfer_protocol/server.py
Original file line number Diff line number Diff line change
@@ -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()