Python/file_transfer_protocol/server.py
AlexDvorak 9456e81437 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__':
2019-08-07 15:44:48 +02:00

35 lines
1.1 KiB
Python

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()