Skip to content

Commit 9456e81

Browse files
AlexDvorakcclauss
authored andcommitted
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__':
1 parent 561a414 commit 9456e81

File tree

5 files changed

+63
-58
lines changed

5 files changed

+63
-58
lines changed

Diff for: .travis.yml

-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ script:
3838
- mypy --ignore-missing-imports .
3939
- pytest . --doctest-modules
4040
--ignore=file_transfer_protocol/ftp_send_receive.py
41-
--ignore=file_transfer_protocol/ftp_client_server.py
4241
--ignore=machine_learning/linear_regression.py
4342
--ignore=machine_learning/perceptron.py
4443
--ignore=machine_learning/random_forest_classification/random_forest_classification.py

Diff for: file_transfer_protocol/client.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
if __name__ == '__main__':
2+
import socket # Import socket module
3+
4+
sock = socket.socket() # Create a socket object
5+
host = socket.gethostname() # Get local machine name
6+
port = 12312
7+
8+
sock.connect((host, port))
9+
sock.send(b'Hello server!')
10+
11+
with open('Received_file', 'wb') as out_file:
12+
print('File opened')
13+
print('Receiving data...')
14+
while True:
15+
data = sock.recv(1024)
16+
print(f"data={data}")
17+
if not data:
18+
break
19+
out_file.write(data) # Write data to a file
20+
21+
print('Successfully got the file')
22+
sock.close()
23+
print('Connection closed')

Diff for: file_transfer_protocol/ftp_client_server.py

-57
This file was deleted.

Diff for: file_transfer_protocol/mytext.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Hello
2+
This is sample data
3+
«küßî»
4+
“ЌύБЇ”
5+
😀😉
6+
😋

Diff for: file_transfer_protocol/server.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
if __name__ == '__main__':
2+
import socket # Import socket module
3+
4+
ONE_CONNECTION_ONLY = True # Set this to False if you wish to continuously accept connections
5+
6+
filename='mytext.txt'
7+
port = 12312 # Reserve a port for your service.
8+
sock = socket.socket() # Create a socket object
9+
host = socket.gethostname() # Get local machine name
10+
sock.bind((host, port)) # Bind to the port
11+
sock.listen(5) # Now wait for client connection.
12+
13+
print('Server listening....')
14+
15+
while True:
16+
conn, addr = sock.accept() # Establish connection with client.
17+
print(f"Got connection from {addr}")
18+
data = conn.recv(1024)
19+
print(f"Server received {data}")
20+
21+
with open(filename,'rb') as in_file:
22+
data = in_file.read(1024)
23+
while (data):
24+
conn.send(data)
25+
print(f"Sent {data!r}")
26+
data = in_file.read(1024)
27+
28+
print('Done sending')
29+
conn.close()
30+
if ONE_CONNECTION_ONLY: # This is to make sure that the program doesn't hang while testing
31+
break
32+
33+
sock.shutdown(1)
34+
sock.close()

0 commit comments

Comments
 (0)