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 2 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
12 changes: 6 additions & 6 deletions file_transfer_protocol/client.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import socket # Import socket module

s = socket.socket() # Create a socket object
sock = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12312

s.connect((host, port))
s.send(b'Hello server!')
sock.connect((host, port))
sock.send(b'Hello server!')

with open('Received_file', 'wb') as f:
with open('Received_file', 'wb') as file:
print('File opened')
print('Receiving data...')
while True:
Expand All @@ -16,8 +16,8 @@
if not data:
break
# write data to a file
f.write(data)
file.write(data)

print('Successfully got the file')
s.close()
sock.close()
print('Connection closed')
26 changes: 13 additions & 13 deletions file_transfer_protocol/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,32 @@
ONE_CONNECTION_ONLY = True # Set this to False if you wish to continuously accept connections

port = 12312 # Reserve a port for your service.
s = socket.socket() # Create a socket object
sock = 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.
sock.bind((host, port)) # Bind to the port
sock.listen(5) # Now wait for client connection.

print('Server listening....')

while True:
conn, addr = s.accept() # Establish connection with client.
conn, addr = sock.accept() # Establish connection with client.
print('Got connection from', addr)
data = conn.recv(1024)
print('Server received', repr(data))

filename='mytext.txt'
f = open(filename,'rb')
l = f.read(1024)
while (l):
conn.send(l)
print('Sent ',repr(l))
l = f.read(1024)
f.close()
file = open(filename,'rb')
data = file.read(1024)
while (data):
conn.send(data)
print('Sent ',repr(data))
data = file.read(1024)
file.close()

print('Done sending')
conn.send(b'Thank you for connecting')
conn.close()
if ONE_CONNECTION_ONLY: # This is to make sure that the program doesn't hang while testing
break
s.shutdown(1)
s.close()
sock.shutdown(1)
sock.close()